resourcerer 1.2.3 → 2.0.0-beta-1

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.
Files changed (59) hide show
  1. package/README.md +133 -164
  2. package/build/index.d.ts +9 -0
  3. package/build/index.js +9 -0
  4. package/build/lib/collection.d.ts +107 -0
  5. package/build/lib/collection.js +251 -0
  6. package/build/lib/config.d.ts +20 -0
  7. package/build/lib/config.js +23 -0
  8. package/build/lib/error-boundary.d.ts +12 -0
  9. package/build/lib/error-boundary.js +18 -0
  10. package/build/lib/events.d.ts +11 -0
  11. package/build/lib/events.js +12 -0
  12. package/build/lib/model-cache.d.ts +13 -0
  13. package/build/lib/model-cache.js +62 -0
  14. package/build/lib/model.d.ts +48 -0
  15. package/build/lib/model.js +170 -0
  16. package/build/lib/prefetch.d.ts +3 -0
  17. package/build/lib/prefetch.js +27 -0
  18. package/build/lib/request.d.ts +20 -0
  19. package/build/lib/request.js +53 -0
  20. package/build/lib/resourcerer.d.ts +13 -0
  21. package/build/lib/resourcerer.js +406 -0
  22. package/build/lib/sync.d.ts +18 -0
  23. package/build/lib/sync.js +58 -0
  24. package/build/lib/types.d.ts +66 -0
  25. package/build/lib/types.js +1 -0
  26. package/build/lib/utils.d.ts +14 -0
  27. package/build/lib/utils.js +87 -0
  28. package/docs/advanced_topics.md +9 -77
  29. package/docs/collection.md +28 -28
  30. package/docs/migrating_to_2.0.md +72 -0
  31. package/docs/model.md +21 -21
  32. package/docs/typescript.md +72 -22
  33. package/package.json +27 -30
  34. package/resourcerer.d.ts +58 -0
  35. package/test/collection.test.js +305 -316
  36. package/test/config.test.js +21 -75
  37. package/test/events.test.js +42 -41
  38. package/test/model-cache.test.js +99 -77
  39. package/test/model.test.js +204 -209
  40. package/test/prefetch.test.js +35 -61
  41. package/test/request.test.js +104 -109
  42. package/test/sync.test.js +122 -109
  43. package/test/utils.test.js +173 -159
  44. package/index.d.ts +0 -270
  45. package/index.js +0 -14
  46. package/lib/collection.js +0 -584
  47. package/lib/config.js +0 -131
  48. package/lib/constants.js +0 -6
  49. package/lib/error-boundary.js +0 -47
  50. package/lib/events.js +0 -19
  51. package/lib/model-cache.js +0 -131
  52. package/lib/model.js +0 -372
  53. package/lib/prefetch.js +0 -59
  54. package/lib/request.js +0 -126
  55. package/lib/resourcerer.js +0 -985
  56. package/lib/sync.js +0 -111
  57. package/lib/utils.js +0 -220
  58. package/test/model-mocks.js +0 -110
  59. package/test/setup.js +0 -63
