-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathrequests.test.ts
More file actions
171 lines (145 loc) · 5.43 KB
/
Copy pathrequests.test.ts
File metadata and controls
171 lines (145 loc) · 5.43 KB
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
import * as Http from '@tkrotoff/fetch';
import {
abortRequestExample,
del200OKExample,
downloadProgressExample,
get200OKExample,
get404NotFoundExample,
get500InternalServerErrorExample,
getCorsBlockedExample,
postJSON201CreatedExample,
uploadFilesExample
} from './requests';
// https://github.com/aelbore/esbuild-jest/issues/26#issuecomment-968853688
// https://github.com/swc-project/swc/issues/5059
jest.mock('@tkrotoff/fetch', () => ({
__esModule: true,
...jest.requireActual('@tkrotoff/fetch')
}));
beforeEach(jest.restoreAllMocks);
test('get200OKExample()', async () => {
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
Http.createJSONResponsePromise({
userId: 1,
id: 1,
title: 'sunt aut facere repellat provident occaecati excepturi optio reprehenderit',
body: 'quia et suscipit\nsuscipit recusandae consequuntur expedita et cum\nreprehenderit molestiae ut ut quas totam\nnostrum rerum est autem sunt rem eveniet architecto'
})
);
await get200OKExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
});
test('postJSON201CreatedExample()', async () => {
const mock = jest.spyOn(Http, 'postJSON').mockImplementation(() =>
Http.createJSONResponsePromise({
id: 101,
title: 'foo',
body: 'bar',
userId: 1
})
);
await postJSON201CreatedExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts', {
body: 'bar',
title: 'foo',
userId: 1
});
});
test('del200OKExample()', async () => {
const mock = jest.spyOn(Http, 'del').mockImplementation(() => Http.createJSONResponsePromise({}));
await del200OKExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://jsonplaceholder.typicode.com/posts/1');
});
test('get404NotFoundExample()', async () => {
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
Http.createResponsePromise('404 Not Found', {
status: Http.HttpStatus._404_NotFound,
statusText: 'Not Found'
})
);
await get404NotFoundExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://httpstat.us/404/cors');
});
test('get500InternalServerErrorExample()', async () => {
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
Http.createResponsePromise('500 Internal Server Error', {
status: Http.HttpStatus._500_InternalServerError,
statusText: 'Internal Server Error'
})
);
await get500InternalServerErrorExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://httpstat.us/500/cors');
});
test('getCorsBlockedExample()', async () => {
const mock = jest.spyOn(Http, 'get').mockRejectedValue(new TypeError('Failed to fetch'));
await getCorsBlockedExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://postman-echo.com/get?foo1=bar1&foo2=bar2');
});
test('uploadFilesExample()', async () => {
const mock = jest.spyOn(Http, 'post').mockImplementation(() =>
Http.createJSONResponsePromise({
files: { file0: 'file0Content', file1: 'file1Content' }
})
);
const file0 = new File(['file0Content'], 'file0', { type: 'text/plain' });
const file1 = new File(['file1Content'], 'file1', { type: 'text/plain' });
await uploadFilesExample([file0, file1] as unknown as FileList);
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith('https://httpbin.org/anything', expect.any(FormData));
});
test('abortRequestExample()', async () => {
const abortError = new DOMException('The user aborted a request.', 'AbortError');
const mock = jest.spyOn(Http, 'get').mockImplementation((_input, init) => {
// Mock aborted request
// https://github.com/github/fetch/blob/v3.4.1/fetch.js#L497
const response = new Promise((resolve, reject) => {
setTimeout(() => {
if (init!.signal && init!.signal.aborted) {
reject(abortError);
}
resolve('**********');
}, 600);
});
return response as Http.ResponsePromiseWithBodyMethods;
});
await abortRequestExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith(
'https://httpbin.org/drip?duration=2&numbytes=10&code=200&delay=2',
{
signal: expect.any(AbortSignal)
}
);
});
// FIXME jsdom does not support Blob.stream https://github.com/jsdom/jsdom/issues/2555
// "TypeError: blob.stream is not a function"
// eslint-disable-next-line jest/no-disabled-tests
test.skip('downloadProgressExample()', async () => {
document.body.innerHTML =
'<progress id="download-progress-indicator" value="0"></progress>' +
'<img id="download-progress-img" alt="download-progress-img" src="data:," />';
// Use File instead? https://developer.mozilla.org/en-US/docs/Web/API/File/File
const content = new Uint8Array([
/* https://stackoverflow.com/a/64635408 */
]);
const blob = new Blob([content.buffer]);
const stream = blob.stream();
const mock = jest.spyOn(Http, 'get').mockImplementation(() =>
Http.createResponsePromise(stream, {
headers: {
'content-length': blob.size.toString()
}
})
);
await downloadProgressExample();
expect(mock).toHaveBeenCalledTimes(1);
expect(mock).toHaveBeenCalledWith(
'https://fetch-progress.anthum.com/30kbps/images/sunrise-baseline.jpg'
);
});