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/model.test.js
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
|
-
import * as sync from
|
|
1
|
+
import * as sync from "../lib/sync";
|
|
2
2
|
|
|
3
|
-
import Collection from
|
|
4
|
-
import Model from
|
|
3
|
+
import Collection from "../lib/collection";
|
|
4
|
+
import Model from "../lib/model";
|
|
5
|
+
import { vi } from "vitest";
|
|
5
6
|
|
|
6
|
-
describe(
|
|
7
|
+
describe("Model", () => {
|
|
7
8
|
var model,
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
collection,
|
|
10
|
+
callback = vi.fn();
|
|
10
11
|
|
|
11
12
|
beforeEach(() => {
|
|
12
|
-
|
|
13
|
+
vi.spyOn(sync, "default").mockResolvedValue([]);
|
|
13
14
|
});
|
|
14
15
|
|
|
15
16
|
afterEach(() => {
|
|
@@ -18,35 +19,34 @@ describe('Model', () => {
|
|
|
18
19
|
callback.mockClear();
|
|
19
20
|
});
|
|
20
21
|
|
|
21
|
-
it(
|
|
22
|
-
model = new Model;
|
|
22
|
+
it("is given a cid", () => {
|
|
23
|
+
model = new Model();
|
|
23
24
|
|
|
24
25
|
expect(model.cid).toBeDefined();
|
|
25
|
-
expect(model.cid.startsWith(
|
|
26
|
+
expect(model.cid.startsWith("c")).toBe(true);
|
|
26
27
|
});
|
|
27
28
|
|
|
28
|
-
it(
|
|
29
|
-
expect(Model.idAttribute).toEqual(
|
|
30
|
-
expect(Model.cacheFields).toEqual([]);
|
|
29
|
+
it("has expected default static properties", () => {
|
|
30
|
+
expect(Model.idAttribute).toEqual("id");
|
|
31
31
|
expect(Model.dependencies).toEqual([]);
|
|
32
32
|
expect(Model.defaults).toEqual({});
|
|
33
33
|
});
|
|
34
34
|
|
|
35
|
-
it(
|
|
36
|
-
model = new Model;
|
|
37
|
-
collection = new Collection;
|
|
35
|
+
it("gets its collection assigned to a `collection` property if passed", () => {
|
|
36
|
+
model = new Model();
|
|
37
|
+
collection = new Collection();
|
|
38
38
|
|
|
39
39
|
expect(model.collection).not.toBeDefined();
|
|
40
40
|
|
|
41
|
-
model = new Model({}, {collection});
|
|
41
|
+
model = new Model({}, { collection });
|
|
42
42
|
expect(model.collection).toEqual(collection);
|
|
43
43
|
});
|
|
44
44
|
|
|
45
|
-
it(
|
|
45
|
+
it("parses its attributes before being set if passed a `parse: true` option", () => {
|
|
46
46
|
class _Model extends Model {
|
|
47
47
|
parse(attrs) {
|
|
48
48
|
return Object.keys(attrs).reduce(
|
|
49
|
-
(memo, attr) => Object.assign(memo, {[attr]: attrs[attr] + 5}),
|
|
49
|
+
(memo, attr) => Object.assign(memo, { [attr]: attrs[attr] + 5 }),
|
|
50
50
|
{}
|
|
51
51
|
);
|
|
52
52
|
}
|
|
@@ -56,144 +56,144 @@ describe('Model', () => {
|
|
|
56
56
|
parse() {}
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
model = new _Model({one: 1, two: 2});
|
|
60
|
-
expect(model.toJSON()).toEqual({one: 1, two: 2});
|
|
59
|
+
model = new _Model({ one: 1, two: 2 });
|
|
60
|
+
expect(model.toJSON()).toEqual({ one: 1, two: 2 });
|
|
61
61
|
|
|
62
|
-
model = new _Model({one: 1, two: 2}, {parse: true});
|
|
63
|
-
expect(model.toJSON()).toEqual({one: 6, two: 7});
|
|
64
|
-
model = new __Model({one: 1, two: 2}, {parse: true});
|
|
62
|
+
model = new _Model({ one: 1, two: 2 }, { parse: true });
|
|
63
|
+
expect(model.toJSON()).toEqual({ one: 6, two: 7 });
|
|
64
|
+
model = new __Model({ one: 1, two: 2 }, { parse: true });
|
|
65
65
|
expect(model.toJSON()).toEqual({});
|
|
66
66
|
});
|
|
67
67
|
|
|
68
|
-
it(
|
|
69
|
-
model = new Model({}, {one: 1, two: 2, parse: true, silent: true});
|
|
68
|
+
it("unreserved option items are assigned to a `urlOptions` instance property", () => {
|
|
69
|
+
model = new Model({}, { one: 1, two: 2, parse: true, silent: true });
|
|
70
70
|
|
|
71
|
-
expect(model.urlOptions).toEqual({one: 1, two: 2});
|
|
71
|
+
expect(model.urlOptions).toEqual({ one: 1, two: 2 });
|
|
72
72
|
});
|
|
73
73
|
|
|
74
|
-
it(
|
|
74
|
+
it("can optionally have defaults that are a function", () => {
|
|
75
75
|
class _Model extends Model {
|
|
76
|
-
static defaults = {one: 1, two: 2}
|
|
76
|
+
static defaults = { one: 1, two: 2 };
|
|
77
77
|
}
|
|
78
78
|
|
|
79
|
-
model = new _Model({two: 5});
|
|
80
|
-
expect(model.toJSON()).toEqual({one: 1, two: 5});
|
|
79
|
+
model = new _Model({ two: 5 });
|
|
80
|
+
expect(model.toJSON()).toEqual({ one: 1, two: 5 });
|
|
81
81
|
|
|
82
82
|
class __Model extends Model {
|
|
83
83
|
static defaults() {
|
|
84
|
-
return {one: 1, two: 2};
|
|
84
|
+
return { one: 1, two: 2 };
|
|
85
85
|
}
|
|
86
86
|
}
|
|
87
87
|
|
|
88
|
-
model = new __Model({two: 5});
|
|
89
|
-
expect(model.toJSON()).toEqual({one: 1, two: 5});
|
|
88
|
+
model = new __Model({ two: 5 });
|
|
89
|
+
expect(model.toJSON()).toEqual({ one: 1, two: 5 });
|
|
90
90
|
});
|
|
91
91
|
|
|
92
|
-
it(
|
|
93
|
-
model = new Model;
|
|
94
|
-
model.sync(
|
|
95
|
-
expect(sync.default).toHaveBeenCalledWith(
|
|
92
|
+
it("model.sync proxies the sync module", () => {
|
|
93
|
+
model = new Model();
|
|
94
|
+
model.sync("GET", {});
|
|
95
|
+
expect(sync.default).toHaveBeenCalledWith("GET", {});
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
describe(
|
|
99
|
-
it(
|
|
100
|
-
model = new Model({one:
|
|
98
|
+
describe("get", () => {
|
|
99
|
+
it("returns the value of the data at the given property", () => {
|
|
100
|
+
model = new Model({ one: "one", two: null });
|
|
101
101
|
|
|
102
|
-
expect(model.get(
|
|
103
|
-
expect(model.get(
|
|
102
|
+
expect(model.get("one")).toEqual("one");
|
|
103
|
+
expect(model.get("two")).toBe(null);
|
|
104
104
|
});
|
|
105
105
|
});
|
|
106
106
|
|
|
107
|
-
describe(
|
|
108
|
-
it(
|
|
109
|
-
model = new Model({one:
|
|
107
|
+
describe("has", () => {
|
|
108
|
+
it("returns the true if there is a defined, non-null value at the given property", () => {
|
|
109
|
+
model = new Model({ one: "one", two: null, three: undefined, four: 0 });
|
|
110
110
|
|
|
111
|
-
expect(model.has(
|
|
112
|
-
expect(model.has(
|
|
113
|
-
expect(model.has(
|
|
114
|
-
expect(model.has(
|
|
111
|
+
expect(model.has("one")).toBe(true);
|
|
112
|
+
expect(model.has("two")).toBe(false);
|
|
113
|
+
expect(model.has("three")).toBe(false);
|
|
114
|
+
expect(model.has("four")).toBe(true);
|
|
115
115
|
});
|
|
116
116
|
});
|
|
117
117
|
|
|
118
|
-
describe(
|
|
119
|
-
it(
|
|
120
|
-
model = new Model({one:
|
|
118
|
+
describe("pick", () => {
|
|
119
|
+
it("returns a subset of a model's attributes", () => {
|
|
120
|
+
model = new Model({ one: "one", two: null, three: undefined, four: 0 });
|
|
121
121
|
|
|
122
|
-
expect(model.pick(
|
|
122
|
+
expect(model.pick("one", "two", "five")).toEqual({ one: "one" });
|
|
123
123
|
});
|
|
124
124
|
});
|
|
125
125
|
|
|
126
|
-
describe(
|
|
127
|
-
it(
|
|
128
|
-
model = new Model().set({one:
|
|
129
|
-
expect(model.toJSON()).toEqual({one:
|
|
126
|
+
describe("set", () => {
|
|
127
|
+
it("sets new attributes on a model", () => {
|
|
128
|
+
model = new Model().set({ one: "one", two: 2 });
|
|
129
|
+
expect(model.toJSON()).toEqual({ one: "one", two: 2 });
|
|
130
130
|
});
|
|
131
131
|
|
|
132
|
-
describe(
|
|
132
|
+
describe("triggers an update", () => {
|
|
133
133
|
beforeEach(() => {
|
|
134
|
-
model = new Model({id:
|
|
134
|
+
model = new Model({ id: "1234", one: "one", two: { three: "four" } });
|
|
135
135
|
model.onUpdate(callback);
|
|
136
136
|
});
|
|
137
137
|
|
|
138
|
-
it(
|
|
139
|
-
model.set({one:
|
|
138
|
+
it("if a value has changed", () => {
|
|
139
|
+
model.set({ one: "one" });
|
|
140
140
|
expect(callback).not.toHaveBeenCalled();
|
|
141
141
|
|
|
142
|
-
model.set({two: {three:
|
|
142
|
+
model.set({ two: { three: "four" } });
|
|
143
143
|
expect(callback).not.toHaveBeenCalled();
|
|
144
144
|
|
|
145
|
-
model.set({one:
|
|
145
|
+
model.set({ one: "five" });
|
|
146
146
|
expect(callback).toHaveBeenCalled();
|
|
147
147
|
|
|
148
148
|
callback.mockClear();
|
|
149
|
-
model = new Model;
|
|
149
|
+
model = new Model();
|
|
150
150
|
model.set();
|
|
151
151
|
expect(callback).not.toHaveBeenCalled();
|
|
152
152
|
});
|
|
153
153
|
|
|
154
|
-
it(
|
|
155
|
-
model.set({one:
|
|
154
|
+
it("unless the `silent` option is passed", () => {
|
|
155
|
+
model.set({ one: "five" }, { silent: true });
|
|
156
156
|
expect(callback).not.toHaveBeenCalled();
|
|
157
157
|
});
|
|
158
158
|
});
|
|
159
159
|
|
|
160
|
-
it(
|
|
161
|
-
model = new Model({id:
|
|
162
|
-
collection = new Collection;
|
|
160
|
+
it("updates its id property, if appropriate, as well as that of the collection", () => {
|
|
161
|
+
model = new Model({ id: "1234", one: "one", two: { three: "four" } });
|
|
162
|
+
collection = new Collection();
|
|
163
163
|
|
|
164
|
-
expect(model.id).toEqual(
|
|
165
|
-
model.set({id:
|
|
166
|
-
expect(model.id).toEqual(
|
|
164
|
+
expect(model.id).toEqual("1234");
|
|
165
|
+
model.set({ id: "2345" });
|
|
166
|
+
expect(model.id).toEqual("2345");
|
|
167
167
|
|
|
168
|
-
model = new Model({id:
|
|
168
|
+
model = new Model({ id: "1234" });
|
|
169
169
|
collection.add(model);
|
|
170
|
-
expect(collection.has(
|
|
170
|
+
expect(collection.has("1234")).toBe(true);
|
|
171
171
|
|
|
172
|
-
model.set({id:
|
|
173
|
-
expect(collection.has(
|
|
174
|
-
expect(collection.has(
|
|
172
|
+
model.set({ id: "2345" });
|
|
173
|
+
expect(collection.has("1234")).toBe(false);
|
|
174
|
+
expect(collection.has("2345")).toBe(true);
|
|
175
175
|
});
|
|
176
176
|
});
|
|
177
177
|
|
|
178
|
-
describe(
|
|
179
|
-
it(
|
|
180
|
-
model = new Model({one:
|
|
178
|
+
describe("unset", () => {
|
|
179
|
+
it("removes an attribute from a model's data", () => {
|
|
180
|
+
model = new Model({ one: "one", two: null, three: undefined, four: 0 });
|
|
181
181
|
|
|
182
|
-
model.unset(
|
|
183
|
-
expect(model.toJSON()).toEqual({one:
|
|
182
|
+
model.unset("two");
|
|
183
|
+
expect(model.toJSON()).toEqual({ one: "one", three: undefined, four: 0 });
|
|
184
184
|
});
|
|
185
185
|
});
|
|
186
186
|
|
|
187
|
-
describe(
|
|
188
|
-
it(
|
|
189
|
-
model = new Model({one:
|
|
187
|
+
describe("clear", () => {
|
|
188
|
+
it("removes all attributes from a model", () => {
|
|
189
|
+
model = new Model({ one: "one", two: null, three: undefined, four: 0 });
|
|
190
190
|
|
|
191
191
|
model.clear();
|
|
192
192
|
expect(model.toJSON()).toEqual({});
|
|
193
193
|
});
|
|
194
194
|
});
|
|
195
195
|
|
|
196
|
-
describe(
|
|
196
|
+
describe("fetch", () => {
|
|
197
197
|
var response = {};
|
|
198
198
|
|
|
199
199
|
beforeEach(() => {
|
|
@@ -203,27 +203,24 @@ describe('Model', () => {
|
|
|
203
203
|
}
|
|
204
204
|
}
|
|
205
205
|
|
|
206
|
-
model = new _Model;
|
|
207
|
-
sync.default.mockResolvedValue([
|
|
208
|
-
{data: {one: 'one', two: 'two'}},
|
|
209
|
-
response
|
|
210
|
-
]);
|
|
206
|
+
model = new _Model();
|
|
207
|
+
sync.default.mockResolvedValue([{ data: { one: "one", two: "two" } }, response]);
|
|
211
208
|
});
|
|
212
209
|
|
|
213
|
-
it(
|
|
210
|
+
it("calls sync with a GET method", async () => {
|
|
214
211
|
await model.fetch();
|
|
215
212
|
|
|
216
|
-
expect(sync.default).toHaveBeenCalledWith(model, {method:
|
|
217
|
-
await model.fetch({method:
|
|
213
|
+
expect(sync.default).toHaveBeenCalledWith(model, { method: "GET", parse: true });
|
|
214
|
+
await model.fetch({ method: "POST", url: "/library" });
|
|
218
215
|
|
|
219
216
|
expect(sync.default).toHaveBeenCalledWith(model, {
|
|
220
|
-
method:
|
|
217
|
+
method: "POST",
|
|
221
218
|
parse: true,
|
|
222
|
-
url:
|
|
219
|
+
url: "/library",
|
|
223
220
|
});
|
|
224
221
|
});
|
|
225
222
|
|
|
226
|
-
it(
|
|
223
|
+
it("triggers an update on returning", async () => {
|
|
227
224
|
var request;
|
|
228
225
|
|
|
229
226
|
model.onUpdate(callback);
|
|
@@ -232,20 +229,20 @@ describe('Model', () => {
|
|
|
232
229
|
|
|
233
230
|
await request;
|
|
234
231
|
expect(callback).toHaveBeenCalled();
|
|
235
|
-
expect(model.toJSON()).toEqual({one:
|
|
232
|
+
expect(model.toJSON()).toEqual({ one: "one", two: "two" });
|
|
236
233
|
model.clear();
|
|
237
234
|
callback.mockClear();
|
|
238
235
|
|
|
239
|
-
await model.fetch({parse: false});
|
|
236
|
+
await model.fetch({ parse: false });
|
|
240
237
|
expect(callback).toHaveBeenCalled();
|
|
241
|
-
expect(model.toJSON()).toEqual({data: {one:
|
|
238
|
+
expect(model.toJSON()).toEqual({ data: { one: "one", two: "two" } });
|
|
242
239
|
});
|
|
243
240
|
|
|
244
|
-
it(
|
|
241
|
+
it("resolves a tuple of the model instance and repsonse object", async () => {
|
|
245
242
|
expect(await model.fetch()).toEqual([model, response]);
|
|
246
243
|
});
|
|
247
244
|
|
|
248
|
-
it(
|
|
245
|
+
it("rejects the response", async () => {
|
|
249
246
|
sync.default.mockRejectedValue(response);
|
|
250
247
|
|
|
251
248
|
try {
|
|
@@ -256,7 +253,7 @@ describe('Model', () => {
|
|
|
256
253
|
});
|
|
257
254
|
});
|
|
258
255
|
|
|
259
|
-
describe(
|
|
256
|
+
describe("save", () => {
|
|
260
257
|
var response = {};
|
|
261
258
|
|
|
262
259
|
class _Model extends Model {
|
|
@@ -266,110 +263,109 @@ describe('Model', () => {
|
|
|
266
263
|
}
|
|
267
264
|
|
|
268
265
|
beforeEach(() => {
|
|
269
|
-
model = new _Model({one:
|
|
270
|
-
sync.default.mockResolvedValue([
|
|
271
|
-
{data: {one: 'one', two: 'two'}},
|
|
272
|
-
response
|
|
273
|
-
]);
|
|
266
|
+
model = new _Model({ one: "one" });
|
|
267
|
+
sync.default.mockResolvedValue([{ data: { one: "one", two: "two" } }, response]);
|
|
274
268
|
});
|
|
275
269
|
|
|
276
|
-
it(
|
|
270
|
+
it("calls sync with a write method", async () => {
|
|
277
271
|
await model.save();
|
|
278
272
|
|
|
279
|
-
expect(sync.default).toHaveBeenCalledWith(model, {parse: true, method:
|
|
273
|
+
expect(sync.default).toHaveBeenCalledWith(model, { parse: true, method: "POST" });
|
|
280
274
|
|
|
281
|
-
model = new _Model({id:
|
|
282
|
-
await model.save({one:
|
|
283
|
-
expect(sync.default).toHaveBeenCalledWith(model, {parse: true, method:
|
|
284
|
-
expect(model.get(
|
|
275
|
+
model = new _Model({ id: "1234" });
|
|
276
|
+
await model.save({ one: "one" });
|
|
277
|
+
expect(sync.default).toHaveBeenCalledWith(model, { parse: true, method: "PUT" });
|
|
278
|
+
expect(model.get("one")).toEqual("one");
|
|
285
279
|
|
|
286
|
-
model = new _Model({id:
|
|
287
|
-
await model.save({one:
|
|
288
|
-
expect(sync.default).toHaveBeenCalledWith(
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
280
|
+
model = new _Model({ id: "1234" });
|
|
281
|
+
await model.save({ one: "one" }, { patch: true });
|
|
282
|
+
expect(sync.default).toHaveBeenCalledWith(model, {
|
|
283
|
+
parse: true,
|
|
284
|
+
patch: true,
|
|
285
|
+
method: "PATCH",
|
|
286
|
+
attrs: { one: "one" },
|
|
287
|
+
});
|
|
292
288
|
});
|
|
293
289
|
|
|
294
|
-
it(
|
|
290
|
+
it("parses results unless `parse` is set to false", async () => {
|
|
295
291
|
var result;
|
|
296
292
|
|
|
297
293
|
model.onUpdate(callback);
|
|
298
294
|
|
|
299
295
|
result = await model.save();
|
|
300
|
-
expect(model.toJSON()).toEqual({one:
|
|
296
|
+
expect(model.toJSON()).toEqual({ one: "one", two: "two" });
|
|
301
297
|
expect(result).toEqual([model, response]);
|
|
302
298
|
|
|
303
299
|
model.clear();
|
|
304
300
|
callback.mockClear();
|
|
305
301
|
|
|
306
|
-
await model.save({three:
|
|
307
|
-
expect(model.toJSON()).toEqual({data: {one:
|
|
302
|
+
await model.save({ three: "three" }, { parse: false });
|
|
303
|
+
expect(model.toJSON()).toEqual({ data: { one: "one", two: "two" }, three: "three" });
|
|
308
304
|
expect(callback).toHaveBeenCalledTimes(2);
|
|
309
305
|
});
|
|
310
306
|
|
|
311
|
-
it(
|
|
307
|
+
it("does not set attributes until after the request returns when `wait` is true", async () => {
|
|
312
308
|
var result;
|
|
313
309
|
|
|
314
310
|
model.onUpdate(callback);
|
|
315
311
|
|
|
316
|
-
result = model.save({three:
|
|
312
|
+
result = model.save({ three: "three" }, { wait: true });
|
|
317
313
|
expect(callback).not.toHaveBeenCalled();
|
|
318
|
-
expect(model.toJSON()).toEqual({one:
|
|
314
|
+
expect(model.toJSON()).toEqual({ one: "one" });
|
|
319
315
|
|
|
320
316
|
await result;
|
|
321
317
|
|
|
322
318
|
expect(callback).toHaveBeenCalled();
|
|
323
|
-
expect(model.toJSON()).toEqual({one:
|
|
319
|
+
expect(model.toJSON()).toEqual({ one: "one", two: "two", three: "three" });
|
|
324
320
|
});
|
|
325
321
|
|
|
326
|
-
it(
|
|
322
|
+
it("restores previous attributes if error", async () => {
|
|
327
323
|
sync.default.mockRejectedValue(response);
|
|
328
324
|
|
|
329
325
|
try {
|
|
330
|
-
await model.save({one:
|
|
326
|
+
await model.save({ one: "two", two: "three" });
|
|
331
327
|
} catch (err) {
|
|
332
328
|
expect(err).toEqual(response);
|
|
333
|
-
expect(model.toJSON()).toEqual({one:
|
|
329
|
+
expect(model.toJSON()).toEqual({ one: "one" });
|
|
334
330
|
}
|
|
335
331
|
|
|
336
332
|
try {
|
|
337
|
-
await model.save({one:
|
|
333
|
+
await model.save({ one: "two", two: "three" }, { wait: true });
|
|
338
334
|
} catch (err) {
|
|
339
335
|
expect(err).toEqual(response);
|
|
340
|
-
expect(model.toJSON()).toEqual({one:
|
|
336
|
+
expect(model.toJSON()).toEqual({ one: "one" });
|
|
341
337
|
}
|
|
342
338
|
});
|
|
343
339
|
});
|
|
344
340
|
|
|
345
|
-
describe(
|
|
341
|
+
describe("destroy", () => {
|
|
346
342
|
var response = {};
|
|
347
343
|
|
|
348
344
|
beforeEach(() => {
|
|
349
|
-
model = new Model({id:
|
|
345
|
+
model = new Model({ id: "1234" });
|
|
350
346
|
sync.default.mockResolvedValue([undefined, response]);
|
|
351
347
|
});
|
|
352
348
|
|
|
353
|
-
it(
|
|
349
|
+
it("resolves immediately if new", async () => {
|
|
354
350
|
model = new Model();
|
|
355
351
|
|
|
356
352
|
expect(await model.destroy()).toEqual([model, undefined]);
|
|
357
353
|
});
|
|
358
354
|
|
|
359
|
-
it(
|
|
355
|
+
it("calls sync with a DELETE method", async () => {
|
|
360
356
|
await model.destroy();
|
|
361
357
|
|
|
362
|
-
expect(sync.default).toHaveBeenCalledWith(model, {method:
|
|
363
|
-
await model.destroy({url:
|
|
358
|
+
expect(sync.default).toHaveBeenCalledWith(model, { method: "DELETE" });
|
|
359
|
+
await model.destroy({ url: "/library" });
|
|
364
360
|
|
|
365
|
-
expect(sync.default).toHaveBeenCalledWith(model, {method:
|
|
361
|
+
expect(sync.default).toHaveBeenCalledWith(model, { method: "DELETE", url: "/library" });
|
|
366
362
|
});
|
|
367
363
|
|
|
368
|
-
it(
|
|
364
|
+
it("resolves a tuple of the model instance and repsonse object", async () => {
|
|
369
365
|
expect(await model.destroy()).toEqual([model, response]);
|
|
370
366
|
});
|
|
371
367
|
|
|
372
|
-
it(
|
|
368
|
+
it("rejects the response", async () => {
|
|
373
369
|
sync.default.mockRejectedValue(response);
|
|
374
370
|
|
|
375
371
|
try {
|
|
@@ -379,7 +375,7 @@ describe('Model', () => {
|
|
|
379
375
|
}
|
|
380
376
|
});
|
|
381
377
|
|
|
382
|
-
it(
|
|
378
|
+
it("updates immediately if `wait` is false", async () => {
|
|
383
379
|
var request;
|
|
384
380
|
|
|
385
381
|
collection = new Collection([model]);
|
|
@@ -393,14 +389,14 @@ describe('Model', () => {
|
|
|
393
389
|
await request;
|
|
394
390
|
});
|
|
395
391
|
|
|
396
|
-
it(
|
|
392
|
+
it("updates when the promise resolves if `wait` is true", async () => {
|
|
397
393
|
var request;
|
|
398
394
|
|
|
399
395
|
collection = new Collection([model]);
|
|
400
396
|
model.onUpdate(callback);
|
|
401
397
|
expect(collection.has(model.id)).toBe(true);
|
|
402
398
|
|
|
403
|
-
request = model.destroy({wait: true});
|
|
399
|
+
request = model.destroy({ wait: true });
|
|
404
400
|
|
|
405
401
|
expect(callback).not.toHaveBeenCalled();
|
|
406
402
|
expect(collection.has(model.id)).toBe(true);
|
|
@@ -410,112 +406,111 @@ describe('Model', () => {
|
|
|
410
406
|
expect(collection.has(model.id)).toBe(false);
|
|
411
407
|
});
|
|
412
408
|
|
|
413
|
-
it(
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
collectionCallback = jest.fn();
|
|
409
|
+
it("adds the model back to its collection when the promise rejects if `wait` is false", async () => {
|
|
410
|
+
var request,
|
|
411
|
+
collectionCallback = vi.fn();
|
|
417
412
|
|
|
418
|
-
|
|
413
|
+
sync.default.mockRejectedValue(response);
|
|
419
414
|
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
415
|
+
collection = new Collection([model]);
|
|
416
|
+
collection.onUpdate(collectionCallback);
|
|
417
|
+
model.onUpdate(callback);
|
|
423
418
|
|
|
424
|
-
|
|
419
|
+
expect(collection.has(model.id)).toBe(true);
|
|
425
420
|
|
|
426
|
-
|
|
421
|
+
request = model.destroy();
|
|
427
422
|
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
try {
|
|
433
|
-
await request;
|
|
434
|
-
} catch (err) {
|
|
435
|
-
expect(collectionCallback).toHaveBeenCalledTimes(2);
|
|
436
|
-
expect(collection.has(model.id)).toBe(true);
|
|
437
|
-
expect(err).toEqual(response);
|
|
438
|
-
}
|
|
423
|
+
expect(callback).toHaveBeenCalled();
|
|
424
|
+
expect(collectionCallback).toHaveBeenCalled();
|
|
425
|
+
expect(collection.has(model.id)).toBe(false);
|
|
439
426
|
|
|
440
|
-
|
|
427
|
+
try {
|
|
428
|
+
await request;
|
|
429
|
+
} catch (err) {
|
|
430
|
+
expect(collectionCallback).toHaveBeenCalledTimes(2);
|
|
431
|
+
expect(collection.has(model.id)).toBe(true);
|
|
432
|
+
expect(err).toEqual(response);
|
|
433
|
+
}
|
|
441
434
|
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
435
|
+
collectionCallback.mockClear();
|
|
436
|
+
|
|
437
|
+
try {
|
|
438
|
+
await model.destroy({ wait: true });
|
|
439
|
+
} catch (err) {
|
|
440
|
+
expect(collectionCallback).not.toHaveBeenCalled();
|
|
441
|
+
expect(collection.has(model.id)).toBe(true);
|
|
442
|
+
expect(err).toEqual(response);
|
|
443
|
+
}
|
|
444
|
+
});
|
|
450
445
|
});
|
|
451
446
|
|
|
452
|
-
describe(
|
|
453
|
-
it(
|
|
454
|
-
model = new Model;
|
|
447
|
+
describe("url", () => {
|
|
448
|
+
it("throws if there is no overridden url property and the model is not in a collection", () => {
|
|
449
|
+
model = new Model();
|
|
455
450
|
|
|
456
451
|
expect(() => model.url()).toThrowError('A "url" property or function must be specified');
|
|
457
|
-
model.urlRoot =
|
|
458
|
-
expect(model.url()).toEqual(
|
|
452
|
+
model.urlRoot = "/library";
|
|
453
|
+
expect(model.url()).toEqual("/library");
|
|
459
454
|
|
|
460
455
|
collection = new Collection();
|
|
461
|
-
collection.url =
|
|
462
|
-
model = new Model({}, {collection});
|
|
463
|
-
expect(model.url()).toEqual(
|
|
456
|
+
collection.url = "/library";
|
|
457
|
+
model = new Model({}, { collection });
|
|
458
|
+
expect(model.url()).toEqual("/library");
|
|
464
459
|
});
|
|
465
460
|
|
|
466
|
-
it(
|
|
461
|
+
it("appends its id to the url if not new", () => {
|
|
467
462
|
class _Model extends Model {
|
|
468
|
-
static idAttribute =
|
|
463
|
+
static idAttribute = "name";
|
|
469
464
|
}
|
|
470
465
|
|
|
471
|
-
model = new _Model({name:
|
|
472
|
-
model.urlRoot = () =>
|
|
473
|
-
expect(model.url()).toEqual(
|
|
466
|
+
model = new _Model({ name: "noah?grant" });
|
|
467
|
+
model.urlRoot = () => "/library";
|
|
468
|
+
expect(model.url()).toEqual("/library/noah%3Fgrant");
|
|
474
469
|
|
|
475
470
|
collection = new Collection();
|
|
476
|
-
collection.url = () =>
|
|
477
|
-
model = new _Model({name:
|
|
478
|
-
expect(model.url()).toEqual(
|
|
471
|
+
collection.url = () => "/library";
|
|
472
|
+
model = new _Model({ name: "noah?grant" }, { collection });
|
|
473
|
+
expect(model.url()).toEqual("/library/noah%3Fgrant");
|
|
479
474
|
});
|
|
480
475
|
|
|
481
|
-
it(
|
|
476
|
+
it("is called by default with its urlOptions", () => {
|
|
482
477
|
class _Model extends Model {
|
|
483
|
-
urlRoot({section}) {
|
|
478
|
+
urlRoot({ section }) {
|
|
484
479
|
return `/library/${section}`;
|
|
485
480
|
}
|
|
486
481
|
}
|
|
487
482
|
|
|
488
|
-
model = new _Model({}, {section:
|
|
489
|
-
expect(model.url()).toEqual(
|
|
483
|
+
model = new _Model({}, { section: "nature" });
|
|
484
|
+
expect(model.url()).toEqual("/library/nature");
|
|
490
485
|
|
|
491
486
|
collection = new Collection();
|
|
492
|
-
collection.url = ({section}) => `/library/${section}`;
|
|
493
|
-
model = new Model({}, {collection, section:
|
|
494
|
-
expect(model.url()).toEqual(
|
|
487
|
+
collection.url = ({ section }) => `/library/${section}`;
|
|
488
|
+
model = new Model({}, { collection, section: "history" });
|
|
489
|
+
expect(model.url()).toEqual("/library/history");
|
|
495
490
|
});
|
|
496
491
|
});
|
|
497
492
|
|
|
498
|
-
describe(
|
|
499
|
-
it(
|
|
493
|
+
describe("parse", () => {
|
|
494
|
+
it("is by default the identity function", () => {
|
|
500
495
|
model = new Model();
|
|
501
|
-
expect(model.parse({foo:
|
|
496
|
+
expect(model.parse({ foo: "bar" })).toEqual({ foo: "bar" });
|
|
502
497
|
});
|
|
503
498
|
});
|
|
504
499
|
|
|
505
|
-
describe(
|
|
506
|
-
it(
|
|
500
|
+
describe("isNew", () => {
|
|
501
|
+
it("returns true if the model has an id attribute", () => {
|
|
507
502
|
class _Model extends Model {
|
|
508
|
-
static idAttribute =
|
|
503
|
+
static idAttribute = "name";
|
|
509
504
|
}
|
|
510
505
|
|
|
511
|
-
model = new Model;
|
|
506
|
+
model = new Model();
|
|
512
507
|
expect(model.isNew()).toBe(true);
|
|
513
|
-
model.set({id:
|
|
508
|
+
model.set({ id: "1234" });
|
|
514
509
|
expect(model.isNew()).toBe(false);
|
|
515
510
|
|
|
516
|
-
model = new _Model({id:
|
|
511
|
+
model = new _Model({ id: "1234" });
|
|
517
512
|
expect(model.isNew()).toBe(true);
|
|
518
|
-
model.set({name:
|
|
513
|
+
model.set({ name: "noah" });
|
|
519
514
|
expect(model.isNew()).toBe(false);
|
|
520
515
|
});
|
|
521
516
|
});
|