Download:
child 10:9af1bfa5800d
parent 8:dd50fe10144c
9:f3aad748ea73
Anton Shestakov <engored@ya.ru>, Thu, 13 Dec 2012 03:19:14 +0900
More tests.

3 файлов изменено, 66 вставок(+), 9 удалений(-) [+]
test/basic-filters.js file | annotate | diff | comparison | revisions
test/changing-models.js file | annotate | diff | comparison | revisions
test/index.html file | annotate | diff | comparison | revisions
--- a/test/basic-filters.js Wed Dec 12 19:41:08 2012 +0900
+++ b/test/basic-filters.js Thu Dec 13 03:19:14 2012 +0900
@@ -1,4 +1,4 @@
-var a, b, c, col, otherCol;
+var a, b, c, col, otherCol, additions, removals, resets;
module('Backbone.Shard', {
setup: function() {
@@ -7,6 +7,10 @@
c = new Backbone.Model({data: 'c'});
col = new Backbone.Collection([a, b, c]);
otherCol = new Backbone.Collection();
+
+ additions = 0;
+ removals = 0;
+ resets = 0;
}
});
@@ -27,10 +31,6 @@
filter: function() { return false; }
});
- var additions = 0;
- var removals = 0;
- var resets = 0;
-
shard.on('add', function() {
additions++;
}).on('remove', function() {
@@ -70,10 +70,6 @@
filter: function() { return true; }
});
- var additions = 0;
- var removals = 0;
- var resets = 0;
-
shard.on('add', function() {
additions++;
}).on('remove', function() {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/changing-models.js Thu Dec 13 03:19:14 2012 +0900
@@ -0,0 +1,60 @@
+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');
+});
--- a/test/index.html Wed Dec 12 19:41:08 2012 +0900
+++ b/test/index.html Thu Dec 13 03:19:14 2012 +0900
@@ -12,6 +12,7 @@
<script src="basic-filters.js"></script>
<script src="sensible-filters.js"></script>
<script src="underscore-methods.js"></script>
+ <script src="changing-models.js"></script>
</head>
<body>
<div id="qunit"></div>