37:46f343bd26af default tip
Anton Shestakov <av6@dwimlabs.net>, Thu, 05 Jul 2018 22:55:53 +0800
demo: jQuery 3.3.1

previous change 34:36496092e7d7

test/underscore-methods.js

Permissions: -rw-r--r--

Other formats: Feeds:
var a, b, c, d, col, shard;
module('Backbone.Shard', {
setup: function() {
a = new Backbone.Model({data: 'a'});
b = new Backbone.Model({data: 'b'});
c = new Backbone.Model({data: 'c'});
d = new Backbone.Model({data: 'd'});
col = new Backbone.Collection([a, b, c, d]);
shard = new Backbone.Shard({
collection: col,
filter: function(model) { return model.get('data') !== 'd'; }
});
}
});
test('Underscore methods: basics', function() {
strictEqual(shard.first(), a, 'first() works');
strictEqual(shard.last(), c, 'last() works');
deepEqual(shard.initial(), [a, b], 'initial() works');
deepEqual(shard.rest(), [b, c], 'rest() works');
equal(shard.isEmpty(), false, 'isEmpty() works');
deepEqual(shard.toArray(), [a, b, c], 'toArray() works');
equal(shard.all(_.identity), true, 'all() works');
equal(shard.any(), true, 'any() works');
deepEqual(shard.sortBy(function(model) {
return -model.get('data').charCodeAt(0);
}), [c, b, a], 'sortBy() works');
deepEqual(shard.groupBy(function(model) {
return model.get('data').charCodeAt(0) % 2;
}), {0: [b], 1: [a, c]}, 'groupBy() works');
deepEqual(shard.partition(function(model) {
return model.get('data').charCodeAt(0) % 2;
}), [[a, c], [b]], 'partition() works');
deepEqual(shard.chunk(1), [[a], [b], [c]], 'chunk() works');
deepEqual(shard.indexBy('data'), {a: a, b: b, c: c}, 'indexBy() works');
var sh = shard.shuffle().reduce(function(memo, model) { return memo += model.get('data'); }, '');
ok(sh.length === 3 && _(sh).contains('a') && _(sh).contains('b') && _(sh).contains('c'), 'shuffle() works');
var s = shard.sample(999).reduce(function(memo, model) { return memo += model.get('data'); }, '');
ok(s.length === 3 && _(s).contains('a') && _(s).contains('b') && _(s).contains('c'), 'sample() works');
});
test('Underscore methods: searching and filtering', function() {
equal(shard.indexOf(c), 2, 'indexOf() works');
equal(shard.lastIndexOf(c), 2, 'lastIndexOf() works');
equal(shard.findIndex(function(model) {
return model.get('data').charCodeAt(0) % 2;
}), 0, 'findIndex() works');
equal(shard.findLastIndex(function(model) {
return model.get('data').charCodeAt(0) % 2;
}), 2, 'findLastIndex() works');
strictEqual(shard.min(function(model) {
return model.get('data').charCodeAt(0);
}), a, 'min() works');
strictEqual(shard.max(function(model) {
return model.get('data').charCodeAt(0);
}), c, 'max() works');
strictEqual(shard.find(function(model) {
return model.get('data') === 'b';
}), b, 'find() works');
deepEqual(shard.filter(function(model) {
return model.get('data') !== 'b';
}), [a, c], 'filter() works');
deepEqual(shard.reject(function(model) {
return model.get('data') === 'b';
}), [a, c], 'reject() works');
equal(shard.contains(b), true, 'contains() works');
deepEqual(shard.without(b), [a, c], 'without() works');
deepEqual(shard.difference([a, c]), [b], 'difference() works');
});
test('Underscore methods: iterating over models', function() {
var sdata = '';
shard.each(function(model) { sdata += model.get('data'); });
equal(sdata, 'abc', 'each() works');
equal(shard.map(function(model) {
return model.get('data');
}).join(' '), 'a b c', 'map() works');
shard.invoke('set', 'updated', true);
equal(_.all(shard.pluck('updated')), true, 'invoke() works');
});
test('Underscore methods: data hunt', function() {
equal(shard.pluck('data').join(' '), 'a b c', 'pluck() works');
equal(shard
.chain()
.map(function(model) { return model.get('data'); })
.first()
.value(), 'a', 'chain() works');
equal(shard.reduce(function(memo, model) {
return memo += model.get('data');
}, ''), 'abc', 'reduce() works');
equal(shard.reduceRight(function(memo, model) {
return memo += model.get('data');
}, ''), 'cba', 'reduceRight() works');
});
test('Underscore methods: method aliases', function() {
strictEqual(shard.head(), a, 'head() works');
deepEqual(shard.tail(), [b, c], 'tail() works');
deepEqual(shard.drop(), [b, c], 'drop() works');
deepEqual(shard.countBy('data'), {a: 1, b: 1, c: 1}, 'countBy() works');
equal(shard.includes(b), true, 'includes() works');
deepEqual(shard.select(function(model) {
return model.get('data') !== 'b';
}), [a, c], 'select() works');
equal(shard.foldl(function(memo, model) {
return memo += model.get('data');
}, ''), 'abc', 'foldl() works');
});