@@ -1,13 +1,14 @@
1
- import * as sync from '../lib/sync';
2
- import Collection from '../lib/collection';
3
- import Model from '../lib/model';
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('Collection', () => {
6
+ describe("Collection", () => {
6
7
  var collection,
7
- callback = jest.fn();
8
+ callback = vi.fn();
8
9
 
9
10
  beforeEach(() => {
10
- jest.spyOn(sync, 'default').mockResolvedValue([]);
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('is instantiated with appropriate internal properties', () => {
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('can override class properties via the options hash', () => {
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 = 'name'
35
+ static comparator = "name";
35
36
  }
36
37
 
37
38
  collection = new _Collection();
38
- expect(collection.comparator).toEqual('name');
39
+ expect(collection.comparator).toEqual("name");
39
40
  expect(collection.Model).toEqual(_Model);
40
41
 
41
- collection = new _Collection([], {Model: __Model, comparator: 'email'});
42
- expect(collection.comparator).toEqual('email');
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('unreserved option items are assigned to a `urlOptions` instance property', () => {
47
- collection = new Collection([], {one: 1, two: 2, comparator: 'foo', parse: true});
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('urlOptions get passed down to models', async() => {
54
- collection = new Collection([{id: '1'}], {one: 1, two: 2, comparator: 'foo', parse: true});
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('1').urlOptions).toEqual({one: 1, two: 2});
57
+ expect(collection.get("1").urlOptions).toEqual({ one: 1, two: 2 });
57
58
 
58
- collection.add({id: '2'});
59
- expect(collection.get('2').urlOptions).toEqual({one: 1, two: 2});
59
+ collection.add({ id: "2" });
60
+ expect(collection.get("2").urlOptions).toEqual({ one: 1, two: 2 });
60
61
 
61
- sync.default.mockResolvedValue([[{id: '3'}], {}]);
62
+ sync.default.mockResolvedValue([[{ id: "3" }], {}]);
62
63
  await collection.fetch();
63
- expect(collection.get('3').urlOptions).toEqual({one: 1, two: 2});
64
+ expect(collection.get("3").urlOptions).toEqual({ one: 1, two: 2 });
64
65
  });
65
66
 
66
- it('adds any models passed to its models list', () => {
67
- var model1 = new Model,
68
- model2 = new Model;
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: 'model3'}, {id: 'model4'}]);
74
- expect(collection.models.map((model) => model.toJSON()))
75
- .toEqual([{id: 'model3'}, {id: 'model4'}]);
76
- expect(collection.toJSON()).toEqual([{id: 'model3'}, {id: 'model4'}]);
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('toJSON returns an data attributes for each model', () => {
80
- collection = new Collection([new Model({id: 'model1'}), new Model({id: 'model2'})]);
81
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}]);
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('collection.sync proxies the sync module', () => {
85
- collection = new Collection;
86
- collection.sync('GET', {});
87
- expect(sync.default).toHaveBeenCalledWith('GET', {});
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('add', () => {
91
- it('appends a model to a collection', () => {
92
- collection = new Collection([{id: 'model1'}, {id: 'model2'}]);
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: 'model3'});
96
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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: 'model3'});
101
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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('remove', () => {
107
- it('removes a model from a collection', () => {
108
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
109
- expect(collection.get('model2')).toBeDefined();
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: 'model2'});
113
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model3'}]);
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('model2')).not.toBeDefined();
118
+ expect(collection.get("model2")).not.toBeDefined();
116
119
 
117
- collection.remove([{id: 'model1'}, {id: 'model3'}]);
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('does not trigger an update if the `silent` flag is passed', () => {
123
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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: 'model2'});
129
+ collection.remove({ id: "model2" });
127
130
  expect(callback).toHaveBeenCalled();
128
131
  callback.mockClear();
129
132
 
130
- collection.remove({id: 'model1'}, {silent: true});
133
+ collection.remove({ id: "model1" }, { silent: true });
131
134
  expect(callback).not.toHaveBeenCalled();
132
135
  });
133
136
 
134
- it('does not trigger an update if nothing was removed', () => {
135
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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: 'model5'});
139
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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('reset', () => {
145
- it('removes all existing models and adds new ones', () => {
146
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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('model1')).not.toBeDefined();
151
- expect(collection.get('model2')).not.toBeDefined();
152
- expect(collection.get('model3')).not.toBeDefined();
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: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
156
- collection.reset([{id: 'model4'}, {id: 'model5'}, {id: 'model6'}]);
157
- expect(collection.toJSON()).toEqual([{id: 'model4'}, {id: 'model5'}, {id: 'model6'}]);
158
- expect(collection.get('model4')).toBeDefined();
159
- expect(collection.get('model5')).toBeDefined();
160
- expect(collection.get('model6')).toBeDefined();
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('does not trigger an update if passed the `silent` flag', () => {
165
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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('set', () => {
183
+ describe("set", () => {
181
184
  beforeEach(() => {
182
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
185
+ collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
183
186
  collection.onUpdate(callback);
184
187
  });
185
188
 
186
- it('adds a list of attribues as models to the collection', () => {
187
- collection.set([{id: 'model4'}, {id: 'model5'}]);
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: 'model1'},
190
- {id: 'model2'},
191
- {id: 'model3'},
192
- {id: 'model4'},
193
- {id: 'model5'}
192
+ { id: "model1" },
193
+ { id: "model2" },
194
+ { id: "model3" },
195
+ { id: "model4" },
196
+ { id: "model5" },
194
197
  ]);
195
198
  });
196
199
 
197
- it('adds a single set of attributes as a model to the collection', () => {
198
- collection.set({id: 'model4'});
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: 'model1'},
201
- {id: 'model2'},
202
- {id: 'model3'},
203
- {id: 'model4'}
203
+ { id: "model1" },
204
+ { id: "model2" },
205
+ { id: "model3" },
206
+ { id: "model4" },
204
207
  ]);
205
208
  });
206
209
 
207
- it('parses attributes before setting if `parse` flag is set', () => {
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: 'model3_parsed'});
210
- collection.set({id: 'model3'}, {parse: true});
212
+ collection.at(2).parse = (attrs) => ({ id: "model3_parsed" });
213
+ collection.set({ id: "model3" }, { parse: true });
211
214
  expect(collection.toJSON()).toEqual([
212
- {id: 'model1'},
213
- {id: 'model2'},
214
- {id: 'model3_parsed'}
215
+ { id: "model1" },
216
+ { id: "model2" },
217
+ { id: "model3_parsed" },
215
218
  ]);
216
219
 
217
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
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: 'model4'}, {id: 'model5'}], {parse: true});
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: 'model1'},
224
- {id: 'model2'},
225
- {id: 'model3'},
226
- {id: 'model4', index: 0},
227
- {id: 'model5', index: 1}
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: 'model6'}), {parse: true});
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: 'model1'},
236
- {id: 'model2'},
237
- {id: 'model3'},
238
- {id: 'model4', index: 0},
239
- {id: 'model5', index: 1},
240
- {id: 'model6'}
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('adds existing models as a models on a collection', () => {
247
+ it("adds existing models as a models on a collection", () => {
258
248
  class _Model extends Model {}
259
249
 
260
- collection = new Collection([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
250
+ collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
261
251
  collection.set([
262
- new _Model({id: 'model4'}),
263
- new _Model({id: 'model3'}),
264
- new _Model({id: 'model3'})
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: 'model1'},
269
- {id: 'model2'},
270
- {id: 'model3'},
271
- {id: 'model4'}
258
+ { id: "model1" },
259
+ { id: "model2" },
260
+ { id: "model3" },
261
+ { id: "model4" },
272
262
  ]);
273
263
 
274
- expect(collection.get('model4') instanceof _Model).toBe(true);
275
- expect(collection.get('model3') instanceof _Model).toBe(false);
264
+ expect(collection.get("model4") instanceof _Model).toBe(true);
265
+ expect(collection.get("model3") instanceof _Model).toBe(false);
276
266
  });
277
267
 
278
- it('parses attributes first if the `parse` flag is set', () => {
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: 'model1'}, {id: 'model2'}, {id: 'model3'}], {parse: true});
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: 'model1_parsed'},
289
- {id: 'model2_parsed'},
290
- {id: 'model3_parsed'}
278
+ { id: "model1_parsed" },
279
+ { id: "model2_parsed" },
280
+ { id: "model3_parsed" },
291
281
  ]);
292
282
  });
293
283
 
294
- it('merges changes to an existing model', () => {
295
- collection.set({id: 'model3', name: 'noah'});
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: 'model1'},
299
- {id: 'model2'},
300
- {id: 'model3', name: 'noah'}
288
+ { id: "model1" },
289
+ { id: "model2" },
290
+ { id: "model3", name: "noah" },
301
291
  ]);
