1 | var assert = require("assert");
|
2 | var BaseCollection = require("../../lib/Collection.js");
|
3 | var ExtendedModel = require("../../lib/ExtendedModel.js");
|
4 | var ExtendedCollection = require("../../lib/ExtendedCollection.js");
|
5 |
|
6 | var TestModel = ExtendedModel.extend("TestModel", { id_attribute: "id" });
|
7 | var TestCollection = ExtendedCollection.extend("TestCollection", { model: TestModel });
|
8 |
|
9 | var currentFileMark = ["\t\t\t", "[", __filename, "]", "\n"].join("");
|
10 | describe('ExtendedCollection -> groupBy' + currentFileMark, function(){
|
11 |
|
12 | it("Sets collection group by constructor options", function(next){
|
13 | var collection = new TestCollection([
|
14 | { id: 1, field: "a" },
|
15 | { id: 2, field: "a" },
|
16 | { id: 3, field: "b" },
|
17 | { id: 4, field: "b" },
|
18 | { id: 5, field: "b" },
|
19 | ], { group: {field: "field"}} );
|
20 |
|
21 | assert.equal(collection.getGroup("field", "a") instanceof BaseCollection, true );
|
22 | assert.equal(collection.getGroup("field", "b") instanceof BaseCollection, true );
|
23 | assert.equal(collection.getGroup("field", "a").length, 2);
|
24 | assert.equal(collection.getGroup("field", "b").length, 3);
|
25 |
|
26 | next();
|
27 | });
|
28 |
|
29 | it("Sets collection group by prototype property", function(next){
|
30 | var GroupedCollection = TestCollection.extend("GroupedCollection", { group: {field: "field"} } );
|
31 | var collection = new GroupedCollection([
|
32 | { id: 1, field: "a" },
|
33 | { id: 2, field: "a" },
|
34 | { id: 3, field: "b" },
|
35 | { id: 4, field: "b" },
|
36 | { id: 5, field: "b" },
|
37 | ]);
|
38 |
|
39 | assert.equal(collection.getGroup("field", "a") instanceof BaseCollection, true );
|
40 | assert.equal(collection.getGroup("field", "b") instanceof BaseCollection, true );
|
41 | assert.equal(collection.getGroup("field", "a").length, 2);
|
42 | assert.equal(collection.getGroup("field", "b").length, 3);
|
43 |
|
44 | next();
|
45 | });
|
46 |
|
47 | it("Updates groups on model change", function(next){
|
48 | var GroupedCollection = TestCollection.extend("GroupedCollection", { group: {field: "field"} } );
|
49 | var collection = new GroupedCollection([
|
50 | { id: 1, field: "a" },
|
51 | { id: 2, field: "a" },
|
52 | { id: 3, field: "b" },
|
53 | { id: 4, field: "b" },
|
54 | { id: 5, field: "b" },
|
55 | ]);
|
56 |
|
57 | collection.get(1).set({field: "b"});
|
58 |
|
59 | assert.equal(collection.getGroup("field", "a").length, 1);
|
60 | assert.equal(collection.getGroup("field", "b").length, 4);
|
61 | next();
|
62 | });
|
63 |
|
64 | it("Create group on model change", function(next){
|
65 | var GroupedCollection = TestCollection.extend("GroupedCollection", { group: {field: "field"} } );
|
66 | var collection = new GroupedCollection([
|
67 | { id: 1, field: "a" },
|
68 | { id: 2, field: "a" },
|
69 | { id: 3, field: "b" },
|
70 | { id: 4, field: "b" },
|
71 | { id: 5, field: "b" },
|
72 | ]);
|
73 |
|
74 | collection.get(1).set({field: "c"});
|
75 |
|
76 | assert.equal(collection.getGroup("field", "a").length, 1);
|
77 | assert.equal(collection.getGroup("field", "c").length, 1);
|
78 | assert.equal(collection.getGroup("field", "b").length, 3);
|
79 | next();
|
80 | });
|
81 |
|
82 | it("Drop group when no models from index", function(next){
|
83 | var GroupedCollection = TestCollection.extend("GroupedCollection", { group: {field: "field"} } );
|
84 | var collection = new GroupedCollection([
|
85 | { id: 1, field: "a" },
|
86 | { id: 2, field: "a" },
|
87 | { id: 3, field: "b" },
|
88 | { id: 4, field: "b" },
|
89 | { id: 5, field: "b" },
|
90 | ]);
|
91 |
|
92 | collection.get(1).set({field: "c"});
|
93 | collection.get(2).set({field: "c"});
|
94 |
|
95 | assert.equal(collection.getGroup("field", "a"), undefined );
|
96 | next();
|
97 | });
|
98 |
|
99 | it("groupBy array", function(next){
|
100 | var GroupedCollection = TestCollection.extend("GroupedCollection", { group: {field: "field"} } );
|
101 | var collection = new GroupedCollection([
|
102 | { id: 1, field: ["a", "c"] },
|
103 | { id: 2, field: ["a", "d"] },
|
104 | { id: 3, field: ["b", "c"] },
|
105 | { id: 4, field: ["d", "c"] },
|
106 | { id: 5, field: ["b", "d"] },
|
107 | ]);
|
108 |
|
109 | assert.equal(collection.getGroup("field", "a").length, 2 );
|
110 | assert.equal(collection.getGroup("field", "b").length, 2 );
|
111 | assert.equal(collection.getGroup("field", "c").length, 3 );
|
112 | assert.equal(collection.getGroup("field", "d").length, 3 );
|
113 |
|
114 | collection.get(1).set({field: ["a", "d"]});
|
115 | collection.get(3).set({field: ["b", "d"]});
|
116 |
|
117 | assert.equal(collection.getGroup("field", "a").length, 2 );
|
118 | assert.equal(collection.getGroup("field", "b").length, 2 );
|
119 | assert.equal(collection.getGroup("field", "c").length, 1 );
|
120 | assert.equal(collection.getGroup("field", "d").length, 5 );
|
121 |
|
122 | collection.get(4).set({field: ["d", "d"]});
|
123 |
|
124 | assert.equal(collection.getGroup("field", "d").length, 5 );
|
125 | assert.equal(collection.getGroup("field", "c"), undefined);
|
126 | next();
|
127 | });
|
128 |
|
129 | it("Emits group:*name* when new group is created and drop:*name* when group is removed", function(next){
|
130 | var collection = new TestCollection([],{ group: {field: "field"}} );
|
131 |
|
132 | var created = [];
|
133 | var removed = [];
|
134 | collection.on("group:field", function(index, group){
|
135 | created.push([index, group.length > 0 ]);
|
136 | });
|
137 | collection.on("dropGroup:field", function(index){
|
138 | removed.push(index);
|
139 | });
|
140 |
|
141 | collection.add([
|
142 | { id: 1, field: "a" },
|
143 | { id: 2, field: "a" },
|
144 | { id: 3, field: "b" },
|
145 | { id: 4, field: "b" },
|
146 | { id: 5, field: "b" },
|
147 | ]);
|
148 |
|
149 | assert.deepEqual(created, [
|
150 | ["a", true],
|
151 | ["b", true],
|
152 | ]);
|
153 |
|
154 | collection.remove(1);
|
155 | assert.deepEqual(removed, []);
|
156 | collection.remove(2);
|
157 | assert.deepEqual(removed, ["a"]);
|
158 | assert.equal(collection.getGroup("field", "a"), undefined );
|
159 |
|
160 | var prevModels;
|
161 | collection.once("reset", function(collection, options){ prevModels = options.previousModels });
|
162 |
|
163 | collection.reset([]);
|
164 | assert.deepEqual(removed, ["a", "b"]);
|
165 | assert.equal(collection.getGroup("field", "b"), undefined );
|
166 |
|
167 | next();
|
168 | });
|
169 |
|
170 | });
|