resourcerer 2.0.6 → 2.1.0
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/build/index.d.ts +2 -1
- package/build/index.js +1 -0
- package/build/lib/canonical-model-cache.d.ts +8 -0
- package/build/lib/canonical-model-cache.js +24 -0
- package/build/lib/canonical-model.d.ts +14 -0
- package/build/lib/canonical-model.js +33 -0
- package/build/lib/collection.d.ts +2 -1
- package/build/lib/collection.js +4 -0
- package/build/lib/events.d.ts +7 -7
- package/build/lib/events.js +2 -2
- package/build/lib/model-cache.d.ts +1 -0
- package/build/lib/model-cache.js +1 -0
- package/build/lib/model.d.ts +16 -3
- package/build/lib/model.js +55 -3
- package/build/lib/request.d.ts +1 -0
- package/build/lib/resourcerer.js +1 -1
- package/build/lib/sync.js +3 -2
- package/build/lib/types.d.ts +3 -0
- package/build/lib/utils.d.ts +1 -0
- package/build/lib/utils.js +3 -0
- package/package.json +1 -1
- package/test/canonical-model-cache.test.js +75 -0
- package/test/canonical-model.test.js +87 -0
- package/test/collection.test.js +16 -2
- package/test/events.test.js +83 -0
- package/test/model-cache.test.js +30 -24
- package/test/model.test.js +358 -1
- package/test/sync.test.js +10 -0
- package/test/utils.test.js +22 -5
package/test/events.test.js
CHANGED
|
@@ -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,51 @@ 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 target is also a source", () => {
|
|
224
|
+
const sourceModel = new SubscribingSourceAndTargetModel({ id: "1234" });
|
|
225
|
+
const targetModel = new SubscribingSourceAndTargetModel({ id: "1234" });
|
|
226
|
+
|
|
227
|
+
sourceModel.set({ name: "Zorah" });
|
|
228
|
+
expect(targetModel.get("name")).toEqual("Zorah");
|
|
229
|
+
targetModel.set({ name: "Zorah 2" });
|
|
230
|
+
expect(sourceModel.get("name")).toEqual("Zorah 2");
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
it("unless a model has unsubscribed", () => {
|
|
234
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
235
|
+
expect(targetModel.get("name")).toEqual("C. Zorah");
|
|
236
|
+
targetModel.unsubscribe();
|
|
237
|
+
sourceModel.set({ name: "Zorah 2" });
|
|
238
|
+
expect(targetModel.get("name")).toEqual("C. Zorah");
|
|
239
|
+
});
|
|
240
|
+
});
|
|
194
241
|
});
|
|
195
242
|
|
|
196
243
|
it("gracefully removes listeners even if there are none", () => {
|
|
@@ -199,3 +246,39 @@ describe("Events", () => {
|
|
|
199
246
|
expect(() => model.offUpdate(model)).not.toThrow();
|
|
200
247
|
});
|
|
201
248
|
});
|
|
249
|
+
|
|
250
|
+
class CanonicalTestModel extends CanonicalModel {}
|
|
251
|
+
// this model has its own idattribute of id, but the canonical model uses the value of the _id property
|
|
252
|
+
class SubscribingSourceModel extends Model {
|
|
253
|
+
static subscriptions = [
|
|
254
|
+
{
|
|
255
|
+
Model: CanonicalTestModel,
|
|
256
|
+
idField: "_id",
|
|
257
|
+
toSource: (attrs) => ({ _id: attrs._id, name: attrs.name }),
|
|
258
|
+
},
|
|
259
|
+
];
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
class SubscribingTargetModel extends Model {
|
|
263
|
+
static subscriptions = [
|
|
264
|
+
{
|
|
265
|
+
Model: CanonicalTestModel,
|
|
266
|
+
// default idField to id
|
|
267
|
+
fromSource: (attrs) => ({ id: attrs._id, name: "C. " + attrs.name }),
|
|
268
|
+
},
|
|
269
|
+
];
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
class SubscribingTargetCollection extends Collection {
|
|
273
|
+
static Model = SubscribingTargetModel;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
class SubscribingSourceAndTargetModel extends Model {
|
|
277
|
+
static subscriptions = [
|
|
278
|
+
{
|
|
279
|
+
Model: CanonicalTestModel,
|
|
280
|
+
fromSource: (attrs) => attrs,
|
|
281
|
+
toSource: (attrs) => attrs,
|
|
282
|
+
},
|
|
283
|
+
];
|
|
284
|
+
}
|
package/test/model-cache.test.js
CHANGED
|
@@ -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
|
-
|
|
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",
|
|
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",
|
|
101
|
+
ModelCache.put("foo", new Model(), component);
|
|
101
102
|
expect(ModelCache.get("foo")).toBeDefined();
|
|
102
103
|
ModelCache.unregister(component);
|
|
103
|
-
ModelCache.put("foo",
|
|
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",
|
|
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",
|
|
128
|
-
ModelCache.put("bar",
|
|
129
|
-
ModelCache.put("baz",
|
|
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",
|
|
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();
|
package/test/model.test.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
import * as sync from "../lib/sync";
|
|
2
|
+
import CanonicalModel from "../lib/canonical-model";
|
|
3
|
+
import { canonicalModelCache } from "../lib/canonical-model-cache";
|
|
2
4
|
|
|
3
5
|
import Collection from "../lib/collection";
|
|
4
6
|
import Model from "../lib/model";
|
|
@@ -47,7 +49,7 @@ describe("Model", () => {
|
|
|
47
49
|
parse(attrs) {
|
|
48
50
|
return Object.keys(attrs).reduce(
|
|
49
51
|
(memo, attr) => Object.assign(memo, { [attr]: attrs[attr] + 5 }),
|
|
50
|
-
{}
|
|
52
|
+
{},
|
|
51
53
|
);
|
|
52
54
|
}
|
|
53
55
|
}
|
|
@@ -514,4 +516,359 @@ describe("Model", () => {
|
|
|
514
516
|
expect(model.isNew()).toBe(false);
|
|
515
517
|
});
|
|
516
518
|
});
|
|
519
|
+
|
|
520
|
+
describe("subscriptions", () => {
|
|
521
|
+
let sourceModel, targetModel;
|
|
522
|
+
|
|
523
|
+
beforeEach(() => {
|
|
524
|
+
sourceModel = new SubscribingSourceModel();
|
|
525
|
+
targetModel = new SubscribingTargetModel();
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
afterEach(() => {
|
|
529
|
+
sourceModel.unsubscribe();
|
|
530
|
+
targetModel.unsubscribe();
|
|
531
|
+
canonicalModelCache.clear();
|
|
532
|
+
});
|
|
533
|
+
|
|
534
|
+
describe("are not made", () => {
|
|
535
|
+
it("if the model is the empty model", async () => {
|
|
536
|
+
vi.spyOn(targetModel, "fetch").mockResolvedValue([{}, {}]);
|
|
537
|
+
|
|
538
|
+
targetModel.isEmptyModel = true;
|
|
539
|
+
await targetModel.fetch();
|
|
540
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
541
|
+
expect(targetModel.get("name")).toBeUndefined();
|
|
542
|
+
delete targetModel.isEmptyModel;
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
it("if the model is new", () => {
|
|
546
|
+
// there is no id linking them, so no subscriptions will be made
|
|
547
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
548
|
+
expect(targetModel.get("name")).toBeUndefined();
|
|
549
|
+
});
|
|
550
|
+
|
|
551
|
+
it("if the subscribe option is false when the model is instantiated", () => {
|
|
552
|
+
sourceModel = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
553
|
+
targetModel = new SubscribingTargetModel({ id: "1234", subscribe: false });
|
|
554
|
+
|
|
555
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
556
|
+
expect(targetModel.get("name")).toBeUndefined();
|
|
557
|
+
});
|
|
558
|
+
|
|
559
|
+
it("if the model is already subscribed", () => {
|
|
560
|
+
targetModel = new SubscribingTargetModel({ id: "1234" });
|
|
561
|
+
sourceModel = new SubscribingSourceModel();
|
|
562
|
+
|
|
563
|
+
vi.spyOn(targetModel, "set");
|
|
564
|
+
|
|
565
|
+
targetModel._subscribe();
|
|
566
|
+
targetModel._subscribe();
|
|
567
|
+
targetModel._subscribe();
|
|
568
|
+
targetModel._subscribe();
|
|
569
|
+
|
|
570
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
571
|
+
expect(targetModel.get("name")).toBe("C. Zorah");
|
|
572
|
+
// only called once!
|
|
573
|
+
expect(targetModel.set).toHaveBeenCalledTimes(1);
|
|
574
|
+
});
|
|
575
|
+
});
|
|
576
|
+
|
|
577
|
+
describe("are made", () => {
|
|
578
|
+
it("when the model is instantiated", () => {
|
|
579
|
+
targetModel = new SubscribingTargetModel({ id: "1234" });
|
|
580
|
+
|
|
581
|
+
vi.spyOn(targetModel, "set");
|
|
582
|
+
sourceModel = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
583
|
+
|
|
584
|
+
expect(targetModel.get("name")).toBe("C. Zorah");
|
|
585
|
+
expect(targetModel.set).toHaveBeenCalledTimes(1);
|
|
586
|
+
});
|
|
587
|
+
|
|
588
|
+
it("when the model is fetched", async () => {
|
|
589
|
+
// no id yet, so no subscription made
|
|
590
|
+
targetModel = new SubscribingTargetModel();
|
|
591
|
+
// id will return on the fetch
|
|
592
|
+
vi.spyOn(targetModel, "sync").mockResolvedValue([{ id: "1234" }, {}]);
|
|
593
|
+
|
|
594
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
595
|
+
expect(targetModel.get("name")).not.toBe("C. Zorah");
|
|
596
|
+
|
|
597
|
+
await targetModel.fetch();
|
|
598
|
+
|
|
599
|
+
sourceModel.set({ _id: "1234", name: "Zorah 2" });
|
|
600
|
+
expect(targetModel.get("name")).toBe("C. Zorah 2");
|
|
601
|
+
});
|
|
602
|
+
|
|
603
|
+
it("when the model is saved", async () => {
|
|
604
|
+
// no id yet, so no subscription made
|
|
605
|
+
targetModel = new SubscribingTargetModel();
|
|
606
|
+
// id will return on the save
|
|
607
|
+
vi.spyOn(targetModel, "sync").mockResolvedValue([{ id: "1234" }, {}]);
|
|
608
|
+
|
|
609
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
610
|
+
expect(targetModel.get("name")).not.toBe("C. Zorah");
|
|
611
|
+
|
|
612
|
+
await targetModel.save();
|
|
613
|
+
|
|
614
|
+
sourceModel.set({ _id: "1234", name: "Zorah 2" });
|
|
615
|
+
expect(targetModel.get("name")).toBe("C. Zorah 2");
|
|
616
|
+
});
|
|
617
|
+
});
|
|
618
|
+
|
|
619
|
+
describe("are not updated", () => {
|
|
620
|
+
it("if the subscribe option is false", () => {
|
|
621
|
+
sourceModel = new SubscribingSourceModel();
|
|
622
|
+
targetModel = new SubscribingTargetModel({ id: "1234" });
|
|
623
|
+
|
|
624
|
+
sourceModel.set({ _id: "1234", name: "Zorah" }, { subscribe: false });
|
|
625
|
+
expect(targetModel.get("name")).not.toBeDefined();
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
it("if the source options is 'subscription'", () => {
|
|
629
|
+
sourceModel = new SubscribingSourceModel();
|
|
630
|
+
targetModel = new SubscribingTargetModel({ id: "1234" });
|
|
631
|
+
|
|
632
|
+
sourceModel.set({ _id: "1234", name: "Zorah" }, { source: "subscription" });
|
|
633
|
+
expect(targetModel.get("name")).not.toBeDefined();
|
|
634
|
+
});
|
|
635
|
+
|
|
636
|
+
it("if nothing changes", () => {
|
|
637
|
+
vi.spyOn(SubscribingTargetModel.prototype, "_updateSubscriptions");
|
|
638
|
+
|
|
639
|
+
sourceModel = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
640
|
+
targetModel = new SubscribingTargetModel({ id: "1234", name: "C. Zorah" });
|
|
641
|
+
|
|
642
|
+
expect(SubscribingTargetModel.prototype._updateSubscriptions).toHaveBeenCalledTimes(1);
|
|
643
|
+
SubscribingTargetModel.prototype._updateSubscriptions.mockClear();
|
|
644
|
+
|
|
645
|
+
sourceModel.set({ _id: "1234", name: "Zorah" });
|
|
646
|
+
expect(SubscribingTargetModel.prototype._updateSubscriptions).not.toHaveBeenCalled();
|
|
647
|
+
});
|
|
648
|
+
});
|
|
649
|
+
});
|
|
650
|
+
|
|
651
|
+
describe("_subscribe", () => {
|
|
652
|
+
beforeEach(() => {
|
|
653
|
+
vi.spyOn(CanonicalModel.prototype, "onUpdate").mockReturnValue();
|
|
654
|
+
vi.spyOn(CanonicalModel.prototype, "offUpdate").mockReturnValue();
|
|
655
|
+
});
|
|
656
|
+
|
|
657
|
+
describe("does nothing", () => {
|
|
658
|
+
it("if the model is the empty model", () => {
|
|
659
|
+
const emptyModel = new SubscribingTargetModel();
|
|
660
|
+
|
|
661
|
+
emptyModel.isEmptyModel = true;
|
|
662
|
+
emptyModel._subscribe();
|
|
663
|
+
|
|
664
|
+
expect(CanonicalModel.prototype.onUpdate).not.toHaveBeenCalled();
|
|
665
|
+
expect(CanonicalModel.prototype.offUpdate).not.toHaveBeenCalled();
|
|
666
|
+
});
|
|
667
|
+
|
|
668
|
+
it("if the model has no canonical model id", () => {
|
|
669
|
+
const model = new SubscribingTargetModel({ name: "Zorah" });
|
|
670
|
+
|
|
671
|
+
model._subscribe();
|
|
672
|
+
|
|
673
|
+
expect(CanonicalModel.prototype.onUpdate).not.toHaveBeenCalled();
|
|
674
|
+
expect(CanonicalModel.prototype.offUpdate).not.toHaveBeenCalled();
|
|
675
|
+
});
|
|
676
|
+
});
|
|
677
|
+
|
|
678
|
+
describe("the id field", () => {
|
|
679
|
+
it("can come from attributes set on the model", () => {
|
|
680
|
+
const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
681
|
+
|
|
682
|
+
model._subscribe();
|
|
683
|
+
|
|
684
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
|
|
685
|
+
expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledWith(model);
|
|
686
|
+
});
|
|
687
|
+
|
|
688
|
+
it("can come from the attributes passed to the _subscribe method", () => {
|
|
689
|
+
const model = new SubscribingSourceModel();
|
|
690
|
+
|
|
691
|
+
model._subscribe({ _id: "1234", name: "Zorah" });
|
|
692
|
+
|
|
693
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
|
|
694
|
+
expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledWith(model);
|
|
695
|
+
});
|
|
696
|
+
|
|
697
|
+
it("can be nested with a dot-separated string", () => {
|
|
698
|
+
class NestedSubscribingSourceModel extends SubscribingSourceModel {
|
|
699
|
+
static subscriptions = [
|
|
700
|
+
{
|
|
701
|
+
Model: CanonicalTestModel,
|
|
702
|
+
idField: "nested._id",
|
|
703
|
+
},
|
|
704
|
+
];
|
|
705
|
+
}
|
|
706
|
+
|
|
707
|
+
const model = new NestedSubscribingSourceModel({ nested: { _id: "1234", name: "Zorah" } });
|
|
708
|
+
|
|
709
|
+
model._subscribe();
|
|
710
|
+
|
|
711
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
|
|
712
|
+
expect(CanonicalModel.prototype.offUpdate).toHaveBeenCalledWith(model);
|
|
713
|
+
});
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
describe("the onUpdate callback", () => {
|
|
717
|
+
describe("does nothing", () => {
|
|
718
|
+
it("if the context is the same as the model instance", () => {
|
|
719
|
+
const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
|
|
720
|
+
const model2 = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
|
|
721
|
+
|
|
722
|
+
vi.spyOn(model, "set");
|
|
723
|
+
|
|
724
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
|
|
725
|
+
// same context
|
|
726
|
+
CanonicalModel.prototype.onUpdate.mock.calls[0][0]({ name: "Zorah 2" }, model);
|
|
727
|
+
|
|
728
|
+
expect(model.set).not.toHaveBeenCalled();
|
|
729
|
+
});
|
|
730
|
+
|
|
731
|
+
it("if there is no fromSource function", () => {
|
|
732
|
+
// the source model is toSource only
|
|
733
|
+
const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
734
|
+
const model2 = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
735
|
+
|
|
736
|
+
vi.spyOn(model, "set");
|
|
737
|
+
|
|
738
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
|
|
739
|
+
// different context
|
|
740
|
+
CanonicalModel.prototype.onUpdate.mock.calls[0][0]({ name: "Zorah 2" }, model2);
|
|
741
|
+
|
|
742
|
+
expect(model.set).not.toHaveBeenCalled();
|
|
743
|
+
});
|
|
744
|
+
});
|
|
745
|
+
|
|
746
|
+
describe("updates the model", () => {
|
|
747
|
+
it("if the fromSource function is provided", () => {
|
|
748
|
+
const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
|
|
749
|
+
const model2 = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
750
|
+
|
|
751
|
+
vi.spyOn(model, "set");
|
|
752
|
+
|
|
753
|
+
expect(CanonicalModel.prototype.onUpdate).toHaveBeenCalled();
|
|
754
|
+
// different context
|
|
755
|
+
CanonicalModel.prototype.onUpdate.mock.calls[0][0](
|
|
756
|
+
{ _id: "1234", name: "Zorah 2" },
|
|
757
|
+
model2,
|
|
758
|
+
);
|
|
759
|
+
|
|
760
|
+
expect(model.set).toHaveBeenCalledWith(
|
|
761
|
+
// mapped attributes
|
|
762
|
+
{ id: "1234", name: "C. Zorah 2" },
|
|
763
|
+
{ source: "subscription" },
|
|
764
|
+
);
|
|
765
|
+
});
|
|
766
|
+
});
|
|
767
|
+
});
|
|
768
|
+
});
|
|
769
|
+
|
|
770
|
+
describe("unsubscribe", () => {
|
|
771
|
+
beforeEach(() => {
|
|
772
|
+
vi.spyOn(CanonicalModel.prototype, "removeSubscription").mockReturnValue();
|
|
773
|
+
});
|
|
774
|
+
|
|
775
|
+
it("does nothing if the model has no canonical model id", () => {
|
|
776
|
+
const model = new SubscribingTargetModel({ name: "Zorah" });
|
|
777
|
+
|
|
778
|
+
model.unsubscribe();
|
|
779
|
+
|
|
780
|
+
expect(CanonicalModel.prototype.removeSubscription).not.toHaveBeenCalled();
|
|
781
|
+
});
|
|
782
|
+
|
|
783
|
+
it("removes the model from the canonical model's subscriptions", () => {
|
|
784
|
+
const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
|
|
785
|
+
|
|
786
|
+
model.unsubscribe();
|
|
787
|
+
|
|
788
|
+
expect(CanonicalModel.prototype.removeSubscription).toHaveBeenCalledWith(model, "1234");
|
|
789
|
+
});
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
describe("_updateSubscriptions", () => {
|
|
793
|
+
beforeEach(() => {
|
|
794
|
+
vi.spyOn(CanonicalModel.prototype, "set").mockReturnValue();
|
|
795
|
+
});
|
|
796
|
+
|
|
797
|
+
describe("does nothing", () => {
|
|
798
|
+
it("if the model has no canonical model id", () => {
|
|
799
|
+
const model = new SubscribingTargetModel({ name: "Zorah" });
|
|
800
|
+
|
|
801
|
+
model._updateSubscriptions();
|
|
802
|
+
|
|
803
|
+
expect(CanonicalModel.prototype.set).not.toHaveBeenCalled();
|
|
804
|
+
});
|
|
805
|
+
|
|
806
|
+
it("if the model is the empty model", () => {
|
|
807
|
+
const model = new SubscribingSourceModel();
|
|
808
|
+
|
|
809
|
+
model.isEmptyModel = true;
|
|
810
|
+
model.set({ _id: "1234", name: "Zorah" });
|
|
811
|
+
model._updateSubscriptions();
|
|
812
|
+
|
|
813
|
+
expect(CanonicalModel.prototype.set).not.toHaveBeenCalled();
|
|
814
|
+
});
|
|
815
|
+
|
|
816
|
+
it("if there is no toSource function", () => {
|
|
817
|
+
const model = new SubscribingTargetModel({ id: "1234", name: "Zorah" });
|
|
818
|
+
|
|
819
|
+
model._updateSubscriptions({ _id: "1234", name: "Zorah" });
|
|
820
|
+
|
|
821
|
+
expect(CanonicalModel.prototype.set).not.toHaveBeenCalled();
|
|
822
|
+
});
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
describe("the id field", () => {
|
|
826
|
+
it("can come from attributes set on the model", () => {
|
|
827
|
+
const model = new SubscribingSourceModel({ _id: "1234", name: "Zorah" });
|
|
828
|
+
|
|
829
|
+
model._updateSubscriptions();
|
|
830
|
+
|
|
831
|
+
expect(CanonicalModel.prototype.set).toHaveBeenCalledWith(
|
|
832
|
+
{ _id: "1234", name: "Zorah" },
|
|
833
|
+
model,
|
|
834
|
+
{},
|
|
835
|
+
);
|
|
836
|
+
});
|
|
837
|
+
|
|
838
|
+
it("can come from the attributes passed to the _updateSubscriptions method", () => {
|
|
839
|
+
const model = new SubscribingSourceModel();
|
|
840
|
+
const options = {};
|
|
841
|
+
|
|
842
|
+
model._updateSubscriptions({ _id: "1234", name: "Zorah" }, options);
|
|
843
|
+
|
|
844
|
+
expect(CanonicalModel.prototype.set).toHaveBeenCalledWith(
|
|
845
|
+
{ _id: "1234", name: "Zorah" },
|
|
846
|
+
model,
|
|
847
|
+
options,
|
|
848
|
+
);
|
|
849
|
+
});
|
|
850
|
+
});
|
|
851
|
+
});
|
|
517
852
|
});
|
|
853
|
+
|
|
854
|
+
class CanonicalTestModel extends CanonicalModel {}
|
|
855
|
+
// this model has its own idattribute of id, but the canonical model uses the value of the _id property
|
|
856
|
+
class SubscribingSourceModel extends Model {
|
|
857
|
+
static subscriptions = [
|
|
858
|
+
{
|
|
859
|
+
Model: CanonicalTestModel,
|
|
860
|
+
idField: "_id",
|
|
861
|
+
toSource: (attrs) => ({ _id: attrs._id, name: attrs.name }),
|
|
862
|
+
},
|
|
863
|
+
];
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
class SubscribingTargetModel extends Model {
|
|
867
|
+
static subscriptions = [
|
|
868
|
+
{
|
|
869
|
+
Model: CanonicalTestModel,
|
|
870
|
+
// default idField to id
|
|
871
|
+
fromSource: (attrs) => ({ id: attrs._id, name: "C. " + attrs.name }),
|
|
872
|
+
},
|
|
873
|
+
];
|
|
874
|
+
}
|
package/test/sync.test.js
CHANGED
|
@@ -83,6 +83,16 @@ describe("sync", () => {
|
|
|
83
83
|
await library.at(0).save(null, { params: { a: "a", one: 1 }, contentType: "resourcerer/json" });
|
|
84
84
|
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
85
85
|
expect(window.fetch.mock.calls[0][1].body).toEqual("a=a&one=1");
|
|
86
|
+
window.fetch.mockClear();
|
|
87
|
+
|
|
88
|
+
// with body content: FormData
|
|
89
|
+
const formData = new FormData();
|
|
90
|
+
|
|
91
|
+
formData.append("a", 1);
|
|
92
|
+
|
|
93
|
+
await library.at(0).save(null, { params: formData, contentType: "multipart/form-data" });
|
|
94
|
+
expect(window.fetch.mock.calls[0][0]).toEqual("/library");
|
|
95
|
+
expect(window.fetch.mock.calls[0][1].body).toEqual(formData);
|
|
86
96
|
});
|
|
87
97
|
|
|
88
98
|
it("passes urlOptions to the model to formulate the url path", async () => {
|