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/changing-models.js

Permissions: -rw-r--r--

Other formats: Feeds:
var a, b, c, d, col, additions, removals, changes, likechanges, readShard, unreadShard;
module('Backbone.Shard', {
setup: function() {
a = new Backbone.Model({data: 'a', read: true, like: false});
b = new Backbone.Model({data: 'b', read: true, like: false});
c = new Backbone.Model({data: 'c', read: false, like: false});
d = new Backbone.Model({data: 'd', read: false, like: false});
col = new Backbone.Collection([a, b, c, d]);
readShard = new Backbone.Shard({
collection: col,
filter: function(model) { return model.get('read'); }
});
unreadShard = new Backbone.Shard({
collection: col,
filter: function(model) { return !model.get('read'); }
});
additions = {read: 0, unread: 0};
removals = {read: 0, unread: 0};
changes = {read: 0, unread: 0};
likechanges = {read: 0, unread: 0};
readShard.on('add', function() {
additions.read++;
}).on('remove', function() {
removals.read++;
}).on('change', function() {
changes.read++;
}).on('change:like', function() {
likechanges.read++;
});
unreadShard.on('add', function() {
additions.unread++;
}).on('remove', function() {
removals.unread++;
}).on('change', function() {
changes.unread++;
}).on('change:like', function() {
likechanges.unread++;
});
}
});
test('Updating model attributes not used in shard filter', function() {
a.set('like', true);
equal(likechanges.read, 1, 'change:like triggered in readShard');
equal(likechanges.unread, 0, 'change:like not triggered in unreadShard');
});
test('Updating model attributes used in shard filter', function() {
a.set('read', false);
deepEqual(readShard.size(), 1, 'removes from readShard');
equal(removals.read, 1, '... and triggers remove event');
equal(unreadShard.size(), 3, 'adds to unreadShard');
equal(additions.unread, 1, '... and triggers add event');
});