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

previous change 22:03d3495b69d9

test/basic-filters.js

Permissions: -rw-r--r--

Other formats: Feeds:
var a, b, c, col, otherCol, additions, removals, resets;
module('Backbone.Shard', {
setup: function() {
a = new Backbone.Model({data: 'a'});
b = new Backbone.Model({data: 'b'});
c = new Backbone.Model({data: 'c'});
col = new Backbone.Collection([a, b, c]);
otherCol = new Backbone.Collection();
additions = 0;
removals = 0;
resets = 0;
}
});
test('All-exclusive filter function, collection is prepopulated', function() {
var shard = new Backbone.Shard({
collection: col,
filter: function() { return false; }
});
equal(shard.size(), 0, 'shard is empty');
equal(shard.first(), undefined, 'nothing is the first element');
equal(shard.last(), undefined, 'nothing is the last element');
});
test('All-exclusive filter function, collection populated dynamically', function() {
var shard = new Backbone.Shard({
collection: otherCol,
filter: function() { return false; }
});
shard.on('add', function() {
additions++;
}).on('remove', function() {
removals++;
}).on('reset', function() {
resets++;
});
otherCol.add([a, b, c]);
equal(additions, 0, 'adding models not matching shard filter to collection does not trigger add events');
equal(shard.size(), 0, '... and the shard is empty');
otherCol.remove([a, b, c]);
equal(removals, 0, 'removing models not matching shard filter from collection does not trigger remove events');
equal(shard.size(), 0, '... and the shard is empty');
otherCol.reset([a, b, c]);
equal(resets, 1, 'resetting collection triggers reset events');
equal(shard.size(), 0, '... and the shard is empty');
});
test('All-inclusive filter function, collection is prepopulated', function() {
var shard = new Backbone.Shard({
collection: col,
filter: function() { return true; }
});
equal(shard.size(), 3, 'shard has 3 items');
equal(shard.first(), a, 'a is the first element');
equal(shard.indexOf(b), 1, 'b is the second element');
equal(shard.last(), c, 'c is the last element');
});
test('All-inclusive filter function, collection populated dynamically', function() {
var shard = new Backbone.Shard({
collection: otherCol,
filter: function() { return true; }
});
shard.on('add', function() {
additions++;
}).on('remove', function() {
removals++;
}).on('reset', function() {
resets++;
});
otherCol.add([a, b, c]);
equal(additions, 3, 'adding models matching shard filter to collection triggers add events');
equal(shard.size(), 3, '... and the shard has 3 items');
otherCol.remove([a, b, c]);
equal(removals, 3, 'removing models matching shard filter from collection triggers remove events');
equal(shard.size(), 0, '... and the shard is empty');
otherCol.reset([a, b, c]);
equal(resets, 1, 'resetting collection triggers reset events');
equal(shard.size(), 3, '... and the shard has 3 items');
});