UNPKG

5.22 kBJavaScriptView Raw
1var assert = require("assert");
2var ExtendedModel = require("../../lib/ExtendedModel.js");
3var ExtendedCollection = require("../../lib/ExtendedCollection.js");
4
5var TestModel = ExtendedModel.extend("TestModel", { id_attribute: "id" });
6var TestCollection = ExtendedCollection.extend("TestCollection", { model: TestModel });
7
8var currentFileMark = ["\t\t\t", "[", __filename, "]", "\n"].join("");
9describe('ExtendedCollection -> indexBy' + currentFileMark, function(){
10
11 it("Sets collection index by constructor options", function(next){
12 var collection = new TestCollection([
13 { id: 1, field: "a" },
14 { id: 2, field: "b" },
15 { id: 3, field: "c" },
16 { id: 4, field: "d" },
17 { id: 5, field: "e" },
18 ], {index: {field: "field"}});
19
20 assert.equal(collection.getBy("field", "a"), collection.get(1));
21 assert.equal(collection.getBy("field", "b"), collection.get(2));
22 assert.equal(collection.getBy("field", "c"), collection.get(3));
23 assert.equal(collection.getBy("field", "d"), collection.get(4));
24 assert.equal(collection.getBy("field", "e"), collection.get(5));
25
26 next();
27 });
28
29 it("Sets collection index by prototype property", function(next){
30 var IndexedCollection = TestCollection.extend("IndexedCollection", {
31 index: { field: "field" }
32 });
33 var collection = new IndexedCollection([
34 { id: 1, field: "a" },
35 { id: 2, field: "b" },
36 { id: 3, field: "c" },
37 { id: 4, field: "d" },
38 { id: 5, field: "e" },
39 ]);
40
41 assert.equal(collection.getBy("field", "a"), collection.get(1));
42 assert.equal(collection.getBy("field", "b"), collection.get(2));
43 assert.equal(collection.getBy("field", "c"), collection.get(3));
44 assert.equal(collection.getBy("field", "d"), collection.get(4));
45 assert.equal(collection.getBy("field", "e"), collection.get(5));
46
47 next();
48 });
49
50 it("Sets collection index by calling indexBy method", function(next){
51
52 var collection = new TestCollection([
53 { id: 1, field: "a" },
54 { id: 2, field: "b" },
55 { id: 3, field: "c" },
56 { id: 4, field: "d" },
57 { id: 5, field: "e" },
58 ]);
59 collection.indexBy("field");
60
61 assert.equal(collection.getBy("field", "a"), collection.get(1));
62 assert.equal(collection.getBy("field", "b"), collection.get(2));
63 assert.equal(collection.getBy("field", "c"), collection.get(3));
64 assert.equal(collection.getBy("field", "d"), collection.get(4));
65 assert.equal(collection.getBy("field", "e"), collection.get(5));
66
67 next();
68 });
69
70 it("Updates index on model change", function(next){
71
72 var collection = new TestCollection([
73 { id: 1, field: "a" },
74 { id: 2, field: "b" },
75 { id: 3, field: "c" },
76 { id: 4, field: "d" },
77 { id: 5, field: "e" },
78 ]);
79 collection.indexBy("field");
80
81 collection.get(1).set({field: "x"});
82 assert.equal(collection.getBy("field", "a"), undefined);
83 assert.equal(collection.getBy("field", "x"), collection.get(1));
84
85 next();
86 });
87
88 it("Updates index on model remove", function(next){
89
90 var collection = new TestCollection([
91 { id: 1, field: "a" },
92 { id: 2, field: "b" },
93 { id: 3, field: "c" },
94 { id: 4, field: "d" },
95 { id: 5, field: "e" },
96 ]);
97 collection.indexBy("field");
98 collection.add();
99
100 collection.remove(1);
101 assert.equal(collection.getBy("field", "a"), undefined);
102 next();
103 });
104
105 it("Sets index with iterator", function(next){
106
107 var collection = new TestCollection([
108 { id: 1, field: "a" },
109 { id: 2, field: "b" },
110 { id: 3, field: "c" },
111 { id: 4, field: "d" },
112 { id: 5, field: "e" },
113 ]);
114 collection.indexBy("field", function(model){
115 return model.get("field")+"_"+model.id;
116 });
117
118 assert.equal(collection.getBy("field", "a_1"), collection.get(1));
119 assert.equal(collection.getBy("field", "b_2"), collection.get(2));
120 assert.equal(collection.getBy("field", "c_3"), collection.get(3));
121 assert.equal(collection.getBy("field", "d_4"), collection.get(4));
122 assert.equal(collection.getBy("field", "e_5"), collection.get(5));
123
124 collection.get(1).set({field: "x"});
125 assert.equal(collection.getBy("field", "a_1"), undefined);
126 assert.equal(collection.getBy("field", "x_1"), collection.get(1));
127
128 next();
129 });
130
131 it("dropIndex", function(next){
132
133 var collection = new TestCollection([
134 { id: 1, field: "a" },
135 { id: 2, field: "b" },
136 { id: 3, field: "c" },
137 { id: 4, field: "d" },
138 { id: 5, field: "e" },
139 ]);
140 collection.indexBy("field", function(model){
141 return model.get("field")+"_"+model.id;
142 });
143
144 collection.dropIndex("field");
145
146 assert.equal(collection.getBy("field", "a_1"), undefined );
147 assert.equal(collection.getBy("field", "b_2"), undefined );
148 assert.equal(collection.getBy("field", "c_3"), undefined );
149 assert.equal(collection.getBy("field", "d_4"), undefined );
150 assert.equal(collection.getBy("field", "e_5"), undefined );
151
152 next();
153 });
154
155});