11:4232194a24ca
Anton Shestakov <engored@ya.ru>, Fri, 14 Dec 2012 23:54:33 +0900
Changes for Backbone 0.9.9: Shards now also proxy Underscore method name aliases.

next change 14:01b83171cef5
previous change 8:dd50fe10144c

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'; }
});
}
});
// TODO: test sortedIndex
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.indexOf(c), 2, 'indexOf() works');
equal(shard.lastIndexOf(c), 2, 'lastIndexOf() works');
equal(shard.isEmpty(), false, 'isEmpty() works');
deepEqual(shard.toArray(), [a, b, c], 'toArray() works');
equal(shard.contains(b), true, 'contains() works');
deepEqual(shard.without(b), [a, c], 'without() 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');
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');
});
test('Underscore methods: searching and filtering', function() {
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');
});
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 (working in Backbone.Collection since 0.9.9)', function() {
strictEqual(shard.head(), a, 'head() works');
deepEqual(shard.tail(), [b, c], 'tail() works');
deepEqual(shard.countBy('data'), {a: 1, b: 1, c: 1}, 'countBy() 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');
});