Skip to content

Commit 2f342da

Browse files
author
jarvisjiang
committed
change mock api for test cases
1 parent 706509b commit 2f342da

File tree

1 file changed

+28
-32
lines changed

1 file changed

+28
-32
lines changed

tests/fetch.test.ts

Lines changed: 28 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,70 @@ import { ABORT_ERROR, TIMEOUT_ERROR } from "../src/fetch/defines.ts";
33
import { fetchT } from '../src/mod.ts';
44

55
Deno.test('fetch', async (t) => {
6-
const mockServer = 'https://16a6dafa-2258-4a83-88fa-31a409e42b17.mock.pstmn.io';
7-
const mockTodos = `${ mockServer }/todos`;
8-
const mockTodo1 = `${ mockTodos }/1`;
9-
const mockInvalidJson = `${ mockServer }/invalid_json`;
6+
const mockServer = 'https://fakestoreapi.com';
7+
const mockAll = `${ mockServer }/products`;
8+
const mockSingle = `${ mockAll }/1`;
109
const mockNotFound = `${ mockServer }/not_found`;
10+
const mockInvalidJson = `https://github.com/JiangJie/fetch-t`;
1111

1212
await t.step('Invalid url will throw', () => {
1313
const url = {};
1414
assertThrows(() => fetchT((url as string)), Error);
1515
});
1616

1717
await t.step('Get Response by default', async () => {
18-
const res = (await fetchT(mockTodos)).unwrap();
18+
const res = (await fetchT(mockSingle)).unwrap();
1919
const data = await res.text();
2020

21-
assert(data.includes(`"id": 1`));
21+
assert(typeof data === 'string');
2222
});
2323

2424
await t.step('Get Response by RequestInit', async () => {
25-
const res = (await fetchT(mockTodo1, {
25+
const res = (await fetchT(mockSingle, {
2626
mode: 'no-cors',
2727
} as RequestInit)).unwrap();
2828
const data = await res.text();
2929

30-
assert(data.includes(`"id": 1`));
30+
assert(typeof data === 'string');
3131
});
3232

3333
await t.step('Get text by response type', async () => {
34-
const data = (await fetchT(mockTodo1, {
34+
const data = (await fetchT(mockSingle, {
3535
responseType: 'text',
3636
})).unwrap();
3737

38-
assert(data.includes(`"id": 1`));
38+
assert(typeof data === 'string');
3939
});
4040

4141
await t.step('Get arraybuffer by response type', async () => {
42-
const data = (await fetchT(mockTodo1, {
42+
const data = (await fetchT(mockSingle, {
4343
responseType: 'arraybuffer',
4444
})).unwrap();
4545

46-
assert(data.byteLength === 37);
46+
assert(data instanceof ArrayBuffer);
4747
});
4848

4949
await t.step('Get blob by response type', async () => {
50-
const data = (await fetchT(mockTodo1, {
50+
const data = (await fetchT(mockSingle, {
5151
responseType: 'blob',
5252
})).unwrap();
5353

54-
assert(data.size === 37);
54+
assert(data instanceof Blob);
5555
});
5656

5757
await t.step('Get json by response type', async () => {
58-
const data = (await fetchT<{ id: number }[]>(mockTodos, {
58+
const data = (await fetchT<{ id: number }>(mockSingle, {
5959
responseType: 'json',
6060
method: 'GET',
6161
})).unwrap();
6262

63-
assert(data.length === 1);
64-
assert(data[0].id === 1);
63+
assert('id' in data);
6564
});
6665

6766
await t.step('Post json', async () => {
6867
const data = (await fetchT<{
69-
id: number;
70-
}>(mockTodos, {
68+
title: string;
69+
}>(mockAll, {
7170
responseType: 'json',
7271
method: 'POST',
7372
body: JSON.stringify({
@@ -78,33 +77,30 @@ Deno.test('fetch', async (t) => {
7877
},
7978
})).unwrap();
8079

81-
assert(data.id === 2);
80+
assert(data.title === 'happy-2');
8281
});
8382

8483
await t.step('Put json', async () => {
8584
const data = (await fetchT<{
86-
id: number;
8785
title: string;
88-
}>(mockTodo1, {
86+
}>(mockSingle, {
8987
responseType: 'json',
9088
method: 'PUT',
9189
body: JSON.stringify({
92-
id: 100,
9390
title: 'happy-new',
9491
}),
9592
headers: {
9693
'Content-type': 'application/json; charset=UTF-8',
9794
},
9895
})).unwrap();
9996

100-
assert(data.id === 100);
10197
assert(data.title === 'happy-new');
10298
});
10399

104100
await t.step('Patch json', async () => {
105101
const data = (await fetchT<{
106102
title: string;
107-
}>(mockTodo1, {
103+
}>(mockSingle, {
108104
responseType: 'json',
109105
method: 'PATCH',
110106
body: JSON.stringify({
@@ -120,13 +116,13 @@ Deno.test('fetch', async (t) => {
120116

121117
await t.step('Delete json', async () => {
122118
const data = (await fetchT<{
123-
success: boolean;
124-
}>(mockTodo1, {
119+
title: boolean;
120+
}>(mockSingle, {
125121
responseType: 'json',
126122
method: 'DELETE',
127123
})).unwrap();
128124

129-
assert(data.success);
125+
assert('title' in data);
130126
});
131127

132128
await t.step('Get invalid json', async () => {
@@ -138,7 +134,7 @@ Deno.test('fetch', async (t) => {
138134
});
139135

140136
await t.step('Abort fetch by default', async () => {
141-
const fetchTask = fetchT(mockTodo1, {
137+
const fetchTask = fetchT(mockSingle, {
142138
abortable: true,
143139
timeout: 1000,
144140
});
@@ -153,7 +149,7 @@ Deno.test('fetch', async (t) => {
153149
});
154150

155151
await t.step('Abort fetch by custom', async () => {
156-
const fetchTask = fetchT(mockTodo1, {
152+
const fetchTask = fetchT(mockSingle, {
157153
abortable: true,
158154
timeout: 1000,
159155
});
@@ -173,13 +169,13 @@ Deno.test('fetch', async (t) => {
173169
});
174170

175171
await t.step('Invalid timeout', () => {
176-
assertThrows(() => fetchT(mockTodo1, {
172+
assertThrows(() => fetchT(mockSingle, {
177173
timeout: -1,
178174
}), Error);
179175
});
180176

181177
await t.step('Abort fetch by timeout', async () => {
182-
const res = await fetchT(mockTodo1, {
178+
const res = await fetchT(mockSingle, {
183179
timeout: 1,
184180
});
185181

0 commit comments

Comments
 (0)