This repository has been archived by the owner on Jun 17, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ApiRequestManager.test.ts
263 lines (216 loc) · 9.86 KB
/
ApiRequestManager.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
// Copyright 2021 Peter Beverloo & AnimeCon. All rights reserved.
// Use of this source code is governed by a MIT license that can be
// found in the LICENSE file.
import { RestoreConsole, default as mockConsole } from 'jest-mock-console';
import { clear as kvClear } from 'idb-keyval';
import fetchMock from 'jest-fetch-mock';
import { ApiRequestManager } from './ApiRequestManager';
import { IAuthResponse } from '../api/IAuth';
import { IContentResponse, IContentResponsePage } from '../api/IContent';
import { IEventResponse } from '../api/IEvent';
describe('ApiRequestManager', () => {
let restoreConsole: RestoreConsole | undefined = undefined;
afterEach(() => restoreConsole!());
beforeEach(async () => {
// (1) Install the moacked console, to catch console.error() messages.
restoreConsole = mockConsole();
// (2) Clear the cache, as this test suite depends on validating caching behaviour.
await kvClear();
});
it('is able to issue requests and receive successful responses', async () => {
const kFirstValidContentResponse = {
pages: [
{
pathname: '/index.html',
content: 'Hello, world!',
modified: 1451606400,
}
]
};
const kSecondValidContentResponse = {
pages: [
{
pathname: '/index.html',
content: 'Have a good evening!', // <-- updated
modified: 1451606400,
}
]
};
const responses: IContentResponse[] = [];
const requestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error?: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IContentResponse) {
responses.push(response);
}
});
// The first response will always hit the network, no matter what happens.
fetchMock.mockOnceIf('/api/content', async request => ({
body: JSON.stringify(kFirstValidContentResponse),
status: 200,
}));
expect(await requestManager.issue()).toBeTruthy();
expect(responses).toHaveLength(1);
expect(responses[0]).toEqual(kFirstValidContentResponse);
// The second response will hit the network, but will then be ignored because it's observed
// that the returned data has not been invalidated.
fetchMock.mockOnceIf('/api/content', async request => ({
body: JSON.stringify(kFirstValidContentResponse),
status: 200,
}));
expect(await requestManager.issue()).toBeTruthy();
expect(responses).toHaveLength(1);
// The third update will go through to the network again, where the server will issue new
// data, which means that we will be receiving a callback for a successful response.
fetchMock.mockOnceIf('/api/content', async request => ({
body: JSON.stringify(kSecondValidContentResponse),
status: 200,
}));
expect(await requestManager.issue()).toBeTruthy();
expect(responses).toHaveLength(2);
expect(responses[1]).toEqual(kSecondValidContentResponse);
});
it('is able to issue requests and receive failed responses', async () => {
fetchMock.mockIf('/api/content', async request => ({
status: 403,
}));
const errors: Error[] = [];
const requestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error: Error) {
errors.push(error);
}
onSuccessResponse(response: IContentResponse) {
throw new Error('The `onSuccessResponse` callback was unexpectedly invoked.');
}
});
expect(await requestManager.issue()).toBeFalsy();
expect(errors).toHaveLength(1);
expect(errors[0].message).toEqual('Unable to fetch data from the server (403).');
});
it('ensures that only a single response is in progress at once', async () => {
jest.useFakeTimers();
fetchMock.mockIf('/api/content', async request => {
jest.advanceTimersByTime(/* msToRun= */ 150);
return JSON.stringify({ pages: [] });
});
let responseCount = 0;
const requestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error: Error) {
// Note that AbortError instances are suppressed when considering to invoke the
// onFailedResponse callback, as they're a direct consequence of our design.
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IContentResponse) {
responseCount++;
}
});
const promii: Promise<boolean>[] = [];
const waitPromise = new Promise<boolean>(resolve => {
setTimeout(() => resolve(requestManager.issue()), 100);
});
promii.push(requestManager.issue());
promii.push(waitPromise);
expect(await Promise.all(promii)).toEqual([ false, true ]);
expect(responseCount).toEqual(1);
});
it('has the ability to cache request/response pairs', async () => {
const kValidContentResponse = {
pages: [
{
pathname: '/index.html',
content: 'Hello, world!',
modified: 1451606400,
}
]
};
// Request the IContent API and expect the results to be cached in the store.
{
let pages: IContentResponsePage[] = [];
const contentRequestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IContentResponse) {
pages = response.pages;
}
});
fetchMock.mockOnceIf('/api/content', async request => ({
body: JSON.stringify(kValidContentResponse),
status: 200,
}));
expect(await contentRequestManager.issue()).toBeTruthy();
expect(pages).toHaveLength(1);
}
// Request the IContent API again, but have the network request return a non-ok status.
{
let pages: IContentResponsePage[] = [];
const contentRequestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IContentResponse) {
pages = response.pages;
}
});
fetchMock.mockOnceIf('/api/content', async request => ({
status: 403,
}));
expect(await contentRequestManager.issue()).toBeTruthy();
expect(pages).toHaveLength(1);
}
// Request the IContent API again, and expect it to not have been overwritten by the non-OK
// response that was issued in the previous response.
{
let pages: IContentResponsePage[] = [];
const contentRequestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IContentResponse) {
pages = response.pages;
}
});
fetchMock.mockOnceIf('/api/content', async request => ({
status: 403,
}));
expect(await contentRequestManager.issue()).toBeTruthy();
expect(pages).toHaveLength(1);
}
});
it('has the ability to determine whether a request is cacheable', async () => {
const authRequestManager = new ApiRequestManager('IAuth', new class {
onFailedResponse(error: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IAuthResponse) {
throw new Error('The `onSuccessResponse` callback was unexpectedly invoked.');
}
});
expect(authRequestManager.determineCacheKey({
emailAddress: 'foo@example.com',
accessCode: '1234',
})).toBeUndefined();
const contentRequestManager = new ApiRequestManager('IContent', new class {
onFailedResponse(error: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IContentResponse) {
throw new Error('The `onSuccessResponse` callback was unexpectedly invoked.');
}
});
expect(contentRequestManager.determineCacheKey()).toEqual('IContent');
const eventRequestManager = new ApiRequestManager('IEvent', new class {
onFailedResponse(error: Error) {
throw new Error('The `onFailedResponse` callback was unexpectedly invoked.');
}
onSuccessResponse(response: IEventResponse) {
throw new Error('The `onSuccessResponse` callback was unexpectedly invoked.');
}
});
expect(eventRequestManager.determineCacheKey({ authToken: 'AT', event: 'EVT' }))
.toEqual('IEvent-AT-EVT');
expect(eventRequestManager.determineCacheKey({ event: 'EVT', authToken: 'AT' }))
.toEqual('IEvent-AT-EVT');
});
});