302
292
  });
303
293
 
304
- it('appends a new model if no sorting comparator', () => {
305
- collection.set({id: 'model0'});
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: 'model1'},
309
- {id: 'model2'},
310
- {id: 'model3'},
311
- {id: 'model0'}
298
+ { id: "model1" },
299
+ { id: "model2" },
300
+ { id: "model3" },
301
+ { id: "model0" },
312
302
  ]);
313
303
  });
314
304
 
315
- it('re-sorts the collection if a new model is added with a sorting comparator', () => {
316
- collection = new Collection([
317
- {id: 'model1', name: 'noah'},
318
- {id: 'model2', name: 'zorah'}
319
- ], {comparator: 'name'});
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: 'model3', name: 'alex'});
314
+ collection.set({ id: "model3", name: "alex" });
322
315
 
323
316
  expect(collection.toJSON()).toEqual([
324
- {id: 'model3', name: 'alex'},
325
- {id: 'model1', name: 'noah'},
326
- {id: 'model2', name: 'zorah'}
317
+ { id: "model3", name: "alex" },
318
+ { id: "model1", name: "noah" },
319
+ { id: "model2", name: "zorah" },
327
320
  ]);
328
321
  });
329
322
 
330
- it('does not trigger an update if the `silent` flag is passed', () => {
331
- collection.set([{id: 'model4'}], {silent: true});
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: 'model4'}]);
327
+ collection.set([{ id: "model4" }]);
335
328
  expect(callback).toHaveBeenCalled();
