resourcerer 1.2.3 → 2.0.0-beta
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +133 -164
- package/build/index.d.ts +9 -0
- package/build/index.js +9 -0
- package/build/lib/collection.d.ts +107 -0
- package/build/lib/collection.js +251 -0
- package/build/lib/config.d.ts +20 -0
- package/build/lib/config.js +23 -0
- package/build/lib/error-boundary.d.ts +12 -0
- package/build/lib/error-boundary.js +18 -0
- package/build/lib/events.d.ts +11 -0
- package/build/lib/events.js +12 -0
- package/build/lib/model-cache.d.ts +13 -0
- package/build/lib/model-cache.js +62 -0
- package/build/lib/model.d.ts +48 -0
- package/build/lib/model.js +170 -0
- package/build/lib/prefetch.d.ts +3 -0
- package/build/lib/prefetch.js +27 -0
- package/build/lib/request.d.ts +20 -0
- package/build/lib/request.js +53 -0
- package/build/lib/resourcerer.d.ts +13 -0
- package/build/lib/resourcerer.js +406 -0
- package/build/lib/sync.d.ts +18 -0
- package/build/lib/sync.js +58 -0
- package/build/lib/types.d.ts +66 -0
- package/build/lib/types.js +1 -0
- package/build/lib/utils.d.ts +14 -0
- package/build/lib/utils.js +87 -0
- package/docs/advanced_topics.md +9 -77
- package/docs/collection.md +28 -28
- package/docs/migrating_to_2.0.md +72 -0
- package/docs/model.md +21 -21
- package/docs/typescript.md +68 -20
- package/package.json +27 -30
- package/resourcerer.d.ts +50 -0
- package/test/collection.test.js +305 -316
- package/test/config.test.js +21 -75
- package/test/events.test.js +42 -41
- package/test/model-cache.test.js +99 -77
- package/test/model.test.js +204 -209
- package/test/prefetch.test.js +35 -61
- package/test/request.test.js +104 -109
- package/test/sync.test.js +122 -109
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -270
- package/index.js +0 -14
- package/lib/collection.js +0 -584
- package/lib/config.js +0 -131
- package/lib/constants.js +0 -6
- package/lib/error-boundary.js +0 -47
- package/lib/events.js +0 -19
- package/lib/model-cache.js +0 -131
- package/lib/model.js +0 -372
- package/lib/prefetch.js +0 -59
- package/lib/request.js +0 -126
- package/lib/resourcerer.js +0 -985
- package/lib/sync.js +0 -111
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/test/collection.test.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import * as sync from
|
|
2
|
-
import Collection from
|
|
3
|
-
import Model from
|
|
1
|
+
import * as sync from "../lib/sync";
|
|
2
|
+
import Collection from "../lib/collection";
|
|
3
|
+
import Model from "../lib/model";
|
|
4
|
+
import { vi } from "vitest";
|
|
4
5
|
|
|
5
|
-
describe(
|
|
6
|
+
describe("Collection", () => {
|
|
6
7
|
var collection,
|
|
7
|
-
|
|
8
|
+
callback = vi.fn();
|
|
8
9
|
|
|
9
10
|
beforeEach(() => {
|
|
10
|
-
|
|
11
|
+
vi.spyOn(sync, "default").mockResolvedValue([]);
|
|
11
12
|
});
|
|
12
13
|
|
|
13
14
|
afterEach(() => {
|
|
@@ -16,385 +17,377 @@ describe('Collection', () => {
|
|
|
16
17
|
callback.mockClear();
|
|
17
18
|
});
|
|
18
19
|
|
|
19
|
-
it(
|
|
20
|
-
collection = new Collection;
|
|
20
|
+
it("is instantiated with appropriate internal properties", () => {
|
|
21
|
+
collection = new Collection();
|
|
21
22
|
|
|
22
23
|
expect(collection.length).toEqual(0);
|
|
23
24
|
expect(collection.models).toEqual([]);
|
|
24
25
|
expect(collection._byId).toEqual({});
|
|
25
26
|
});
|
|
26
27
|
|
|
27
|
-
it(
|
|
28
|
+
it("can override class properties via the options hash", () => {
|
|
28
29
|
class _Model extends Model {}
|
|
29
30
|
class __Model extends Model {}
|
|
30
31
|
|
|
31
32
|
class _Collection extends Collection {
|
|
32
|
-
static Model = _Model
|
|
33
|
+
static Model = _Model;
|
|
33
34
|
|
|
34
|
-
static comparator =
|
|
35
|
+
static comparator = "name";
|
|
35
36
|
}
|
|
36
37
|
|
|
37
38
|
collection = new _Collection();
|
|
38
|
-
expect(collection.comparator).toEqual(
|
|
39
|
+
expect(collection.comparator).toEqual("name");
|
|
39
40
|
expect(collection.Model).toEqual(_Model);
|
|
40
41
|
|
|
41
|
-
collection = new _Collection([], {Model: __Model, comparator:
|
|
42
|
-
expect(collection.comparator).toEqual(
|
|
42
|
+
collection = new _Collection([], { Model: __Model, comparator: "email" });
|
|
43
|
+
expect(collection.comparator).toEqual("email");
|
|
43
44
|
expect(collection.Model).toEqual(__Model);
|
|
44
45
|
});
|
|
45
46
|
|
|
46
|
-
it(
|
|
47
|
-
collection = new Collection([], {one: 1, two: 2, comparator:
|
|
47
|
+
it("unreserved option items are assigned to a `urlOptions` instance property", () => {
|
|
48
|
+
collection = new Collection([], { one: 1, two: 2, comparator: "foo", parse: true });
|
|
48
49
|
|
|
49
|
-
expect(collection.urlOptions).toEqual({one: 1, two: 2});
|
|
50
|
+
expect(collection.urlOptions).toEqual({ one: 1, two: 2 });
|
|
50
51
|
expect(collection.toJSON()).toEqual([]);
|
|
51
52
|
});
|
|
52
53
|
|
|
53
|
-
it(
|
|
54
|
-
collection = new Collection([{id:
|
|
54
|
+
it("urlOptions get passed down to models", async () => {
|
|
55
|
+
collection = new Collection([{ id: "1" }], { one: 1, two: 2, comparator: "foo", parse: true });
|
|
55
56
|
|
|
56
|
-
expect(collection.get(
|
|
57
|
+
expect(collection.get("1").urlOptions).toEqual({ one: 1, two: 2 });
|
|
57
58
|
|
|
58
|
-
collection.add({id:
|
|
59
|
-
expect(collection.get(
|
|
59
|
+
collection.add({ id: "2" });
|
|
60
|
+
expect(collection.get("2").urlOptions).toEqual({ one: 1, two: 2 });
|
|
60
61
|
|
|
61
|
-
sync.default.mockResolvedValue([[{id:
|
|
62
|
+
sync.default.mockResolvedValue([[{ id: "3" }], {}]);
|
|
62
63
|
await collection.fetch();
|
|
63
|
-
expect(collection.get(
|
|
64
|
+
expect(collection.get("3").urlOptions).toEqual({ one: 1, two: 2 });
|
|
64
65
|
});
|
|
65
66
|
|
|
66
|
-
it(
|
|
67
|
-
var model1 = new Model,
|
|
68
|
-
|
|
67
|
+
it("adds any models passed to its models list", () => {
|
|
68
|
+
var model1 = new Model(),
|
|
69
|
+
model2 = new Model();
|
|
69
70
|
|
|
70
71
|
collection = new Collection([model1, model2]);
|
|
71
72
|
expect(collection.models).toEqual([model1, model2]);
|
|
72
73
|
|
|
73
|
-
collection = new Collection([{id:
|
|
74
|
-
expect(collection.models.map((model) => model.toJSON()))
|
|
75
|
-
|
|
76
|
-
|
|
74
|
+
collection = new Collection([{ id: "model3" }, { id: "model4" }]);
|
|
75
|
+
expect(collection.models.map((model) => model.toJSON())).toEqual([
|
|
76
|
+
{ id: "model3" },
|
|
77
|
+
{ id: "model4" },
|
|
78
|
+
]);
|
|
79
|
+
expect(collection.toJSON()).toEqual([{ id: "model3" }, { id: "model4" }]);
|
|
77
80
|
});
|
|
78
81
|
|
|
79
|
-
it(
|
|
80
|
-
collection = new Collection([new Model({id:
|
|
81
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
82
|
+
it("toJSON returns an data attributes for each model", () => {
|
|
83
|
+
collection = new Collection([new Model({ id: "model1" }), new Model({ id: "model2" })]);
|
|
84
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
|
|
82
85
|
});
|
|
83
86
|
|
|
84
|
-
it(
|
|
85
|
-
collection = new Collection;
|
|
86
|
-
collection.sync(
|
|
87
|
-
expect(sync.default).toHaveBeenCalledWith(
|
|
87
|
+
it("collection.sync proxies the sync module", () => {
|
|
88
|
+
collection = new Collection();
|
|
89
|
+
collection.sync("GET", {});
|
|
90
|
+
expect(sync.default).toHaveBeenCalledWith("GET", {});
|
|
88
91
|
});
|
|
89
92
|
|
|
90
|
-
describe(
|
|
91
|
-
it(
|
|
92
|
-
collection = new Collection([{id:
|
|
93
|
+
describe("add", () => {
|
|
94
|
+
it("appends a model to a collection", () => {
|
|
95
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }]);
|
|
93
96
|
collection.onUpdate(callback);
|
|
94
97
|
|
|
95
|
-
collection.add({id:
|
|
96
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
98
|
+
collection.add({ id: "model3" });
|
|
99
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
97
100
|
expect(callback).toHaveBeenCalled();
|
|
98
101
|
callback.mockClear();
|
|
99
102
|
|
|
100
|
-
collection.add({id:
|
|
101
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
103
|
+
collection.add({ id: "model3" });
|
|
104
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
102
105
|
expect(callback).toHaveBeenCalled();
|
|
103
106
|
});
|
|
104
107
|
});
|
|
105
108
|
|
|
106
|
-
describe(
|
|
107
|
-
it(
|
|
108
|
-
collection = new Collection([{id:
|
|
109
|
-
expect(collection.get(
|
|
109
|
+
describe("remove", () => {
|
|
110
|
+
it("removes a model from a collection", () => {
|
|
111
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
112
|
+
expect(collection.get("model2")).toBeDefined();
|
|
110
113
|
expect(collection.length).toEqual(3);
|
|
111
114
|
|
|
112
|
-
collection.remove({id:
|
|
113
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
115
|
+
collection.remove({ id: "model2" });
|
|
116
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model3" }]);
|
|
114
117
|
expect(collection.length).toEqual(2);
|
|
115
|
-
expect(collection.get(
|
|
118
|
+
expect(collection.get("model2")).not.toBeDefined();
|
|
116
119
|
|
|
117
|
-
collection.remove([{id:
|
|
120
|
+
collection.remove([{ id: "model1" }, { id: "model3" }]);
|
|
118
121
|
expect(collection.toJSON()).toEqual([]);
|
|
119
122
|
expect(collection.length).toEqual(0);
|
|
120
123
|
});
|
|
121
124
|
|
|
122
|
-
it(
|
|
123
|
-
collection = new Collection([{id:
|
|
125
|
+
it("does not trigger an update if the `silent` flag is passed", () => {
|
|
126
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
124
127
|
collection.onUpdate(callback);
|
|
125
128
|
|
|
126
|
-
collection.remove({id:
|
|
129
|
+
collection.remove({ id: "model2" });
|
|
127
130
|
expect(callback).toHaveBeenCalled();
|
|
128
131
|
callback.mockClear();
|
|
129
132
|
|
|
130
|
-
collection.remove({id:
|
|
133
|
+
collection.remove({ id: "model1" }, { silent: true });
|
|
131
134
|
expect(callback).not.toHaveBeenCalled();
|
|
132
135
|
});
|
|
133
136
|
|
|
134
|
-
it(
|
|
135
|
-
collection = new Collection([{id:
|
|
137
|
+
it("does not trigger an update if nothing was removed", () => {
|
|
138
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
136
139
|
collection.onUpdate(callback);
|
|
137
140
|
|
|
138
|
-
collection.remove({id:
|
|
139
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
141
|
+
collection.remove({ id: "model5" });
|
|
142
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
140
143
|
expect(callback).not.toHaveBeenCalled();
|
|
141
144
|
});
|
|
142
145
|
});
|
|
143
146
|
|
|
144
|
-
describe(
|
|
145
|
-
it(
|
|
146
|
-
collection = new Collection([{id:
|
|
147
|
+
describe("reset", () => {
|
|
148
|
+
it("removes all existing models and adds new ones", () => {
|
|
149
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
147
150
|
collection.reset();
|
|
148
151
|
|
|
149
152
|
expect(collection.toJSON()).toEqual([]);
|
|
150
|
-
expect(collection.get(
|
|
151
|
-
expect(collection.get(
|
|
152
|
-
expect(collection.get(
|
|
153
|
+
expect(collection.get("model1")).not.toBeDefined();
|
|
154
|
+
expect(collection.get("model2")).not.toBeDefined();
|
|
155
|
+
expect(collection.get("model3")).not.toBeDefined();
|
|
153
156
|
expect(collection.length).toEqual(0);
|
|
154
157
|
|
|
155
|
-
collection = new Collection([{id:
|
|
156
|
-
collection.reset([{id:
|
|
157
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
158
|
-
expect(collection.get(
|
|
159
|
-
expect(collection.get(
|
|
160
|
-
expect(collection.get(
|
|
158
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
159
|
+
collection.reset([{ id: "model4" }, { id: "model5" }, { id: "model6" }]);
|
|
160
|
+
expect(collection.toJSON()).toEqual([{ id: "model4" }, { id: "model5" }, { id: "model6" }]);
|
|
161
|
+
expect(collection.get("model4")).toBeDefined();
|
|
162
|
+
expect(collection.get("model5")).toBeDefined();
|
|
163
|
+
expect(collection.get("model6")).toBeDefined();
|
|
161
164
|
expect(collection.length).toEqual(3);
|
|
162
165
|
});
|
|
163
166
|
|
|
164
|
-
it(
|
|
165
|
-
collection = new Collection([{id:
|
|
167
|
+
it("does not trigger an update if passed the `silent` flag", () => {
|
|
168
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
166
169
|
collection.onUpdate(callback);
|
|
167
170
|
collection.reset();
|
|
168
171
|
|
|
169
172
|
expect(callback).toHaveBeenCalled();
|
|
170
173
|
callback.mockClear();
|
|
171
174
|
|
|
172
|
-
collection = new Collection([{id:
|
|
175
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
173
176
|
collection.onUpdate(callback);
|
|
174
|
-
collection.reset([], {silent: true});
|
|
177
|
+
collection.reset([], { silent: true });
|
|
175
178
|
expect(collection.toJSON()).toEqual([]);
|
|
176
179
|
expect(callback).not.toHaveBeenCalled();
|
|
177
180
|
});
|
|
178
181
|
});
|
|
179
182
|
|
|
180
|
-
describe(
|
|
183
|
+
describe("set", () => {
|
|
181
184
|
beforeEach(() => {
|
|
182
|
-
collection = new Collection([{id:
|
|
185
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
183
186
|
collection.onUpdate(callback);
|
|
184
187
|
});
|
|
185
188
|
|
|
186
|
-
it(
|
|
187
|
-
collection.set([{id:
|
|
189
|
+
it("adds a list of attribues as models to the collection", () => {
|
|
190
|
+
collection.set([{ id: "model4" }, { id: "model5" }]);
|
|
188
191
|
expect(collection.toJSON()).toEqual([
|
|
189
|
-
{id:
|
|
190
|
-
{id:
|
|
191
|
-
{id:
|
|
192
|
-
{id:
|
|
193
|
-
{id:
|
|
192
|
+
{ id: "model1" },
|
|
193
|
+
{ id: "model2" },
|
|
194
|
+
{ id: "model3" },
|
|
195
|
+
{ id: "model4" },
|
|
196
|
+
{ id: "model5" },
|
|
194
197
|
]);
|
|
195
198
|
});
|
|
196
199
|
|
|
197
|
-
it(
|
|
198
|
-
collection.set({id:
|
|
200
|
+
it("adds a single set of attributes as a model to the collection", () => {
|
|
201
|
+
collection.set({ id: "model4" });
|
|
199
202
|
expect(collection.toJSON()).toEqual([
|
|
200
|
-
{id:
|
|
201
|
-
{id:
|
|
202
|
-
{id:
|
|
203
|
-
{id:
|
|
203
|
+
{ id: "model1" },
|
|
204
|
+
{ id: "model2" },
|
|
205
|
+
{ id: "model3" },
|
|
206
|
+
{ id: "model4" },
|
|
204
207
|
]);
|
|
205
208
|
});
|
|
206
209
|
|
|
207
|
-
it(
|
|
210
|
+
it("parses attributes before setting if `parse` flag is set", () => {
|
|
208
211
|
// here we parse an existing model
|
|
209
|
-
collection.at(2).parse = (attrs) => ({id:
|
|
210
|
-
collection.set({id:
|
|
212
|
+
collection.at(2).parse = (attrs) => ({ id: "model3_parsed" });
|
|
213
|
+
collection.set({ id: "model3" }, { parse: true });
|
|
211
214
|
expect(collection.toJSON()).toEqual([
|
|
212
|
-
{id:
|
|
213
|
-
{id:
|
|
214
|
-
{id:
|
|
215
|
+
{ id: "model1" },
|
|
216
|
+
{ id: "model2" },
|
|
217
|
+
{ id: "model3_parsed" },
|
|
215
218
|
]);
|
|
216
219
|
|
|
217
|
-
collection = new Collection([{id:
|
|
220
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
218
221
|
|
|
219
|
-
collection.parse = (resp) => resp.map((item, i) => ({...item, index: i}));
|
|
220
|
-
collection.set([{id:
|
|
222
|
+
collection.parse = (resp) => resp.map((item, i) => ({ ...item, index: i }));
|
|
223
|
+
collection.set([{ id: "model4" }, { id: "model5" }], { parse: true });
|
|
221
224
|
|
|
222
225
|
expect(collection.toJSON()).toEqual([
|
|
223
|
-
{id:
|
|
224
|
-
{id:
|
|
225
|
-
{id:
|
|
226
|
-
{id:
|
|
227
|
-
{id:
|
|
226
|
+
{ id: "model1" },
|
|
227
|
+
{ id: "model2" },
|
|
228
|
+
{ id: "model3" },
|
|
229
|
+
{ id: "model4", index: 0 },
|
|
230
|
+
{ id: "model5", index: 1 },
|
|
228
231
|
]);
|
|
229
232
|
|
|
230
|
-
collection.parse = (resp) => resp.map((item, i) => ({...item, index: i}));
|
|
231
|
-
collection.set(new Model({id:
|
|
233
|
+
collection.parse = (resp) => resp.map((item, i) => ({ ...item, index: i }));
|
|
234
|
+
collection.set(new Model({ id: "model6" }), { parse: true });
|
|
232
235
|
|
|
233
|
-
// parse does not happen
|
|
236
|
+
// parse does not happen because it's a model instance already
|
|
234
237
|
expect(collection.toJSON()).toEqual([
|
|
235
|
-
{id:
|
|
236
|
-
{id:
|
|
237
|
-
{id:
|
|
238
|
-
{id:
|
|
239
|
-
{id:
|
|
240
|
-
{id:
|
|
241
|
-
]);
|
|
242
|
-
|
|
243
|
-
collection.parse = (resp) => false;
|
|
244
|
-
collection.set({id: 'model7'}, {parse: true});
|
|
245
|
-
|
|
246
|
-
// setting does not happen
|
|
247
|
-
expect(collection.toJSON()).toEqual([
|
|
248
|
-
{id: 'model1'},
|
|
249
|
-
{id: 'model2'},
|
|
250
|
-
{id: 'model3'},
|
|
251
|
-
{id: 'model4', index: 0},
|
|
252
|
-
{id: 'model5', index: 1},
|
|
253
|
-
{id: 'model6'}
|
|
238
|
+
{ id: "model1" },
|
|
239
|
+
{ id: "model2" },
|
|
240
|
+
{ id: "model3" },
|
|
241
|
+
{ id: "model4", index: 0 },
|
|
242
|
+
{ id: "model5", index: 1 },
|
|
243
|
+
{ id: "model6" },
|
|
254
244
|
]);
|
|
255
245
|
});
|
|
256
246
|
|
|
257
|
-
it(
|
|
247
|
+
it("adds existing models as a models on a collection", () => {
|
|
258
248
|
class _Model extends Model {}
|
|
259
249
|
|
|
260
|
-
collection = new Collection([{id:
|
|
250
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
261
251
|
collection.set([
|
|
262
|
-
new _Model({id:
|
|
263
|
-
new _Model({id:
|
|
264
|
-
new _Model({id:
|
|
252
|
+
new _Model({ id: "model4" }),
|
|
253
|
+
new _Model({ id: "model3" }),
|
|
254
|
+
new _Model({ id: "model3" }),
|
|
265
255
|
]);
|
|
266
256
|
|
|
267
257
|
expect(collection.toJSON()).toEqual([
|
|
268
|
-
{id:
|
|
269
|
-
{id:
|
|
270
|
-
{id:
|
|
271
|
-
{id:
|
|
258
|
+
{ id: "model1" },
|
|
259
|
+
{ id: "model2" },
|
|
260
|
+
{ id: "model3" },
|
|
261
|
+
{ id: "model4" },
|
|
272
262
|
]);
|
|
273
263
|
|
|
274
|
-
expect(collection.get(
|
|
275
|
-
expect(collection.get(
|
|
264
|
+
expect(collection.get("model4") instanceof _Model).toBe(true);
|
|
265
|
+
expect(collection.get("model3") instanceof _Model).toBe(false);
|
|
276
266
|
});
|
|
277
267
|
|
|
278
|
-
it(
|
|
268
|
+
it("parses attributes first if the `parse` flag is set", () => {
|
|
279
269
|
class _Model extends Model {
|
|
280
270
|
parse(attrs) {
|
|
281
|
-
return {...attrs, id: `${attrs.id}_parsed`};
|
|
271
|
+
return { ...attrs, id: `${attrs.id}_parsed` };
|
|
282
272
|
}
|
|
283
273
|
}
|
|
284
274
|
|
|
285
|
-
collection = new Collection([], {Model: _Model});
|
|
286
|
-
collection.set([{id:
|
|
275
|
+
collection = new Collection([], { Model: _Model });
|
|
276
|
+
collection.set([{ id: "model1" }, { id: "model2" }, { id: "model3" }], { parse: true });
|
|
287
277
|
expect(collection.toJSON()).toEqual([
|
|
288
|
-
{id:
|
|
289
|
-
{id:
|
|
290
|
-
{id:
|
|
278
|
+
{ id: "model1_parsed" },
|
|
279
|
+
{ id: "model2_parsed" },
|
|
280
|
+
{ id: "model3_parsed" },
|
|
291
281
|
]);
|
|
292
282
|
});
|
|
293
283
|
|
|
294
|
-
it(
|
|
295
|
-
collection.set({id:
|
|
284
|
+
it("merges changes to an existing model", () => {
|
|
285
|
+
collection.set({ id: "model3", name: "noah" });
|
|
296
286
|
|
|
297
287
|
expect(collection.toJSON()).toEqual([
|
|
298
|
-
{id:
|
|
299
|
-
{id:
|
|
300
|
-
{id:
|
|
288
|
+
{ id: "model1" },
|
|
289
|
+
{ id: "model2" },
|
|
290
|
+
{ id: "model3", name: "noah" },
|
|
301
291
|
]);
|
|
302
292
|
});
|
|
303
293
|
|
|
304
|
-
it(
|
|
305
|
-
collection.set({id:
|
|
294
|
+
it("appends a new model if no sorting comparator", () => {
|
|
295
|
+
collection.set({ id: "model0" });
|
|
306
296
|
|
|
307
297
|
expect(collection.toJSON()).toEqual([
|
|
308
|
-
{id:
|
|
309
|
-
{id:
|
|
310
|
-
{id:
|
|
311
|
-
{id:
|
|
298
|
+
{ id: "model1" },
|
|
299
|
+
{ id: "model2" },
|
|
300
|
+
{ id: "model3" },
|
|
301
|
+
{ id: "model0" },
|
|
312
302
|
]);
|
|
313
303
|
});
|
|
314
304
|
|
|
315
|
-
it(
|
|
316
|
-
collection = new Collection(
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
305
|
+
it("re-sorts the collection if a new model is added with a sorting comparator", () => {
|
|
306
|
+
collection = new Collection(
|
|
307
|
+
[
|
|
308
|
+
{ id: "model1", name: "noah" },
|
|
309
|
+
{ id: "model2", name: "zorah" },
|
|
310
|
+
],
|
|
311
|
+
{ comparator: "name" }
|
|
312
|
+
);
|
|
320
313
|
|
|
321
|
-
collection.set({id:
|
|
314
|
+
collection.set({ id: "model3", name: "alex" });
|
|
322
315
|
|
|
323
316
|
expect(collection.toJSON()).toEqual([
|
|
324
|
-
{id:
|
|
325
|
-
{id:
|
|
326
|
-
{id:
|
|
317
|
+
{ id: "model3", name: "alex" },
|
|
318
|
+
{ id: "model1", name: "noah" },
|
|
319
|
+
{ id: "model2", name: "zorah" },
|
|
327
320
|
]);
|
|
328
321
|
});
|
|
329
322
|
|
|
330
|
-
it(
|
|
331
|
-
collection.set([{id:
|
|
323
|
+
it("does not trigger an update if the `silent` flag is passed", () => {
|
|
324
|
+
collection.set([{ id: "model4" }], { silent: true });
|
|
332
325
|
expect(callback).not.toHaveBeenCalled();
|
|
333
326
|
|
|
334
|
-
collection.set([{id:
|
|
327
|
+
collection.set([{ id: "model4" }]);
|
|
335
328
|
expect(callback).toHaveBeenCalled();
|
|
336
329
|
|
|
337
|
-
collection.set([{id:
|
|
330
|
+
collection.set([{ id: "model5" }]);
|
|
338
331
|
expect(callback).toHaveBeenCalledTimes(2);
|
|
339
332
|
});
|
|
340
333
|
|
|
341
|
-
it(
|
|
334
|
+
it("is a noop if no models are passed", () => {
|
|
342
335
|
collection.set();
|
|
343
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
336
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
|
|
344
337
|
});
|
|
345
338
|
});
|
|
346
339
|
|
|
347
|
-
describe(
|
|
348
|
-
var model1 = new Model({id:
|
|
349
|
-
|
|
340
|
+
describe("get", () => {
|
|
341
|
+
var model1 = new Model({ id: "model1" }),
|
|
342
|
+
model2 = new Model({ id: "model2" });
|
|
350
343
|
|
|
351
344
|
beforeEach(() => {
|
|
352
345
|
collection = new Collection([model1, model2]);
|
|
353
346
|
});
|
|
354
347
|
|
|
355
|
-
it(
|
|
348
|
+
it("returns undefined if a falsey, non-number is passed", () => {
|
|
356
349
|
expect(collection.get()).not.toBeDefined();
|
|
357
350
|
expect(collection.get(null)).not.toBeDefined();
|
|
358
|
-
collection.set({id: 0});
|
|
351
|
+
collection.set({ id: 0 });
|
|
359
352
|
expect(collection.get(0)).toBeDefined();
|
|
360
353
|
});
|
|
361
354
|
|
|
362
|
-
it(
|
|
363
|
-
expect(collection.get(
|
|
355
|
+
it("returns a model at its client id, its id, or by passing those attributes", () => {
|
|
356
|
+
expect(collection.get("model1")).toEqual(model1);
|
|
364
357
|
expect(collection.get(model1.cid)).toEqual(model1);
|
|
365
358
|
expect(collection.get(model1)).toEqual(model1);
|
|
366
359
|
expect(collection.get(model1.toJSON())).toEqual(model1);
|
|
367
360
|
});
|
|
368
361
|
});
|
|
369
362
|
|
|
370
|
-
describe(
|
|
371
|
-
it(
|
|
372
|
-
collection = new Collection([{id:
|
|
373
|
-
expect(collection.has(
|
|
374
|
-
expect(collection.has({id:
|
|
375
|
-
expect(collection.has(
|
|
363
|
+
describe("has", () => {
|
|
364
|
+
it("returns true if a model exists for a given id", () => {
|
|
365
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }]);
|
|
366
|
+
expect(collection.has("model1")).toBe(true);
|
|
367
|
+
expect(collection.has({ id: "model1" })).toBe(true);
|
|
368
|
+
expect(collection.has("model3")).toBe(false);
|
|
376
369
|
expect(collection.has()).toBe(false);
|
|
377
370
|
});
|
|
378
371
|
});
|
|
379
372
|
|
|
380
|
-
describe(
|
|
381
|
-
var model1 = new Model({id:
|
|
382
|
-
|
|
383
|
-
|
|
373
|
+
describe("helper methods", () => {
|
|
374
|
+
var model1 = new Model({ id: "model1" }),
|
|
375
|
+
model2 = new Model({ id: "model2" }),
|
|
376
|
+
model3 = new Model({ id: "model3" });
|
|
384
377
|
|
|
385
378
|
beforeEach(() => {
|
|
386
379
|
collection = new Collection([model1, model2, model3]);
|
|
387
380
|
});
|
|
388
381
|
|
|
389
|
-
describe(
|
|
390
|
-
it(
|
|
382
|
+
describe("at", () => {
|
|
383
|
+
it("returns the model at a specific index", () => {
|
|
391
384
|
expect(collection.at(0)).toEqual(model1);
|
|
392
385
|
expect(collection.at(1)).toEqual(model2);
|
|
393
386
|
expect(collection.at(2)).toEqual(model3);
|
|
394
387
|
expect(collection.at(3)).not.toBeDefined();
|
|
395
388
|
});
|
|
396
389
|
|
|
397
|
-
it(
|
|
390
|
+
it("returns the model at a specific index from the end for a negative index", () => {
|
|
398
391
|
expect(collection.at(-1)).toEqual(model3);
|
|
399
392
|
expect(collection.at(-2)).toEqual(model2);
|
|
400
393
|
expect(collection.at(-3)).toEqual(model1);
|
|
@@ -402,131 +395,128 @@ describe('Collection', () => {
|
|
|
402
395
|
});
|
|
403
396
|
});
|
|
404
397
|
|
|
405
|
-
describe(
|
|
406
|
-
it(
|
|
407
|
-
collection.map((model, i) => model.set({index: i}));
|
|
398
|
+
describe("map", () => {
|
|
399
|
+
it("runs a map function across the models", () => {
|
|
400
|
+
collection.map((model, i) => model.set({ index: i }));
|
|
408
401
|
expect(collection.toJSON()).toEqual([
|
|
409
|
-
{id:
|
|
410
|
-
{id:
|
|
411
|
-
{id:
|
|
402
|
+
{ id: "model1", index: 0 },
|
|
403
|
+
{ id: "model2", index: 1 },
|
|
404
|
+
{ id: "model3", index: 2 },
|
|
412
405
|
]);
|
|
413
406
|
});
|
|
414
407
|
});
|
|
415
408
|
|
|
416
|
-
describe(
|
|
417
|
-
it(
|
|
409
|
+
describe("find", () => {
|
|
410
|
+
it("finds the first model that passes the predicate", () => {
|
|
418
411
|
expect(collection.find((model, i) => i === 1)).toEqual(model2);
|
|
419
412
|
expect(collection.find((model, i) => /model/.test(model.id))).toEqual(model1);
|
|
420
413
|
});
|
|
421
414
|
});
|
|
422
415
|
|
|
423
|
-
describe(
|
|
424
|
-
it(
|
|
416
|
+
describe("filter", () => {
|
|
417
|
+
it("returns all models that pass the predicate", () => {
|
|
425
418
|
expect(collection.filter((model, i) => i > 0)).toEqual([model2, model3]);
|
|
426
419
|
});
|
|
427
420
|
});
|
|
428
421
|
|
|
429
|
-
describe(
|
|
430
|
-
it(
|
|
431
|
-
expect(collection.findWhere({id:
|
|
422
|
+
describe("findWhere", () => {
|
|
423
|
+
it("runs a find with shorthand", () => {
|
|
424
|
+
expect(collection.findWhere({ id: "model2" })).toEqual(model2);
|
|
432
425
|
});
|
|
433
426
|
});
|
|
434
427
|
|
|
435
|
-
describe(
|
|
436
|
-
it(
|
|
437
|
-
expect(collection.where({id:
|
|
438
|
-
expect(collection.where({id:
|
|
428
|
+
describe("where", () => {
|
|
429
|
+
it("runs filter with shorthand", () => {
|
|
430
|
+
expect(collection.where({ id: "model2" })).toEqual([model2]);
|
|
431
|
+
expect(collection.where({ id: "model4" })).toEqual([]);
|
|
439
432
|
});
|
|
440
433
|
});
|
|
441
434
|
|
|
442
|
-
describe(
|
|
443
|
-
it(
|
|
444
|
-
expect(collection.pluck(
|
|
435
|
+
describe("pluck", () => {
|
|
436
|
+
it("picks out a property from all models", () => {
|
|
437
|
+
expect(collection.pluck("id")).toEqual(["model1", "model2", "model3"]);
|
|
445
438
|
});
|
|
446
439
|
});
|
|
447
440
|
|
|
448
|
-
describe(
|
|
449
|
-
it(
|
|
441
|
+
describe("slice", () => {
|
|
442
|
+
it("returns a subarray of models", () => {
|
|
450
443
|
expect(collection.slice(-2)).toEqual([model2, model3]);
|
|
451
444
|
expect(collection.slice(0, 1)).toEqual([model1]);
|
|
452
445
|
});
|
|
453
446
|
});
|
|
454
447
|
|
|
455
|
-
describe(
|
|
448
|
+
describe("sort", () => {
|
|
456
449
|
beforeEach(() => {
|
|
457
450
|
collection = new Collection([
|
|
458
|
-
{id:
|
|
459
|
-
{id:
|
|
460
|
-
{id:
|
|
451
|
+
{ id: "model1", name: "noah" },
|
|
452
|
+
{ id: "model2", name: "zorah" },
|
|
453
|
+
{ id: "model3", name: "alex" },
|
|
461
454
|
]);
|
|
462
455
|
});
|
|
463
456
|
|
|
464
|
-
it(
|
|
465
|
-
expect(() => collection.sort()).toThrowError(
|
|
457
|
+
it("throws if no comparator exists on a collection", () => {
|
|
458
|
+
expect(() => collection.sort()).toThrowError("Cannot sort a set without a comparator");
|
|
466
459
|
});
|
|
467
460
|
|
|
468
|
-
it(
|
|
469
|
-
collection.comparator =
|
|
461
|
+
it("can sort by property", () => {
|
|
462
|
+
collection.comparator = "name";
|
|
470
463
|
collection.sort();
|
|
471
464
|
|
|
472
465
|
expect(collection.toJSON()).toEqual([
|
|
473
|
-
{id:
|
|
474
|
-
{id:
|
|
475
|
-
{id:
|
|
466
|
+
{ id: "model3", name: "alex" },
|
|
467
|
+
{ id: "model1", name: "noah" },
|
|
468
|
+
{ id: "model2", name: "zorah" },
|
|
476
469
|
]);
|
|
477
470
|
});
|
|
478
471
|
|
|
479
|
-
it(
|
|
480
|
-
collection.comparator = (model) => model.get(
|
|
472
|
+
it("can sort by function with one argument", () => {
|
|
473
|
+
collection.comparator = (model) => model.get("name");
|
|
481
474
|
collection.sort();
|
|
482
475
|
|
|
483
476
|
expect(collection.toJSON()).toEqual([
|
|
484
|
-
{id:
|
|
485
|
-
{id:
|
|
486
|
-
{id:
|
|
477
|
+
{ id: "model3", name: "alex" },
|
|
478
|
+
{ id: "model1", name: "noah" },
|
|
479
|
+
{ id: "model2", name: "zorah" },
|
|
487
480
|
]);
|
|
488
481
|
});
|
|
489
482
|
|
|
490
|
-
it(
|
|
491
|
-
collection.comparator = (left, right) => left.get(
|
|
483
|
+
it("can sort by function with two arguments", () => {
|
|
484
|
+
collection.comparator = (left, right) => (left.get("name") > right.get("name ") ? -1 : 1);
|
|
492
485
|
collection.sort();
|
|
493
486
|
|
|
494
487
|
expect(collection.toJSON()).toEqual([
|
|
495
|
-
{id:
|
|
496
|
-
{id:
|
|
497
|
-
{id:
|
|
488
|
+
{ id: "model1", name: "noah" },
|
|
489
|
+
{ id: "model2", name: "zorah" },
|
|
490
|
+
{ id: "model3", name: "alex" },
|
|
498
491
|
]);
|
|
499
492
|
});
|
|
500
493
|
});
|
|
501
494
|
});
|
|
502
495
|
|
|
503
|
-
describe(
|
|
496
|
+
describe("fetch", () => {
|
|
504
497
|
var response = {};
|
|
505
498
|
|
|
506
499
|
beforeEach(() => {
|
|
507
|
-
collection = new Collection;
|
|
508
|
-
collection.parse = (resp) => resp.map((item, i) => ({...item, index: i}));
|
|
500
|
+
collection = new Collection();
|
|
501
|
+
collection.parse = (resp) => resp.map((item, i) => ({ ...item, index: i }));
|
|
509
502
|
|
|
510
|
-
sync.default.mockResolvedValue([
|
|
511
|
-
[{id: 'one'}, {id: 'two'}],
|
|
512
|
-
response
|
|
513
|
-
]);
|
|
503
|
+
sync.default.mockResolvedValue([[{ id: "one" }, { id: "two" }], response]);
|
|
514
504
|
});
|
|
515
505
|
|
|
516
|
-
it(
|
|
506
|
+
it("calls sync with a GET method", async () => {
|
|
517
507
|
await collection.fetch();
|
|
518
508
|
|
|
519
|
-
expect(sync.default).toHaveBeenCalledWith(collection, {method:
|
|
520
|
-
await collection.fetch({method:
|
|
509
|
+
expect(sync.default).toHaveBeenCalledWith(collection, { method: "GET", parse: true });
|
|
510
|
+
await collection.fetch({ method: "POST", url: "/books" });
|
|
521
511
|
|
|
522
512
|
expect(sync.default).toHaveBeenCalledWith(collection, {
|
|
523
|
-
method:
|
|
513
|
+
method: "POST",
|
|
524
514
|
parse: true,
|
|
525
|
-
url:
|
|
515
|
+
url: "/books",
|
|
526
516
|
});
|
|
527
517
|
});
|
|
528
518
|
|
|
529
|
-
it(
|
|
519
|
+
it("triggers an update on returning", async () => {
|
|
530
520
|
var request;
|
|
531
521
|
|
|
532
522
|
collection.onUpdate(callback);
|
|
@@ -535,20 +525,23 @@ describe('Collection', () => {
|
|
|
535
525
|
|
|
536
526
|
await request;
|
|
537
527
|
expect(callback).toHaveBeenCalled();
|
|
538
|
-
expect(collection.toJSON()).toEqual([
|
|
528
|
+
expect(collection.toJSON()).toEqual([
|
|
529
|
+
{ id: "one", index: 0 },
|
|
530
|
+
{ id: "two", index: 1 },
|
|
531
|
+
]);
|
|
539
532
|
collection.reset();
|
|
540
533
|
callback.mockClear();
|
|
541
534
|
|
|
542
|
-
await collection.fetch({parse: false});
|
|
535
|
+
await collection.fetch({ parse: false });
|
|
543
536
|
expect(callback).toHaveBeenCalled();
|
|
544
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
537
|
+
expect(collection.toJSON()).toEqual([{ id: "one" }, { id: "two" }]);
|
|
545
538
|
});
|
|
546
539
|
|
|
547
|
-
it(
|
|
540
|
+
it("resolves a tuple of the collection instance and repsonse object", async () => {
|
|
548
541
|
expect(await collection.fetch()).toEqual([collection, response]);
|
|
549
542
|
});
|
|
550
543
|
|
|
551
|
-
it(
|
|
544
|
+
it("rejects the response", async () => {
|
|
552
545
|
sync.default.mockRejectedValue(response);
|
|
553
546
|
|
|
554
547
|
try {
|
|
@@ -559,132 +552,128 @@ describe('Collection', () => {
|
|
|
559
552
|
});
|
|
560
553
|
});
|
|
561
554
|
|
|
562
|
-
describe(
|
|
555
|
+
describe("create", () => {
|
|
563
556
|
var response = {};
|
|
564
557
|
|
|
565
558
|
beforeEach(() => {
|
|
566
|
-
collection = new Collection([{id:
|
|
567
|
-
sync.default.mockResolvedValue([{id:
|
|
559
|
+
collection = new Collection([{ id: "model1" }, { id: "model2" }]);
|
|
560
|
+
sync.default.mockResolvedValue([{ id: "model3" }, response]);
|
|
568
561
|
});
|
|
569
562
|
|
|
570
|
-
it(
|
|
571
|
-
var request = collection.create({name:
|
|
563
|
+
it("combines adding model to the collection and saving the model to the server", async () => {
|
|
564
|
+
var request = collection.create({ name: "model3" });
|
|
572
565
|
|
|
573
|
-
expect(collection.toJSON()).toEqual([
|
|
574
|
-
{id: 'model1'},
|
|
575
|
-
{id: 'model2'},
|
|
576
|
-
{name: 'model3'}
|
|
577
|
-
]);
|
|
566
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }, { name: "model3" }]);
|
|
578
567
|
|
|
579
568
|
await request;
|
|
580
569
|
expect(collection.toJSON()).toEqual([
|
|
581
|
-
{id:
|
|
582
|
-
{id:
|
|
583
|
-
{id:
|
|
570
|
+
{ id: "model1" },
|
|
571
|
+
{ id: "model2" },
|
|
572
|
+
{ id: "model3", name: "model3" },
|
|
584
573
|
]);
|
|
585
574
|
});
|
|
586
575
|
|
|
587
|
-
it(
|
|
588
|
-
var request = collection.create({name:
|
|
576
|
+
it("if `wait` is true, does not add the model until the request returns", async () => {
|
|
577
|
+
var request = collection.create({ name: "model3" }, { wait: true });
|
|
589
578
|
|
|
590
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
579
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
|
|
591
580
|
|
|
592
581
|
await request;
|
|
593
582
|
expect(collection.toJSON()).toEqual([
|
|
594
|
-
{id:
|
|
595
|
-
{id:
|
|
596
|
-
{id:
|
|
583
|
+
{ id: "model1" },
|
|
584
|
+
{ id: "model2" },
|
|
585
|
+
{ id: "model3", name: "model3" },
|
|
597
586
|
]);
|
|
598
587
|
});
|
|
599
588
|
|
|
600
|
-
it(
|
|
601
|
-
expect(await collection.create({name:
|
|
602
|
-
collection.get(
|
|
603
|
-
response
|
|
589
|
+
it("resolves a tuple of the model instance and repsonse object", async () => {
|
|
590
|
+
expect(await collection.create({ name: "model3" })).toEqual([
|
|
591
|
+
collection.get("model3"),
|
|
592
|
+
response,
|
|
604
593
|
]);
|
|
605
594
|
});
|
|
606
595
|
|
|
607
|
-
it(
|
|
596
|
+
it("rejects the response and removes the model from the collection if added", async () => {
|
|
608
597
|
sync.default.mockRejectedValue(response);
|
|
609
598
|
|
|
610
599
|
try {
|
|
611
|
-
await collection.create({name:
|
|
600
|
+
await collection.create({ name: "model3" });
|
|
612
601
|
} catch (err) {
|
|
613
602
|
expect(err).toEqual(response);
|
|
614
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
603
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
|
|
615
604
|
}
|
|
616
605
|
|
|
617
606
|
try {
|
|
618
|
-
await collection.create({name:
|
|
607
|
+
await collection.create({ name: "model3" }, { wait: true });
|
|
619
608
|
} catch (err) {
|
|
620
609
|
expect(err).toEqual(response);
|
|
621
|
-
expect(collection.toJSON()).toEqual([{id:
|
|
610
|
+
expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
|
|
622
611
|
}
|
|
623
612
|
});
|
|
624
613
|
});
|
|
625
614
|
|
|
626
|
-
describe(
|
|
627
|
-
it(
|
|
615
|
+
describe("parse", () => {
|
|
616
|
+
it("is by default the identity function", () => {
|
|
628
617
|
collection = new Collection();
|
|
629
|
-
expect(collection.parse({foo:
|
|
618
|
+
expect(collection.parse({ foo: "bar" })).toEqual({ foo: "bar" });
|
|
630
619
|
});
|
|
631
620
|
});
|
|
632
621
|
|
|
633
|
-
describe(
|
|
634
|
-
it(
|
|
622
|
+
describe("setting a model id attribute", () => {
|
|
623
|
+
it("on the model sets the id attributes for all models", () => {
|
|
635
624
|
class _Model extends Model {
|
|
636
|
-
static idAttribute =
|
|
625
|
+
static idAttribute = "name";
|
|
637
626
|
}
|
|
638
627
|
|
|
639
628
|
// default is just id
|
|
640
629
|
collection = new Collection();
|
|
641
|
-
collection.add({id:
|
|
642
|
-
expect(collection.get(
|
|
643
|
-
expect(collection.get(
|
|
644
|
-
expect(collection.get(
|
|
645
|
-
expect(collection.Model.idAttribute).toEqual(
|
|
630
|
+
collection.add({ id: "noahgrant", name: "Noah Grant" });
|
|
631
|
+
expect(collection.get("noahgrant")).toBeDefined();
|
|
632
|
+
expect(collection.get("Noah Grant")).not.toBeDefined();
|
|
633
|
+
expect(collection.get("noahgrant").id).toEqual("noahgrant");
|
|
634
|
+
expect(collection.Model.idAttribute).toEqual("id");
|
|
646
635
|
|
|
647
|
-
collection = new Collection([], {Model: _Model});
|
|
648
|
-
collection.add({id:
|
|
649
|
-
expect(collection.get(
|
|
650
|
-
expect(collection.get(
|
|
651
|
-
expect(collection.get(
|
|
652
|
-
expect(collection.Model.idAttribute).toEqual(
|
|
636
|
+
collection = new Collection([], { Model: _Model });
|
|
637
|
+
collection.add({ id: "noahgrant", name: "Noah Grant" });
|
|
638
|
+
expect(collection.get("noahgrant")).not.toBeDefined();
|
|
639
|
+
expect(collection.get("Noah Grant")).toBeDefined();
|
|
640
|
+
expect(collection.get("Noah Grant").id).toEqual("Noah Grant");
|
|
641
|
+
expect(collection.Model.idAttribute).toEqual("name");
|
|
653
642
|
});
|
|
654
643
|
|
|
655
|
-
it(
|
|
644
|
+
it("on the collection sets the id attribute for its models", () => {
|
|
656
645
|
class _Collection extends Collection {
|
|
657
|
-
static
|
|
646
|
+
static idAttribute = "name";
|
|
658
647
|
}
|
|
659
648
|
|
|
660
649
|
collection = new _Collection();
|
|
661
|
-
collection.add({id:
|
|
662
|
-
expect(collection.get(
|
|
663
|
-
expect(collection.get(
|
|
664
|
-
expect(collection.get(
|
|
665
|
-
expect(collection.Model.idAttribute).toEqual(
|
|
650
|
+
collection.add({ id: "noahgrant", name: "Noah Grant" });
|
|
651
|
+
expect(collection.get("noahgrant")).not.toBeDefined();
|
|
652
|
+
expect(collection.get("Noah Grant")).toBeDefined();
|
|
653
|
+
expect(collection.get("Noah Grant").id).toEqual("Noah Grant");
|
|
654
|
+
expect(collection.Model.idAttribute).toEqual("name");
|
|
666
655
|
});
|
|
667
656
|
});
|
|
668
657
|
|
|
669
|
-
describe(
|
|
670
|
-
it(
|
|
671
|
-
var model = new Model({id:
|
|
658
|
+
describe("_updateModelReference", () => {
|
|
659
|
+
it("updates the reference to a model when its id changes", () => {
|
|
660
|
+
var model = new Model({ id: "model1" });
|
|
672
661
|
|
|
673
662
|
collection = new Collection(model);
|
|
674
663
|
expect(model.collection).toEqual(collection);
|
|
675
|
-
expect(collection.get(
|
|
664
|
+
expect(collection.get("model1")).toEqual(model);
|
|
676
665
|
|
|
677
|
-
model.set({id:
|
|
678
|
-
expect(collection.get(
|
|
679
|
-
expect(collection.get(
|
|
666
|
+
model.set({ id: "1model" });
|
|
667
|
+
expect(collection.get("model1")).not.toBeDefined();
|
|
668
|
+
expect(collection.get("1model")).toEqual(model);
|
|
680
669
|
});
|
|
681
670
|
|
|
682
|
-
it(
|
|
683
|
-
var model = new Model({id:
|
|
671
|
+
it("does not add a reference if the id does not exist", () => {
|
|
672
|
+
var model = new Model({ id: "model1" });
|
|
684
673
|
|
|
685
674
|
collection = new Collection(model);
|
|
686
|
-
collection._updateModelReference(null,
|
|
687
|
-
expect(collection.get(
|
|
675
|
+
collection._updateModelReference(null, "model1", model);
|
|
676
|
+
expect(collection.get("model1")).toEqual(model);
|
|
688
677
|
expect(collection.get(null)).not.toBeDefined();
|
|
689
678
|
});
|
|
690
679
|
});
|