resourcerer 1.2.2 → 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 +138 -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 +132 -107
- package/test/utils.test.js +173 -159
- package/index.d.ts +0 -269
- 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 -963
- package/lib/sync.js +0 -94
- package/lib/utils.js +0 -220
- package/test/model-mocks.js +0 -110
- package/test/setup.js +0 -63
package/test/prefetch.test.js
CHANGED
|
@@ -1,101 +1,75 @@
|
|
|
1
|
-
import * as Request from
|
|
2
|
-
import {DecisionsCollection, UserModel} from
|
|
3
|
-
|
|
4
|
-
import Collection from
|
|
5
|
-
import prefetch from
|
|
6
|
-
import ReactDOM from
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
1
|
+
import * as Request from "../lib/request";
|
|
2
|
+
import { DecisionsCollection, UserModel } from "./model-mocks";
|
|
3
|
+
|
|
4
|
+
import Collection from "../lib/collection";
|
|
5
|
+
import prefetch from "../lib/prefetch";
|
|
6
|
+
import ReactDOM from "react-dom";
|
|
7
|
+
import { vi } from "vitest";
|
|
8
|
+
|
|
9
|
+
const renderNode = document.createElement("div");
|
|
10
|
+
const getResources = (props) => ({
|
|
11
|
+
user: {
|
|
12
|
+
params: { home: props.home, source: props.source },
|
|
13
|
+
path: { userId: props.userId },
|
|
13
14
|
},
|
|
14
|
-
|
|
15
|
+
decisions: {},
|
|
15
16
|
});
|
|
16
|
-
const expectedProps = {userId:
|
|
17
|
-
const dummyEvt = {target: renderNode};
|
|
17
|
+
const expectedProps = { userId: "noah", home: "sf", source: "hbase" };
|
|
18
|
+
const dummyEvt = { target: renderNode };
|
|
18
19
|
|
|
19
|
-
describe(
|
|
20
|
+
describe("prefetch", () => {
|
|
20
21
|
beforeEach(() => {
|
|
21
22
|
document.body.appendChild(renderNode);
|
|
22
|
-
|
|
23
|
-
|
|
23
|
+
vi.spyOn(Request, "default").mockResolvedValue(new Collection([]));
|
|
24
|
+
vi.useFakeTimers();
|
|
24
25
|
});
|
|
25
26
|
|
|
26
27
|
afterEach(() => {
|
|
27
28
|
ReactDOM.unmountComponentAtNode(renderNode);
|
|
28
29
|
Request.default.mockRestore();
|
|
29
30
|
renderNode.remove();
|
|
30
|
-
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
it('correctly turns the config object into cache key, params, and options', () => {
|
|
34
|
-
var oldFields = UserModel.dependencies;
|
|
35
|
-
|
|
36
|
-
UserModel.dependencies = ['userId', 'source'];
|
|
37
|
-
|
|
38
|
-
prefetch(getResources, expectedProps)(dummyEvt);
|
|
39
|
-
jest.advanceTimersByTime(100);
|
|
40
|
-
|
|
41
|
-
expect(Request.default.mock.calls[0][0]).toEqual('usersource=hbase_userId=noah');
|
|
42
|
-
expect(Request.default.mock.calls[0][1]).toEqual(UserModel);
|
|
43
|
-
expect(Request.default.mock.calls[0][2]).toEqual({
|
|
44
|
-
options: {userId: 'noah'},
|
|
45
|
-
params: {home: 'sf', source: 'hbase'},
|
|
46
|
-
prefetch: true
|
|
47
|
-
});
|
|
48
|
-
|
|
49
|
-
expect(Request.default.mock.calls[1][0]).toEqual('decisions');
|
|
50
|
-
expect(Request.default.mock.calls[1][1]).toEqual(DecisionsCollection);
|
|
51
|
-
expect(Request.default.mock.calls[1][2]).toEqual({prefetch: true});
|
|
52
|
-
|
|
53
|
-
expect(() => prefetch(() => false)({})).not.toThrow();
|
|
54
|
-
UserModel.dependencies = oldFields;
|
|
31
|
+
vi.useRealTimers();
|
|
55
32
|
});
|
|
56
33
|
|
|
57
|
-
it(
|
|
34
|
+
it("correctly turns the config object into cache key, params, and options", () => {
|
|
58
35
|
var oldFields = UserModel.dependencies;
|
|
59
36
|
|
|
60
|
-
UserModel.dependencies = [];
|
|
61
|
-
UserModel.cacheFields = ['userId', 'source'];
|
|
37
|
+
UserModel.dependencies = ["userId", "source"];
|
|
62
38
|
|
|
63
39
|
prefetch(getResources, expectedProps)(dummyEvt);
|
|
64
|
-
|
|
40
|
+
vi.advanceTimersByTime(100);
|
|
65
41
|
|
|
66
|
-
expect(Request.default.mock.calls[0][0]).toEqual(
|
|
42
|
+
expect(Request.default.mock.calls[0][0]).toEqual("user~source=hbase_userId=noah");
|
|
67
43
|
expect(Request.default.mock.calls[0][1]).toEqual(UserModel);
|
|
68
44
|
expect(Request.default.mock.calls[0][2]).toEqual({
|
|
69
|
-
|
|
70
|
-
params: {home:
|
|
71
|
-
prefetch: true
|
|
45
|
+
path: { userId: "noah" },
|
|
46
|
+
params: { home: "sf", source: "hbase" },
|
|
72
47
|
});
|
|
73
48
|
|
|
74
|
-
expect(Request.default.mock.calls[1][0]).toEqual(
|
|
49
|
+
expect(Request.default.mock.calls[1][0]).toEqual("decisions");
|
|
75
50
|
expect(Request.default.mock.calls[1][1]).toEqual(DecisionsCollection);
|
|
76
|
-
expect(Request.default.mock.calls[1][2]).toEqual({
|
|
51
|
+
expect(Request.default.mock.calls[1][2]).toEqual({});
|
|
77
52
|
|
|
78
53
|
expect(() => prefetch(() => false)({})).not.toThrow();
|
|
79
|
-
UserModel.cacheFields = [];
|
|
80
54
|
UserModel.dependencies = oldFields;
|
|
81
55
|
});
|
|
82
56
|
|
|
83
|
-
it(
|
|
84
|
-
var leaveEvt = new Event(
|
|
57
|
+
it("will fire if the user hovers over the element for longer than the timeout", () => {
|
|
58
|
+
var leaveEvt = new Event("mouseleave");
|
|
85
59
|
|
|
86
60
|
prefetch(getResources, expectedProps)(dummyEvt);
|
|
87
|
-
|
|
61
|
+
vi.advanceTimersByTime(50);
|
|
88
62
|
renderNode.dispatchEvent(leaveEvt);
|
|
89
63
|
expect(Request.default).toHaveBeenCalled();
|
|
90
64
|
});
|
|
91
65
|
|
|
92
|
-
it(
|
|
93
|
-
var leaveEvt = new Event(
|
|
66
|
+
it("will not fetch if the user leaves the element before the timeout", () => {
|
|
67
|
+
var leaveEvt = new Event("mouseleave");
|
|
94
68
|
|
|
95
69
|
prefetch(getResources, expectedProps)(dummyEvt);
|
|
96
|
-
|
|
70
|
+
vi.advanceTimersByTime(25);
|
|
97
71
|
renderNode.dispatchEvent(leaveEvt);
|
|
98
|
-
|
|
72
|
+
vi.advanceTimersByTime(25);
|
|
99
73
|
expect(Request.default).not.toHaveBeenCalled();
|
|
100
74
|
});
|
|
101
75
|
});
|
package/test/request.test.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
import request, {existsInCache, getFromCache} from
|
|
1
|
+
import request, { existsInCache, getFromCache } from "../lib/request";
|
|
2
2
|
|
|
3
|
-
import Model from
|
|
4
|
-
import ModelCache from
|
|
5
|
-
import {waitsFor} from
|
|
3
|
+
import Model from "../lib/model";
|
|
4
|
+
import ModelCache from "../lib/model-cache";
|
|
5
|
+
import { waitsFor } from "./test-utils";
|
|
6
|
+
import { vi } from "vitest";
|
|
6
7
|
|
|
7
8
|
const component = {};
|
|
8
9
|
const CACHE_WAIT = 150000;
|
|
9
10
|
|
|
10
11
|
/* eslint-disable max-nested-callbacks */
|
|
11
|
-
describe(
|
|
12
|
-
var waitSuccess,
|
|
13
|
-
reject;
|
|
12
|
+
describe("Request", () => {
|
|
13
|
+
var waitSuccess, reject;
|
|
14
14
|
|
|
15
15
|
beforeEach(() => {
|
|
16
|
-
|
|
16
|
+
vi.spyOn(Model.prototype, "fetch").mockImplementation(function (options) {
|
|
17
17
|
if (waitSuccess) {
|
|
18
18
|
// use this to ensure that a model gets removed from the loadingCache
|
|
19
19
|
// before other synchronous actions take place
|
|
20
20
|
return new Promise((res, rej) => {
|
|
21
21
|
window.requestAnimationFrame(() => {
|
|
22
22
|
if (reject) {
|
|
23
|
-
rej({status: 404});
|
|
23
|
+
rej({ status: 404 });
|
|
24
24
|
} else {
|
|
25
|
-
res([this, {status: 200}]);
|
|
25
|
+
res([this, { status: 200 }]);
|
|
26
26
|
}
|
|
27
27
|
});
|
|
28
28
|
});
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
return Promise.resolve([this, {status: 200}]);
|
|
31
|
+
return Promise.resolve([this, { status: 200 }]);
|
|
32
32
|
});
|
|
33
33
|
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
vi.spyOn(ModelCache, "put");
|
|
35
|
+
vi.spyOn(ModelCache, "register");
|
|
36
36
|
});
|
|
37
37
|
|
|
38
38
|
afterEach(() => {
|
|
@@ -44,18 +44,18 @@ describe('Request', () => {
|
|
|
44
44
|
unregisterComponent(component);
|
|
45
45
|
});
|
|
46
46
|
|
|
47
|
-
it(
|
|
48
|
-
expect(existsInCache(
|
|
49
|
-
ModelCache.put(
|
|
50
|
-
expect(existsInCache(
|
|
47
|
+
it("when calling 'existsInCache' returns true if the model exists in the model cache", () => {
|
|
48
|
+
expect(existsInCache("foo")).toBe(false);
|
|
49
|
+
ModelCache.put("foo", new Model(), component);
|
|
50
|
+
expect(existsInCache("foo")).toBe(true);
|
|
51
51
|
});
|
|
52
52
|
|
|
53
|
-
describe(
|
|
54
|
-
it(
|
|
53
|
+
describe("when using the default exported function", () => {
|
|
54
|
+
it("returns a promise if the model does not exist", async () => {
|
|
55
55
|
var model,
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
promise = request("foo", Model, { options: { one: 1 }, component }).then(([_model]) => {
|
|
57
|
+
model = _model;
|
|
58
|
+
});
|
|
59
59
|
|
|
60
60
|
await waitsFor(() => model instanceof Model);
|
|
61
61
|
|
|
@@ -63,105 +63,102 @@ describe('Request', () => {
|
|
|
63
63
|
// in this instance we're resolving immediately, so model should be Model
|
|
64
64
|
expect(model instanceof Model).toBe(true);
|
|
65
65
|
// this tests that it properly passes along the options object to the model constructor
|
|
66
|
-
expect(model.urlOptions).toEqual({one: 1});
|
|
66
|
+
expect(model.urlOptions).toEqual({ one: 1 });
|
|
67
67
|
});
|
|
68
68
|
|
|
69
|
-
describe(
|
|
70
|
-
it(
|
|
69
|
+
describe("if the model does exist", () => {
|
|
70
|
+
it("calls the resolve immediately", async () => {
|
|
71
71
|
var model;
|
|
72
72
|
|
|
73
73
|
// put it in the cache
|
|
74
|
-
request(
|
|
74
|
+
request("foo", Model, { component });
|
|
75
75
|
// now call it again
|
|
76
|
-
request(
|
|
76
|
+
request("foo").then(([_model]) => (model = _model));
|
|
77
77
|
|
|
78
78
|
await waitsFor(() => model instanceof Model);
|
|
79
79
|
|
|
80
80
|
expect(model instanceof Model).toBe(true);
|
|
81
81
|
});
|
|
82
82
|
|
|
83
|
-
it(
|
|
83
|
+
it("registers the component if passed one", async () => {
|
|
84
84
|
waitSuccess = true;
|
|
85
85
|
// put it in the cache
|
|
86
|
-
await request(
|
|
86
|
+
await request("newModel", Model, { component });
|
|
87
87
|
|
|
88
88
|
expect(ModelCache.register).toHaveBeenCalled();
|
|
89
89
|
waitSuccess = null;
|
|
90
90
|
// now call it again, since we already have it in the cache
|
|
91
|
-
request(
|
|
91
|
+
request("newModel", Model, { component });
|
|
92
92
|
// no new fetch, but a new register call
|
|
93
93
|
expect(Model.prototype.fetch.mock.calls.length).toEqual(1);
|
|
94
94
|
expect(ModelCache.register.mock.calls.length).toEqual(2);
|
|
95
95
|
});
|
|
96
96
|
});
|
|
97
97
|
|
|
98
|
-
it(
|
|
98
|
+
it("puts a model in the ModelCache", async () => {
|
|
99
99
|
var modelRequest;
|
|
100
100
|
|
|
101
101
|
waitSuccess = true;
|
|
102
|
-
modelRequest = request(
|
|
102
|
+
modelRequest = request("newModel", Model, { component });
|
|
103
103
|
|
|
104
|
-
expect(existsInCache(
|
|
104
|
+
expect(existsInCache("newModel")).toBe(false);
|
|
105
105
|
expect(ModelCache.put).not.toHaveBeenCalled();
|
|
106
106
|
|
|
107
107
|
await modelRequest;
|
|
108
|
-
expect(existsInCache(
|
|
109
|
-
expect(ModelCache.get(
|
|
108
|
+
expect(existsInCache("newModel")).toBe(true);
|
|
109
|
+
expect(ModelCache.get("newModel")).toBeDefined();
|
|
110
110
|
expect(ModelCache.put).toHaveBeenCalled();
|
|
111
111
|
|
|
112
112
|
unregisterComponent(component);
|
|
113
113
|
|
|
114
|
-
modelRequest = request(
|
|
115
|
-
expect(existsInCache(
|
|
116
|
-
expect(ModelCache.get(
|
|
114
|
+
modelRequest = request("newModel2", Model, { prefetch: true });
|
|
115
|
+
expect(existsInCache("newModel2")).toBe(false);
|
|
116
|
+
expect(ModelCache.get("newModel2")).not.toBeDefined();
|
|
117
117
|
expect(ModelCache.put.mock.calls.length).toEqual(1);
|
|
118
118
|
|
|
119
119
|
await modelRequest;
|
|
120
|
-
expect(existsInCache(
|
|
121
|
-
expect(ModelCache.get(
|
|
120
|
+
expect(existsInCache("newModel2")).toBe(true);
|
|
121
|
+
expect(ModelCache.get("newModel2")).toBeDefined();
|
|
122
122
|
expect(ModelCache.put.mock.calls.length).toEqual(2);
|
|
123
123
|
// haven't called register since no component was passed
|
|
124
124
|
expect(ModelCache.register.mock.calls.length).toEqual(1);
|
|
125
125
|
});
|
|
126
126
|
|
|
127
|
-
describe(
|
|
128
|
-
it(
|
|
129
|
-
var [model] = await request(
|
|
127
|
+
describe("if the 'fetch' option is false", () => {
|
|
128
|
+
it("calls the resolve immediately", async () => {
|
|
129
|
+
var [model] = await request("nofetch", Model, { component, fetch: false });
|
|
130
130
|
|
|
131
131
|
await waitsFor(() => model instanceof Model);
|
|
132
132
|
|
|
133
133
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
134
134
|
});
|
|
135
135
|
|
|
136
|
-
it(
|
|
136
|
+
it("puts a model in the ModelCache", async () => {
|
|
137
137
|
var model;
|
|
138
138
|
|
|
139
|
-
expect(ModelCache.get(
|
|
140
|
-
[model] = await request(
|
|
139
|
+
expect(ModelCache.get("nofetch")).not.toBeDefined();
|
|
140
|
+
[model] = await request("nofetch", Model, { component, fetch: false });
|
|
141
141
|
|
|
142
142
|
await waitsFor(() => model instanceof Model);
|
|
143
143
|
|
|
144
|
-
expect(ModelCache.get(
|
|
144
|
+
expect(ModelCache.get("nofetch")).toBeDefined();
|
|
145
145
|
expect(ModelCache.put).toHaveBeenCalled();
|
|
146
146
|
|
|
147
147
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
148
148
|
});
|
|
149
149
|
});
|
|
150
150
|
|
|
151
|
-
describe(
|
|
152
|
-
var promise,
|
|
153
|
-
promise2,
|
|
154
|
-
thenSpy1,
|
|
155
|
-
thenSpy2;
|
|
151
|
+
describe("if called successively before the model returns", () => {
|
|
152
|
+
var promise, promise2, thenSpy1, thenSpy2;
|
|
156
153
|
|
|
157
154
|
beforeEach(() => {
|
|
158
155
|
waitSuccess = true;
|
|
159
|
-
thenSpy1 =
|
|
160
|
-
thenSpy2 =
|
|
156
|
+
thenSpy1 = vi.fn();
|
|
157
|
+
thenSpy2 = vi.fn();
|
|
161
158
|
|
|
162
|
-
promise = request(
|
|
159
|
+
promise = request("foo", Model);
|
|
163
160
|
// call it again
|
|
164
|
-
promise2 = request(
|
|
161
|
+
promise2 = request("foo", Model);
|
|
165
162
|
});
|
|
166
163
|
|
|
167
164
|
afterEach(() => {
|
|
@@ -169,12 +166,11 @@ describe('Request', () => {
|
|
|
169
166
|
promise2 = null;
|
|
170
167
|
});
|
|
171
168
|
|
|
172
|
-
it(
|
|
173
|
-
var m1,
|
|
174
|
-
m2;
|
|
169
|
+
it("returns the unfulfilled promise", async () => {
|
|
170
|
+
var m1, m2;
|
|
175
171
|
|
|
176
|
-
promise.then(([model]) => m1 = model);
|
|
177
|
-
promise2.then(([model]) => m2 = model);
|
|
172
|
+
promise.then(([model]) => (m1 = model));
|
|
173
|
+
promise2.then(([model]) => (m2 = model));
|
|
178
174
|
|
|
179
175
|
await waitsFor(() => m1 instanceof Model);
|
|
180
176
|
|
|
@@ -184,7 +180,7 @@ describe('Request', () => {
|
|
|
184
180
|
expect(Reflect.ownKeys(promise)[0]).toEqual(Reflect.ownKeys(promise2)[0]);
|
|
185
181
|
});
|
|
186
182
|
|
|
187
|
-
it(
|
|
183
|
+
it("executes both resolve handlers", async () => {
|
|
188
184
|
promise.then(thenSpy1);
|
|
189
185
|
promise2.then(thenSpy2);
|
|
190
186
|
|
|
@@ -194,27 +190,27 @@ describe('Request', () => {
|
|
|
194
190
|
expect(thenSpy2).toHaveBeenCalled();
|
|
195
191
|
});
|
|
196
192
|
|
|
197
|
-
describe(
|
|
198
|
-
it(
|
|
193
|
+
describe("and the first call was a prefetch call", () => {
|
|
194
|
+
it("registers the component of the second call with the model cache", () => {
|
|
199
195
|
ModelCache.register.mockClear();
|
|
200
196
|
// prefetch call, no component
|
|
201
|
-
request(
|
|
197
|
+
request("prefetch", Model);
|
|
202
198
|
expect(ModelCache.register).not.toHaveBeenCalled();
|
|
203
199
|
|
|
204
200
|
// call it again with a component
|
|
205
|
-
request(
|
|
201
|
+
request("prefetch", Model, { component });
|
|
206
202
|
expect(ModelCache.register).toHaveBeenCalled();
|
|
207
203
|
});
|
|
208
204
|
});
|
|
209
205
|
});
|
|
210
206
|
|
|
211
|
-
it(
|
|
207
|
+
it("resolves request with model and request status", async () => {
|
|
212
208
|
var resultingModel = null,
|
|
213
|
-
|
|
209
|
+
resultingStatus = null;
|
|
214
210
|
|
|
215
211
|
waitSuccess = true;
|
|
216
212
|
|
|
217
|
-
request(
|
|
213
|
+
request("newModel", Model, { component }).then(([model, status]) => {
|
|
218
214
|
resultingModel = model;
|
|
219
215
|
resultingStatus = status;
|
|
220
216
|
});
|
|
@@ -224,13 +220,13 @@ describe('Request', () => {
|
|
|
224
220
|
expect(resultingStatus).toBe(200);
|
|
225
221
|
});
|
|
226
222
|
|
|
227
|
-
it(
|
|
223
|
+
it("rejects request with response status", async () => {
|
|
228
224
|
var resultingStatus = null;
|
|
229
225
|
|
|
230
226
|
waitSuccess = true;
|
|
231
227
|
reject = true;
|
|
232
228
|
|
|
233
|
-
request(
|
|
229
|
+
request("newModel", Model, { component }).catch((status) => {
|
|
234
230
|
resultingStatus = status;
|
|
235
231
|
});
|
|
236
232
|
|
|
@@ -239,15 +235,15 @@ describe('Request', () => {
|
|
|
239
235
|
expect(resultingStatus).toBe(404);
|
|
240
236
|
});
|
|
241
237
|
|
|
242
|
-
describe(
|
|
243
|
-
it(
|
|
238
|
+
describe("and the request errors", () => {
|
|
239
|
+
it("does not cache the model", async () => {
|
|
244
240
|
var resultingStatus,
|
|
245
|
-
|
|
241
|
+
CACHE_KEY = "errorModel";
|
|
246
242
|
|
|
247
243
|
waitSuccess = true;
|
|
248
244
|
reject = true;
|
|
249
245
|
|
|
250
|
-
request(CACHE_KEY, Model, {component}).catch((status) => {
|
|
246
|
+
request(CACHE_KEY, Model, { component }).catch((status) => {
|
|
251
247
|
resultingStatus = status;
|
|
252
248
|
});
|
|
253
249
|
|
|
@@ -259,60 +255,59 @@ describe('Request', () => {
|
|
|
259
255
|
});
|
|
260
256
|
});
|
|
261
257
|
|
|
262
|
-
describe(
|
|
263
|
-
var startModel,
|
|
264
|
-
finalModel;
|
|
258
|
+
describe("requested with {force: true}", () => {
|
|
259
|
+
var startModel, finalModel;
|
|
265
260
|
|
|
266
|
-
beforeEach(async() => {
|
|
261
|
+
beforeEach(async () => {
|
|
267
262
|
waitSuccess = true;
|
|
268
263
|
|
|
269
|
-
startModel = await request(
|
|
264
|
+
startModel = await request("bar", Model, { component });
|
|
270
265
|
Model.prototype.fetch.mockClear();
|
|
271
266
|
|
|
272
|
-
finalModel = await request(
|
|
267
|
+
finalModel = await request("bar", Model, { component, force: true });
|
|
273
268
|
});
|
|
274
269
|
|
|
275
270
|
afterEach(() => {
|
|
276
|
-
ModelCache.remove(
|
|
271
|
+
ModelCache.remove("bar");
|
|
277
272
|
});
|
|
278
273
|
|
|
279
|
-
it(
|
|
274
|
+
it("fetches the model anyway", () => {
|
|
280
275
|
expect(Model.prototype.fetch).toHaveBeenCalled();
|
|
281
276
|
});
|
|
282
277
|
|
|
283
|
-
it(
|
|
278
|
+
it("uses the same model instance if one already exists", () => {
|
|
284
279
|
expect(startModel).toEqual(finalModel);
|
|
285
280
|
});
|
|
286
281
|
});
|
|
287
282
|
|
|
288
|
-
describe(
|
|
289
|
-
it(
|
|
290
|
-
await request(
|
|
283
|
+
describe("with the 'lazy' option", () => {
|
|
284
|
+
it("returns a new model without fetching", async () => {
|
|
285
|
+
await request("lazy", Model, { component, lazy: true });
|
|
291
286
|
|
|
292
287
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
293
288
|
expect(ModelCache.put).toHaveBeenCalled();
|
|
294
289
|
});
|
|
295
290
|
|
|
296
|
-
it(
|
|
291
|
+
it("returns an existing model without fetching", async () => {
|
|
297
292
|
var existingModel = new Model(),
|
|
298
|
-
|
|
293
|
+
model;
|
|
299
294
|
|
|
300
|
-
ModelCache.put(
|
|
301
|
-
[model] = await request(
|
|
295
|
+
ModelCache.put("lazy", existingModel, component);
|
|
296
|
+
[model] = await request("lazy", Model, { component, lazy: true });
|
|
302
297
|
|
|
303
298
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
304
299
|
expect(ModelCache.put).toHaveBeenCalled();
|
|
305
300
|
expect(existingModel).toEqual(model);
|
|
306
301
|
});
|
|
307
302
|
|
|
308
|
-
it(
|
|
309
|
-
var [model] = await request(
|
|
310
|
-
|
|
303
|
+
it("fetches when another component requests the same model without lazy option", async () => {
|
|
304
|
+
var [model] = await request("lazy", Model, { component, lazy: true }),
|
|
305
|
+
requestPromise;
|
|
311
306
|
|
|
312
307
|
expect(model.lazy).toBe(true);
|
|
313
308
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
314
309
|
|
|
315
|
-
requestPromise = request(
|
|
310
|
+
requestPromise = request("lazy", Model, { component });
|
|
316
311
|
|
|
317
312
|
expect(model.lazy).toBeDefined();
|
|
318
313
|
await requestPromise;
|
|
@@ -324,45 +319,45 @@ describe('Request', () => {
|
|
|
324
319
|
|
|
325
320
|
// now if we ask for lazy again, followed by another non-lazy, it's already in the cache and
|
|
326
321
|
// we will not re-fetch
|
|
327
|
-
[model] = await request(
|
|
322
|
+
[model] = await request("lazy", Model, { component, lazy: true });
|
|
328
323
|
expect(model.lazy).not.toBeDefined();
|
|
329
|
-
[model] = await request(
|
|
324
|
+
[model] = await request("lazy", Model, { component });
|
|
330
325
|
expect(model.lazy).not.toBeDefined();
|
|
331
326
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
332
327
|
|
|
333
328
|
// now with two lazy requests (ie from different components), it still does not get fetched
|
|
334
|
-
await request(
|
|
335
|
-
await request(
|
|
329
|
+
await request("lazy", Model, { component, lazy: true });
|
|
330
|
+
await request("lazy", Model, { component: {}, lazy: true });
|
|
336
331
|
expect(Model.prototype.fetch).not.toHaveBeenCalled();
|
|
337
332
|
});
|
|
338
333
|
});
|
|
339
334
|
});
|
|
340
335
|
|
|
341
|
-
describe(
|
|
336
|
+
describe("when using getFromCache", () => {
|
|
342
337
|
var model;
|
|
343
338
|
|
|
344
339
|
beforeEach(() => {
|
|
345
340
|
model = new Model();
|
|
346
|
-
ModelCache.put(
|
|
341
|
+
ModelCache.put("foo", model);
|
|
347
342
|
});
|
|
348
343
|
|
|
349
|
-
it(
|
|
350
|
-
expect(existsInCache(
|
|
351
|
-
expect(getFromCache(
|
|
352
|
-
expect(getFromCache(
|
|
344
|
+
it("returns the model if it exists in the ModelCache", () => {
|
|
345
|
+
expect(existsInCache("foo")).toBe(true);
|
|
346
|
+
expect(getFromCache("foo")).toEqual(model);
|
|
347
|
+
expect(getFromCache("bar")).not.toBeDefined();
|
|
353
348
|
});
|
|
354
349
|
});
|
|
355
350
|
});
|
|
356
351
|
|
|
357
352
|
/**
|
|
358
353
|
* Because global state operations are async (and we use waitFor), we have to
|
|
359
|
-
* localize our use of the
|
|
354
|
+
* localize our use of the vi mock clock (otherwise waitFor would also be
|
|
360
355
|
* mocked). We refactor the unregister mocking logic here.
|
|
361
356
|
*/
|
|
362
357
|
function unregisterComponent(comp) {
|
|
363
|
-
|
|
358
|
+
vi.useFakeTimers();
|
|
364
359
|
ModelCache.unregister(comp);
|
|
365
|
-
|
|
366
|
-
|
|
360
|
+
vi.advanceTimersByTime(CACHE_WAIT);
|
|
361
|
+
vi.useRealTimers();
|
|
367
362
|
}
|
|
368
363
|
/* eslint-enable max-nested-callbacks */
|