336
329
 
337
- collection.set([{id: 'model5'}]);
330
+ collection.set([{ id: "model5" }]);
338
331
  expect(callback).toHaveBeenCalledTimes(2);
339
332
  });
340
333
 
341
- it('is a noop if no models are passed', () => {
334
+ it("is a noop if no models are passed", () => {
342
335
  collection.set();
343
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}, {id: 'model3'}]);
336
+ expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
344
337
  });
345
338
  });
346
339
 
347
- describe('get', () => {
348
- var model1 = new Model({id: 'model1'}),
349
- model2 = new Model({id: 'model2'});
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('returns undefined if a falsey, non-number is passed', () => {
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('returns a model at its client id, its id, or by passing those attributes', () => {
363
- expect(collection.get('model1')).toEqual(model1);
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('has', () => {
371
- it('returns true if a model exists for a given id', () => {
372
- collection = new Collection([{id: 'model1'}, {id: 'model2'}]);
373
- expect(collection.has('model1')).toBe(true);
374
- expect(collection.has({id: 'model1'})).toBe(true);
375
- expect(collection.has('model3')).toBe(false);
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('helper methods', () => {
381
- var model1 = new Model({id: 'model1'}),
382
- model2 = new Model({id: 'model2'}),
383
- model3 = new Model({id: 'model3'});
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('at', () => {
390
- it('returns the model at a specific index', () => {
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('returns the model at a specific index from the end for a negative index', () => {
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('map', () => {
406
- it('runs a map function across the models', () => {
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: 'model1', index: 0},
410
- {id: 'model2', index: 1},
411
- {id: 'model3', index: 2}
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('find', () => {
417
- it('finds the first model that passes the predicate', () => {
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('filter', () => {
424
- it('returns all models that pass the predicate', () => {
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('findWhere', () => {
430
- it('runs a find with shorthand', () => {
431
- expect(collection.findWhere({id: 'model2'})).toEqual(model2);
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('where', () => {
436
- it('runs filter with shorthand', () => {
437
- expect(collection.where({id: 'model2'})).toEqual([model2]);
438
- expect(collection.where({id: 'model4'})).toEqual([]);
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('pluck', () => {
443
- it('picks out a property from all models', () => {
444
- expect(collection.pluck('id')).toEqual(['model1', 'model2', 'model3']);
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('slice', () => {
449
- it('returns a subarray of models', () => {
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('sort', () => {
448
+ describe("sort", () => {
456
449
  beforeEach(() => {
457
450
  collection = new Collection([
458
- {id: 'model1', name: 'noah'},
459
- {id: 'model2', name: 'zorah'},
460
- {id: 'model3', name: 'alex'}
451
+ { id: "model1", name: "noah" },
452
+ { id: "model2", name: "zorah" },
453
+ { id: "model3", name: "alex" },
461
454
  ]);
462
455
  });
463
456
 
464
- it('throws if no comparator exists on a collection', () => {
465
- expect(() => collection.sort()).toThrowError('Cannot sort a set without a comparator');
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('can sort by property', () => {
469
- collection.comparator = 'name';
461
+ it("can sort by property", () => {
462
+ collection.comparator = "name";
470
463
  collection.sort();
471
464
 
472
465
  expect(collection.toJSON()).toEqual([
473
- {id: 'model3', name: 'alex'},
474
- {id: 'model1', name: 'noah'},
475
- {id: 'model2', name: 'zorah'}
466
+ { id: "model3", name: "alex" },
467
+ { id: "model1", name: "noah" },
468
+ { id: "model2", name: "zorah" },
476
469
  ]);
477
470
  });
478
471
 
479
- it('can sort by function with one argument', () => {
480
- collection.comparator = (model) => model.get('name');
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: 'model3', name: 'alex'},
485
- {id: 'model1', name: 'noah'},
486
- {id: 'model2', name: 'zorah'}
477
+ { id: "model3", name: "alex" },
478
+ { id: "model1", name: "noah" },
479
+ { id: "model2", name: "zorah" },
487
480
  ]);
488
481
  });
489
482
 
490
- it('can sort by function with two arguments', () => {
491
- collection.comparator = (left, right) => left.get('name') > right.get('name ') ? -1 : 1;
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: 'model1', name: 'noah'},
496
- {id: 'model2', name: 'zorah'},
497
- {id: 'model3', name: 'alex'}
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('fetch', () => {
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('calls sync with a GET method', async() => {
506
+ it("calls sync with a GET method", async () => {
517
507
  await collection.fetch();
518
508
 
519
- expect(sync.default).toHaveBeenCalledWith(collection, {method: 'GET', parse: true});
520
- await collection.fetch({method: 'POST', url: '/books'});
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: 'POST',
513
+ method: "POST",
524
514
  parse: true,
525
- url: '/books'
515
+ url: "/books",
526
516
  });
527
517
  });
528
518
 
529
- it('triggers an update on returning', async() => {
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([{id: 'one', index: 0}, {id: 'two', index: 1}]);
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: 'one'}, {id: 'two'}]);
537
+ expect(collection.toJSON()).toEqual([{ id: "one" }, { id: "two" }]);
545
538
  });
546
539
 
547
- it('resolves a tuple of the collection instance and repsonse object', async() => {
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('rejects the response', async() => {
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('create', () => {
555
+ describe("create", () => {
563
556
  var response = {};
564
557
 
565
558
  beforeEach(() => {
566
- collection = new Collection([{id: 'model1'}, {id: 'model2'}]);
567
- sync.default.mockResolvedValue([{id: 'model3'}, response]);
559
+ collection = new Collection([{ id: "model1" }, { id: "model2" }]);
560
+ sync.default.mockResolvedValue([{ id: "model3" }, response]);
568
561
  });
569
562
 
570
- it('combines adding model to the collection and saving the model to the server', async() => {
571
- var request = collection.create({name: 'model3'});
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: 'model1'},
582
- {id: 'model2'},
583
- {id: 'model3', name: 'model3'}
570
+ { id: "model1" },
571
+ { id: "model2" },
572
+ { id: "model3", name: "model3" },
584
573
  ]);
585
574
  });
586
575
 
587
- it('if `wait` is true, does not add the model until the request returns', async() => {
588
- var request = collection.create({name: 'model3'}, {wait: true});
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: 'model1'}, {id: 'model2'}]);
579
+ expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
591
580
 
592
581
  await request;
593
582
  expect(collection.toJSON()).toEqual([
594
- {id: 'model1'},
595
- {id: 'model2'},
596
- {id: 'model3', name: 'model3'}
583
+ { id: "model1" },
584
+ { id: "model2" },
585
+ { id: "model3", name: "model3" },
597
586
  ]);
598
587
  });
599
588
 
600
- it('resolves a tuple of the model instance and repsonse object', async() => {
601
- expect(await collection.create({name: 'model3'})).toEqual([
602
- collection.get('model3'),
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('rejects the response and removes the model from the collection if added', async() => {
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: 'model3'});
600
+ await collection.create({ name: "model3" });
612
601
  } catch (err) {
613
602
  expect(err).toEqual(response);
614
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}]);
603
+ expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
615
604
  }
616
605
 
617
606
  try {
618
- await collection.create({name: 'model3'}, {wait: true});
607
+ await collection.create({ name: "model3" }, { wait: true });
619
608
  } catch (err) {
620
609
  expect(err).toEqual(response);
621
- expect(collection.toJSON()).toEqual([{id: 'model1'}, {id: 'model2'}]);
610
+ expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model2" }]);
622
611
  }
623
612
  });
624
613
  });
625
614
 
626
- describe('parse', () => {
627
- it('is by default the identity function', () => {
615
+ describe("parse", () => {
616
+ it("is by default the identity function", () => {
628
617
  collection = new Collection();
629
- expect(collection.parse({foo: 'bar'})).toEqual({foo: 'bar'});
618
+ expect(collection.parse({ foo: "bar" })).toEqual({ foo: "bar" });
630
619
  });
631
620
  });
632
621
 
633
- describe('setting a model id attribute', () => {
634
- it('on the model sets the id attributes for all models', () => {
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 = 'name'
625
+ static idAttribute = "name";
637
626
  }
638
627
 
639
628
  // default is just id
640
629
  collection = new Collection();
641
- collection.add({id: 'noahgrant', name: 'Noah Grant'});
642
- expect(collection.get('noahgrant')).toBeDefined();
643
- expect(collection.get('Noah Grant')).not.toBeDefined();
644
- expect(collection.get('noahgrant').id).toEqual('noahgrant');
645
- expect(collection.Model.idAttribute).toEqual('id');
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: 'noahgrant', name: 'Noah Grant'});
649
- expect(collection.get('noahgrant')).not.toBeDefined();
650
- expect(collection.get('Noah Grant')).toBeDefined();
651
- expect(collection.get('Noah Grant').id).toEqual('Noah Grant');
652
- expect(collection.Model.idAttribute).toEqual('name');
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('on the collection sets the id attribute for its models', () => {
644
+ it("on the collection sets the id attribute for its models", () => {
656
645
  class _Collection extends Collection {
657
- static modelIdAttribute = 'name'
646
+ static idAttribute = "name";
658
647
  }
659
648
 
660
649
  collection = new _Collection();
661
- collection.add({id: 'noahgrant', name: 'Noah Grant'});
662
- expect(collection.get('noahgrant')).not.toBeDefined();
663
- expect(collection.get('Noah Grant')).toBeDefined();
664
- expect(collection.get('Noah Grant').id).toEqual('Noah Grant');
665
- expect(collection.Model.idAttribute).toEqual('name');
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('_updateModelReference', () => {
670
- it('updates the reference to a model when its id changes', () => {
671
- var model = new Model({id: 'model1'});
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('model1')).toEqual(model);
664
+ expect(collection.get("model1")).toEqual(model);
676
665
 
677
- model.set({id: '1model'});
678
- expect(collection.get('model1')).not.toBeDefined();
679
- expect(collection.get('1model')).toEqual(model);
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('does not add a reference if the id does not exist', () => {
683
- var model = new Model({id: 'model1'});
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, 'model1', model);
687
- expect(collection.get('model1')).toEqual(model);
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
  });