resourcerer 2.0.7 → 2.1.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.
@@ -9,12 +9,13 @@ describe("Collection", () => {
9
9
 
10
10
  beforeEach(() => {
11
11
  vi.spyOn(sync, "default").mockResolvedValue([]);
12
+ vi.spyOn(Model.prototype, "unsubscribe");
12
13
  });
13
14
 
14
15
  afterEach(() => {
15
16
  collection = null;
16
- sync.default.mockRestore();
17
17
  callback.mockClear();
18
+ vi.restoreAllMocks();
18
19
  });
19
20
 
20
21
  it("is instantiated with appropriate internal properties", () => {
@@ -113,6 +114,7 @@ describe("Collection", () => {
113
114
  expect(collection.length).toEqual(3);
114
115
 
115
116
  collection.remove({ id: "model2" });
117
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(1);
116
118
  expect(collection.toJSON()).toEqual([{ id: "model1" }, { id: "model3" }]);
117
119
  expect(collection.length).toEqual(2);
118
120
  expect(collection.get("model2")).not.toBeDefined();
@@ -120,6 +122,8 @@ describe("Collection", () => {
120
122
  collection.remove([{ id: "model1" }, { id: "model3" }]);
121
123
  expect(collection.toJSON()).toEqual([]);
122
124
  expect(collection.length).toEqual(0);
125
+
126
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(3);
123
127
  });
124
128
 
125
129
  it("does not trigger an update if the `silent` flag is passed", () => {
@@ -132,6 +136,7 @@ describe("Collection", () => {
132
136
 
133
137
  collection.remove({ id: "model1" }, { silent: true });
134
138
  expect(callback).not.toHaveBeenCalled();
139
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(2);
135
140
  });
136
141
 
137
142
  it("does not trigger an update if nothing was removed", () => {
@@ -308,7 +313,7 @@ describe("Collection", () => {
308
313
  { id: "model1", name: "noah" },
309
314
  { id: "model2", name: "zorah" },
310
315
  ],
311
- { comparator: "name" }
316
+ { comparator: "name" },
312
317
  );
313
318
 
314
319
  collection.set({ id: "model3", name: "alex" });
@@ -655,6 +660,15 @@ describe("Collection", () => {
655
660
  });
656
661
  });
657
662
 
663
+ describe("unsubscribe", () => {
664
+ it("unsubscribes all models in the collection", () => {
665
+ collection = new Collection([{ id: "model1" }, { id: "model2" }, { id: "model3" }]);
666
+ collection.unsubscribe();
667
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(3);
668
+ expect(collection).toHaveLength(3);
669
+ });
670
+ });
671
+
658
672
  describe("_updateModelReference", () => {
659
673
  it("updates the reference to a model when its id changes", () => {
660
674
  var model = new Model({ id: "model1" });
@@ -1,7 +1,9 @@
1
1
  import * as sync from "../lib/sync";
2
2
  import Collection from "../lib/collection";
3
3
  import Model from "../lib/model";
4
+ import CanonicalModel from "../lib/canonical-model";
4
5
  import { vi } from "vitest";
6
+ import { canonicalModelCache } from "../lib/canonical-model-cache";
5
7
 
6
8
  class TestCollection extends Collection {
7
9
  url() {
@@ -191,6 +193,64 @@ describe("Events", () => {
191
193
  // for the sync, because the late add is silent
192
194
  expect(collectionCallback).toHaveBeenCalledTimes(1);
193
195
  });
196
+
197
+ describe("from a canonical model when a subscribing model is updated, updating the target model's attributes", () => {
198
+ let sourceModel, targetModel;
199
+
200
+ beforeEach(() => {
201
+ sourceModel = new SubscribingSourceModel();
202
+ targetModel = new SubscribingTargetModel({ id: "1234" });
203
+ });
204
+
205
+ afterEach(() => {
206
+ sourceModel.unsubscribe();
207
+ targetModel.unsubscribe();
208
+ canonicalModelCache.clear();
209
+ });
210
+
211
+ it("when the targetModel is solo", () => {
212
+ sourceModel.set({ _id: "1234", name: "Zorah" });
213
+ expect(targetModel.get("name")).toEqual("C. Zorah");
214
+ });
215
+
216
+ it("when the targetModel is part of a collection", () => {
217
+ const collection = new SubscribingTargetCollection([{ id: "1234" }]);
218
+
219
+ sourceModel.set({ _id: "1234", name: "Zorah" });
220
+ expect(collection.get("1234").get("name")).toEqual("C. Zorah");
221
+ });
222
+
223
+ it("when the targetModel is part of a collection with shorthand subscriptions defined", () => {
224
+ const collection = new SubscribingTargetShorthandCollection([{ id: "1234" }]);
225
+
226
+ sourceModel.set({ _id: "1234", name: "Zorah" });
227
+ expect(collection.get("1234").get("name")).toEqual("C. Zorah");
228
+ });
229
+
230
+ it("when the source model is part of a collection with shorthand subscriptions defined", () => {
231
+ new SubscribingSourceShorthandCollection([{ _id: "1234", name: "Zorah" }]);
232
+
233
+ expect(targetModel.get("name")).toEqual("C. Zorah");
234
+ });
235
+
236
+ it("when the target is also a source", () => {
237
+ const sourceModel = new SubscribingSourceAndTargetModel({ id: "1234" });
238
+ const targetModel = new SubscribingSourceAndTargetModel({ id: "1234" });
239
+
240
+ sourceModel.set({ name: "Zorah" });
241
+ expect(targetModel.get("name")).toEqual("Zorah");
242
+ targetModel.set({ name: "Zorah 2" });
243
+ expect(sourceModel.get("name")).toEqual("Zorah 2");
244
+ });
245
+
246
+ it("unless a model has unsubscribed", () => {
247
+ sourceModel.set({ _id: "1234", name: "Zorah" });
248
+ expect(targetModel.get("name")).toEqual("C. Zorah");
249
+ targetModel.unsubscribe();
250
+ sourceModel.set({ name: "Zorah 2" });
251
+ expect(targetModel.get("name")).toEqual("C. Zorah");
252
+ });
253
+ });
194
254
  });
195
255
 
196
256
  it("gracefully removes listeners even if there are none", () => {
@@ -199,3 +259,58 @@ describe("Events", () => {
199
259
  expect(() => model.offUpdate(model)).not.toThrow();
200
260
  });
201
261
  });
262
+
263
+ class CanonicalTestModel extends CanonicalModel {}
264
+ // this model has its own idattribute of id, but the canonical model uses the value of the _id property
265
+ class SubscribingSourceModel extends Model {
266
+ static subscriptions = [
267
+ {
268
+ Model: CanonicalTestModel,
269
+ idField: "_id",
270
+ toSource: (attrs) => ({ _id: attrs._id, name: attrs.name }),
271
+ },
272
+ ];
273
+ }
274
+
275
+ class SubscribingTargetModel extends Model {
276
+ static subscriptions = [
277
+ {
278
+ Model: CanonicalTestModel,
279
+ // default idField to id
280
+ fromSource: (attrs) => ({ id: attrs._id, name: "C. " + attrs.name }),
281
+ },
282
+ ];
283
+ }
284
+
285
+ class SubscribingTargetCollection extends Collection {
286
+ static Model = SubscribingTargetModel;
287
+ }
288
+
289
+ class SubscribingTargetShorthandCollection extends Collection {
290
+ static subscriptions = [
291
+ {
292
+ Model: CanonicalTestModel,
293
+ fromSource: (attrs) => ({ id: attrs._id, name: "C. " + attrs.name }),
294
+ },
295
+ ];
296
+ }
297
+
298
+ class SubscribingSourceShorthandCollection extends Collection {
299
+ static subscriptions = [
300
+ {
301
+ Model: CanonicalTestModel,
302
+ idField: "_id",
303
+ toSource: (attrs) => ({ _id: attrs._id, name: attrs.name }),
304
+ },
305
+ ];
306
+ }
307
+
308
+ class SubscribingSourceAndTargetModel extends Model {
309
+ static subscriptions = [
310
+ {
311
+ Model: CanonicalTestModel,
312
+ fromSource: (attrs) => attrs,
313
+ toSource: (attrs) => attrs,
314
+ },
315
+ ];
316
+ }
@@ -3,13 +3,14 @@ import Model from "../lib/model";
3
3
  import ModelCache, { invalidate } from "../lib/model-cache";
4
4
  import { vi } from "vitest";
5
5
 
6
- const CACHE_WAIT = 150000,
7
- testModel = {};
6
+ const CACHE_WAIT = 150000;
7
+ const testModel = new Model();
8
8
 
9
9
  describe("ModelCache", () => {
10
10
  beforeEach(() => {
11
11
  vi.useFakeTimers();
12
12
  vi.spyOn(ModelCache, "register");
13
+ vi.spyOn(Model.prototype, "unsubscribe");
13
14
  });
14
15
 
15
16
  afterEach(() => {
@@ -19,7 +20,7 @@ describe("ModelCache", () => {
19
20
  });
20
21
 
21
22
  it("gets a model from its cache via a unique key", () => {
22
- var model2 = {};
23
+ var model2 = new Model();
23
24
 
24
25
  ModelCache.put("foo", testModel, {});
25
26
  ModelCache.put("bar", model2, {});
@@ -87,7 +88,7 @@ describe("ModelCache", () => {
87
88
  component2 = {};
88
89
 
89
90
  beforeEach(() => {
90
- ModelCache.put("foo", {}, component);
91
+ ModelCache.put("foo", new Model(), component);
91
92
  });
92
93
 
93
94
  it("clears any current cache removal timeouts", () => {
@@ -97,10 +98,10 @@ describe("ModelCache", () => {
97
98
  expect(ModelCache.get("foo")).not.toBeDefined();
98
99
 
99
100
  // but if we re-register beforehand, we won't
100
- ModelCache.put("foo", {}, component);
101
+ ModelCache.put("foo", new Model(), component);
101
102
  expect(ModelCache.get("foo")).toBeDefined();
102
103
  ModelCache.unregister(component);
103
- ModelCache.put("foo", {}, component);
104
+ ModelCache.put("foo", new Model(), component);
104
105
  vi.advanceTimersByTime(CACHE_WAIT);
105
106
  expect(ModelCache.get("foo")).toBeDefined();
106
107
  });
@@ -111,7 +112,7 @@ describe("ModelCache", () => {
111
112
  // from the cache
112
113
  ModelCache.register("foo", component);
113
114
  // add new entry
114
- ModelCache.put("foo", {}, component2);
115
+ ModelCache.put("foo", new Model(), component2);
115
116
  ModelCache.unregister(component2);
116
117
  vi.advanceTimersByTime(CACHE_WAIT);
117
118
  // even though the first component didn't add the model, the model should
@@ -124,9 +125,9 @@ describe("ModelCache", () => {
124
125
  const component = {};
125
126
 
126
127
  beforeEach(() => {
127
- ModelCache.put("foo", {}, component);
128
- ModelCache.put("bar", {}, component);
129
- ModelCache.put("baz", {}, component);
128
+ ModelCache.put("foo", new Model(), component);
129
+ ModelCache.put("bar", new Model(), component);
130
+ ModelCache.put("baz", new Model(), component);
130
131
  });
131
132
 
132
133
  it("removes the component from the componentManifest for the given cache keys", () => {
@@ -147,8 +148,8 @@ describe("ModelCache", () => {
147
148
 
148
149
  it("schedules the model for cache removal if it no longer has components registered", () => {
149
150
  // let's add another component to foo and to baz
150
- ModelCache.put("foo", {}, {});
151
- ModelCache.put("bar", {}, {});
151
+ ModelCache.put("foo", new Model(), {});
152
+ ModelCache.put("bar", new Model(), {});
152
153
 
153
154
  ModelCache.unregister(component);
154
155
  vi.advanceTimersByTime(CACHE_WAIT);
@@ -158,6 +159,7 @@ describe("ModelCache", () => {
158
159
  // only baz will get removed, since the others have other components
159
160
  // still attached
160
161
  expect(ModelCache.get("baz")).not.toBeDefined();
162
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(1);
161
163
  ModelCache.unregister({});
162
164
  });
163
165
  });
@@ -166,23 +168,27 @@ describe("ModelCache", () => {
166
168
  const component = {};
167
169
 
168
170
  // let's register foo to three components
169
- ModelCache.put("foo", {}, {});
170
- ModelCache.put("foo", {}, {});
171
- ModelCache.put("foo", {}, {});
171
+ ModelCache.put("foo", new Model(), {});
172
+ ModelCache.put("foo", new Model(), {});
173
+ ModelCache.put("foo", new Model(), {});
172
174
 
173
175
  expect(ModelCache.get("foo")).toBeDefined();
174
176
  ModelCache.remove("foo");
175
177
  expect(ModelCache.get("foo")).not.toBeDefined();
178
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(1);
179
+ Model.prototype.unsubscribe.mockClear();
176
180
 
177
- ModelCache.put("foo", {}, component);
181
+ ModelCache.put("foo", new Model(), component);
178
182
  ModelCache.unregister(component);
179
183
 
180
184
  // still defined because of the timeout
181
185
  expect(ModelCache.get("foo")).toBeDefined();
186
+ expect(Model.prototype.unsubscribe).not.toHaveBeenCalled();
182
187
 
183
188
  ModelCache.remove("foo");
184
189
  // now immediately gone
185
190
  expect(ModelCache.get("foo")).not.toBeDefined();
191
+ expect(Model.prototype.unsubscribe).toHaveBeenCalledTimes(1);
186
192
  });
187
193
 
188
194
  it("when calling 'removeAllWithModel' removes all models of a specific key", () => {
@@ -195,7 +201,7 @@ describe("ModelCache", () => {
195
201
  "decisions",
196
202
  ];
197
203
 
198
- cacheKeys.forEach((key) => ModelCache.put(key, {}, {}));
204
+ cacheKeys.forEach((key) => ModelCache.put(key, new Model(), {}));
199
205
 
200
206
  ModelCache.removeAllWithModel("user");
201
207
  expect(ModelCache.get("user")).not.toBeDefined();
@@ -217,7 +223,7 @@ describe("ModelCache", () => {
217
223
  "decisions",
218
224
  ];
219
225
 
220
- cacheKeys.forEach((key) => ModelCache.put(key, {}, {}));
226
+ cacheKeys.forEach((key) => ModelCache.put(key, new Model(), {}));
221
227
 
222
228
  ModelCache.removeAllExcept(["users", "aliens"]);
223
229
  expect(ModelCache.get("user")).not.toBeDefined();
@@ -232,9 +238,9 @@ describe("ModelCache", () => {
232
238
 
233
239
  describe("invalidate", () => {
234
240
  it("removes models from the cache immediately", () => {
235
- ModelCache.put("foo", {}, {});
236
- ModelCache.put("bar", {}, {});
237
- ModelCache.put("baz", {}, {});
241
+ ModelCache.put("foo", new Model(), {});
242
+ ModelCache.put("bar", new Model(), {});
243
+ ModelCache.put("baz", new Model(), {});
238
244
  invalidate("foo");
239
245
  expect(ModelCache.get("foo")).not.toBeDefined();
240
246
  expect(ModelCache.get("bar")).toBeDefined();
@@ -251,9 +257,9 @@ describe("invalidate", () => {
251
257
  });
252
258
 
253
259
  it("removes models except those specified when the 'except' option is true", () => {
254
- ModelCache.put("foo", {}, {});
255
- ModelCache.put("bar", {}, {});
256
- ModelCache.put("baz", {}, {});
260
+ ModelCache.put("foo", new Model(), {});
261
+ ModelCache.put("bar", new Model(), {});
262
+ ModelCache.put("baz", new Model(), {});
257
263
  invalidate(["foo", "bar"], { except: true });
258
264
  expect(ModelCache.get("foo")).toBeDefined();
259
265
  expect(ModelCache.get("bar")).toBeDefined();