This repository has been archived by the owner on Jul 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcreate-use-fetch.test.ts
170 lines (144 loc) · 4.66 KB
/
create-use-fetch.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
import { expect } from 'chai';
import { Headers as NodeFetchHeaders } from 'node-fetch';
import { createUseFetch } from '../fetch-suspense';
import CommonJS = require('../fetch-suspense');
type FetchResponse = Object | string;
interface FetchResponseMetadata {
contentType: null | string;
headers: Headers;
ok: boolean;
redirected: boolean;
response: FetchResponse;
status: number;
statusText: string;
}
interface Options {
lifespan?: number;
metadata?: boolean;
}
interface OptionsWithMetadata extends Options {
metadata: true;
}
interface OptionsWithoutMetadata extends Options {
metadata?: false;
}
interface UseFetch {
(
input: RequestInfo,
init?: RequestInit | undefined,
options?: number | OptionsWithoutMetadata,
): FetchResponse;
(
input: RequestInfo,
init: RequestInit | undefined,
options: OptionsWithMetadata,
): FetchResponseMetadata;
}
const MOCK_BODY: string = 'mock body';
const MOCK_STATUS: number = 420;
const MOCK_STATUS_TEXT: string = 'mock status text';
const TEST_CONTENT_TYPE: string = 'text/plain';
const TEST_PATH: string = '/test-path';
const MOCK_HEADERS_INIT: HeadersInit = {
'Content-Type': TEST_CONTENT_TYPE,
};
const MOCK_HEADERS: Headers =
new NodeFetchHeaders(MOCK_HEADERS_INIT) as any as Headers;
const MOCK_RESPONSE: Response = {
arrayBuffer: (): Promise<ArrayBuffer> =>
Promise.resolve(null as any as ArrayBuffer),
blob: (): Promise<Blob> => Promise.resolve(null as any as Blob),
body: null,
bodyUsed: true,
clone: (): Response => MOCK_RESPONSE,
formData: (): Promise<FormData> => Promise.resolve(null as any as FormData),
headers: MOCK_HEADERS,
json: (): Promise<null> => Promise.resolve(null),
ok: true,
redirected: false,
status: MOCK_STATUS,
statusText: MOCK_STATUS_TEXT,
text: (): Promise<string> => Promise.resolve(MOCK_BODY),
trailer: Promise.resolve(MOCK_HEADERS),
type: null as any as ResponseType,
url: '',
};
const MOCK_FETCH = (): Promise<Response> => Promise.resolve(MOCK_RESPONSE);
describe('createUseFetch', (): void => {
it('should be a function with 1 parameter via CommonJS', (): void => {
expect(CommonJS.createUseFetch).to.be.a('function');
expect(CommonJS.createUseFetch.length).to.equal(1);
});
it('should be a function with 1 parameter via ES6', (): void => {
expect(createUseFetch).to.be.a('function');
expect(createUseFetch.length).to.equal(1);
});
describe('return value', (): void => {
it('should be a function with 3 parameters via CommonJS', (): void => {
const useFetch = CommonJS.createUseFetch(MOCK_FETCH);
expect(useFetch).to.be.a('function');
expect(useFetch.length).to.equal(3);
});
it('should be a function with 3 parameters via ES6', (): void => {
const useFetch = createUseFetch(MOCK_FETCH);
expect(useFetch).to.be.a('function');
expect(useFetch.length).to.equal(3);
});
describe('useFetch', (): void => {
let useFetch: UseFetch;
beforeEach((): void => {
useFetch = createUseFetch(MOCK_FETCH)
});
it('should throw a Promise', (): void => {
try {
useFetch(TEST_PATH);
} catch (p) {
expect(p).to.be.instanceOf(Promise);
return;
}
throw new Error('useFetch did not throw.');
});
it('should not throw twice', async (): Promise<void> => {
try {
useFetch(TEST_PATH);
} catch (p) {
await p;
try {
useFetch(TEST_PATH);
return;
} catch (q) {
throw new Error('useFetch threw twice.');
}
}
throw new Error('useFetch did not throw.');
});
it('should return a Response', async (): Promise<void> => {
try {
useFetch(TEST_PATH);
} catch (p) {
await p;
const response = useFetch(TEST_PATH);
expect(response).to.equal(MOCK_BODY);
return;
}
throw new Error('useFetch did not throw.');
});
it('should return a Response with metadata', async (): Promise<void> => {
try {
useFetch(TEST_PATH, undefined, { metadata: true });
} catch (p) {
await p;
const { contentType, headers, response, status, statusText } =
useFetch(TEST_PATH, undefined, { metadata: true });
expect(contentType).to.equal(TEST_CONTENT_TYPE);
expect(headers).to.equal(MOCK_HEADERS);
expect(response).to.equal(MOCK_BODY);
expect(status).to.equal(MOCK_STATUS);
expect(statusText).to.equal(MOCK_STATUS_TEXT);
return;
}
throw new Error('useFetch did not throw.');
});
});
});
});