/
fetch-spec.ts
201 lines (170 loc) · 5.45 KB
/
fetch-spec.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
import { fromFetch } from 'rxjs/fetch';
import { expect } from 'chai';
import { root } from '../../../src/internal/util/root';
const OK_RESPONSE = {
ok: true,
} as Response;
function mockFetchImpl(input: string | Request, init?: RequestInit): Promise<Response> {
(mockFetchImpl as MockFetch).calls.push({ input, init });
return new Promise<any>((resolve, reject) => {
if (init.signal) {
init.signal.addEventListener('abort', () => {
reject(new MockDOMException());
});
}
return Promise.resolve(null).then(() => {
resolve((mockFetchImpl as any).respondWith);
});
});
}
(mockFetchImpl as MockFetch).reset = function (this: any) {
this.calls = [] as any[];
this.respondWith = OK_RESPONSE;
};
(mockFetchImpl as MockFetch).reset();
const mockFetch: MockFetch = mockFetchImpl as MockFetch;
class MockDOMException {}
class MockAbortController {
readonly signal = new MockAbortSignal();
abort() {
this.signal._signal();
}
constructor() {
MockAbortController.created++;
}
static created = 0;
static reset() {
MockAbortController.created = 0;
}
}
class MockAbortSignal {
private _listeners: Function[] = [];
aborted = false;
addEventListener(name: 'abort', handler: Function) {
this._listeners.push(handler);
}
removeEventListener(name: 'abort', handler: Function) {
const index = this._listeners.indexOf(handler);
if (index >= 0) {
this._listeners.splice(index, 1);
}
}
_signal() {
this.aborted = true;
while (this._listeners.length > 0) {
this._listeners.shift()();
}
}
}
interface MockFetch {
(input: string | Request, init?: RequestInit): Promise<Response>;
calls: { input: string | Request, init: RequestInit | undefined }[];
reset(): void;
respondWith: Response;
}
describe('fromFetch', () => {
let _fetch: typeof fetch;
let _AbortController: AbortController;
beforeEach(() => {
mockFetch.reset();
if (root.fetch) {
_fetch = root.fetch;
}
root.fetch = mockFetch;
MockAbortController.reset();
if (root.AbortController) {
_AbortController = root.AbortController;
}
root.AbortController = MockAbortController;
});
afterEach(() => {
root.fetch = _fetch;
root.AbortController = _AbortController;
});
it('should exist', () => {
expect(fromFetch).to.be.a('function');
});
it('should fetch', done => {
const fetch$ = fromFetch('/foo');
expect(mockFetch.calls.length).to.equal(0);
expect(MockAbortController.created).to.equal(0);
fetch$.subscribe({
next: response => {
expect(response).to.equal(OK_RESPONSE);
},
error: done,
complete: () => {
// Wait until the complete and the subsequent unsubscribe are finished
// before testing these expectations:
setTimeout(() => {
expect(MockAbortController.created).to.equal(1);
expect(mockFetch.calls.length).to.equal(1);
expect(mockFetch.calls[0].input).to.equal('/foo');
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
done();
}, 0);
}
});
});
it('should handle Response that is not `ok`', done => {
mockFetch.respondWith = {
ok: false,
status: 400,
body: 'Bad stuff here'
} as any as Response;
const fetch$ = fromFetch('/foo');
expect(mockFetch.calls.length).to.equal(0);
expect(MockAbortController.created).to.equal(0);
fetch$.subscribe({
next: response => {
expect(response).to.equal(mockFetch.respondWith);
},
complete: done,
error: done
});
expect(MockAbortController.created).to.equal(1);
expect(mockFetch.calls.length).to.equal(1);
expect(mockFetch.calls[0].input).to.equal('/foo');
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
});
it('should abort when unsubscribed', () => {
const fetch$ = fromFetch('/foo');
expect(mockFetch.calls.length).to.equal(0);
expect(MockAbortController.created).to.equal(0);
const subscription = fetch$.subscribe();
expect(MockAbortController.created).to.equal(1);
expect(mockFetch.calls.length).to.equal(1);
expect(mockFetch.calls[0].input).to.equal('/foo');
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
expect(mockFetch.calls[0].init.signal.aborted).to.be.false;
subscription.unsubscribe();
expect(mockFetch.calls[0].init.signal.aborted).to.be.true;
});
it('should allow passing of init object', done => {
const myInit = {};
const fetch$ = fromFetch('/foo', myInit);
fetch$.subscribe({
error: done,
complete: done,
});
expect(mockFetch.calls[0].init).to.equal(myInit);
expect(mockFetch.calls[0].init.signal).not.to.be.undefined;
});
it('should treat passed signals as a cancellation token which triggers an error', done => {
const controller = new MockAbortController();
const signal = controller.signal as any;
const fetch$ = fromFetch('/foo', { signal });
const subscription = fetch$.subscribe({
error: err => {
expect(err).to.be.instanceof(MockDOMException);
done();
}
});
controller.abort();
expect(mockFetch.calls[0].init.signal.aborted).to.be.true;
// The subscription will not be closed until the error fires when the promise resolves.
expect(subscription.closed).to.be.false;
});
});