Download:
child 4:223997f08b4c
parent 2:e1d4f3c5b403
3:c2023ec9fa6a
Anton Shestakov <engored@ya.ru>, Wed, 05 Dec 2012 02:43:19 +0900
Testing Underscore methods.

2 файлов изменено, 95 вставок(+), 0 удалений(-) [+]
test/index.html file | annotate | diff | comparison | revisions
test/underscore-methods.js file | annotate | diff | comparison | revisions
--- a/test/index.html Wed Dec 05 02:41:55 2012 +0900
+++ b/test/index.html Wed Dec 05 02:43:19 2012 +0900
@@ -11,6 +11,7 @@
<script src="../backbone.shard.js"></script>
<script src="basic-filters.js"></script>
<script src="sensible-filters.js"></script>
+ <script src="underscore-methods.js"></script>
</head>
<body>
<div id="qunit"></div>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/test/underscore-methods.js Wed Dec 05 02:43:19 2012 +0900
@@ -0,0 +1,94 @@
+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
+ sortBy
+ groupBy
+ sortedIndex
+ shuffle
+*/
+
+test('Underscore methods: basics', function() {
+ equal(shard.first(), a, 'first() works');
+ equal(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');
+});
+
+test('Underscore methods: searching and filtering', function() {
+ equal(shard.min(function(model) {
+ return model.get('data').charCodeAt(0);
+ }), a, 'min() works');
+
+ equal(shard.max(function(model) {
+ return model.get('data').charCodeAt(0);
+ }), c, 'max() works');
+
+ equal(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');
+});