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/sensible-filters.js

Permissions: -rw-r--r--

Other formats: Feeds:
var a, b, c, d, col, otherCol;
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]);
otherCol = new Backbone.Collection();
}
});
test('Sensible filter function, collection is prepopulated', function() {
var shard = new Backbone.Shard({
collection: col,
filter: function(model) { return _(['a', 'c']).contains(model.get('data')); }
});
equal(shard.size(), 2, 'shard has 2 items');
equal(shard.first(), a, 'a is the first element');
equal(shard.last(), c, 'c is the last element');
});
test('Sensible filter function, collection populated dynamically', function() {
var shard = new Backbone.Shard({
collection: otherCol,
filter: function(model) { return _(['a', 'c']).contains(model.get('data')); }
});
var additions = 0;
var removals = 0;
var resets = 0;
shard.on('add', function() {
additions++;
}).on('remove', function() {
removals++;
}).on('reset', function() {
resets++;
});
otherCol.add([a, b, c, d]);
equal(additions, 2, 'adding models matching shard filter to collection triggers add events');
equal(shard.size(), 2, '... and the shard has 2 items');
otherCol.remove([b, d]);
equal(removals, 0, 'removing models not matching shard filter from collection does not trigger remove events');
equal(shard.size(), 2, '... and the shard has 2 items');
otherCol.remove([a, c]);
equal(removals, 2, 'removing models matching shard filter from collection triggers remove events');
equal(shard.size(), 0, '... and the shard is empty');
otherCol.reset([a, b, c, d]);
equal(resets, 1, 'resetting collection triggers reset events');
equal(shard.size(), 2, '... and the shard has 2 items');
});