1 | var _ = require('underscore');
|
2 | var test = require('tape');
|
3 | var AmpersandState = require('ampersand-state');
|
4 | var AmpersandCollection = require('ampersand-collection');
|
5 | var AmpersandUnderscoreMixins = require('../ampersand-collection-underscore-mixin');
|
6 | var collection;
|
7 |
|
8 | var Model = AmpersandState.extend({
|
9 | props: {
|
10 | id: 'number',
|
11 | foo: 'string',
|
12 | bar: 'string'
|
13 | }
|
14 | });
|
15 |
|
16 | var Collection = AmpersandCollection.extend(AmpersandUnderscoreMixins, {
|
17 | model: Model
|
18 | });
|
19 |
|
20 | var methods = ['forEach', 'each', 'map', 'collect', 'reduce', 'foldl',
|
21 | 'inject', 'reduceRight', 'foldr', 'find', 'detect', 'filter', 'select',
|
22 | 'reject', 'every', 'all', 'some', 'any', 'include', 'contains', 'invoke',
|
23 | 'max', 'min', 'toArray', 'size', 'first', 'head', 'take', 'initial', 'rest',
|
24 | 'tail', 'drop', 'last', 'without', 'difference', 'indexOf', 'shuffle',
|
25 | 'lastIndexOf', 'isEmpty', 'chain', 'sample', 'partition',
|
26 | 'groupBy', 'countBy', 'sortBy', 'indexBy'
|
27 | ];
|
28 |
|
29 | test('extended collection contains all necessary methods', function (t) {
|
30 | _.each(methods, function (method) {
|
31 | t.ok(Collection.prototype[method], 'extended collection contains ' + method + ' method');
|
32 | });
|
33 | t.end();
|
34 | });
|
35 |
|
36 | test('`where` and `findWhere` methods should filter a collection based on a given attributes', function (t) {
|
37 | var collection = new Collection([
|
38 | { id: 1, foo: 'baz', bar: 'baz' },
|
39 | { id: 2, foo: 'baz', bar: 'baz' },
|
40 | { id: 3, foo: 'baz', bar: 'qux' },
|
41 | { id: 4, foo: 'qux', bar: 'qux' },
|
42 | { id: 5, foo: 'qux', bar: 'qux' },
|
43 | { id: 6, foo: 'qux', bar: 'baz' }
|
44 | ]);
|
45 |
|
46 | var whereSingleAttr = collection.where({
|
47 | foo: 'baz'
|
48 | });
|
49 | t.equal(whereSingleAttr.length, 3, 'find all matching models for a given attribute');
|
50 |
|
51 | var whereMultipleAttr = collection.where({
|
52 | foo: 'qux',
|
53 | bar: 'qux'
|
54 | });
|
55 | t.equal(whereMultipleAttr.length, 2, 'find all matching models for a given attributes');
|
56 |
|
57 | var findWhereSingleAttr = collection.findWhere({
|
58 | foo: 'baz'
|
59 | });
|
60 | t.ok(findWhereSingleAttr, 'return first matching model for a given attribute');
|
61 | t.equal(findWhereSingleAttr.get('id'), 1, 'verify that returned model is indeed first possible match');
|
62 |
|
63 | var findWhereMultipleAttr = collection.findWhere({
|
64 | foo: 'qux',
|
65 | bar: 'qux'
|
66 | });
|
67 | t.ok(findWhereMultipleAttr, 'return first matching model for a given attributes');
|
68 | t.equal(findWhereMultipleAttr.get('id'), 4, 'verify that returned model is indeed first possible match');
|
69 |
|
70 | t.end();
|
71 | });
|
72 |
|
73 | test('`pluck` method should get attribute value from each model', function (t) {
|
74 | var collection = new Collection([
|
75 | { id: 1, foo: 'baz', bar: 'qux' },
|
76 | { id: 2, foo: 'qux', bar: 'baz' }
|
77 | ]);
|
78 |
|
79 | var foo = collection.pluck('foo');
|
80 | t.deepEqual(foo, ['baz', 'qux']), 'verify that returned attribute values are correct';
|
81 |
|
82 | var bar = collection.pluck('bar');
|
83 | t.deepEqual(bar, ['qux', 'baz'], 'verify that returned attribute values are correct');
|
84 |
|
85 | t.end();
|
86 | });
|