-
Notifications
You must be signed in to change notification settings - Fork 9
/
requests.test.ts
124 lines (105 loc) · 3.79 KB
/
requests.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
import * as Http from '@tkrotoff/fetch';
import {
abortRequestExample,
del200OKExample,
get200OKExample,
get404NotFoundExample,
get500InternalServerErrorExample,
getCorsBlockedExample,
postJSON201CreatedExample
} 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('abortRequestExample()', async () => {
const abortError = new DOMException('This operation was aborted', '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)
}
);
});