Skip to content

Commit 176df44

Browse files
committed
Fix our tests
1 parent 80b3b52 commit 176df44

File tree

3 files changed

+58
-51
lines changed

3 files changed

+58
-51
lines changed

test/unit/tw-fetch-tool.js

Lines changed: 0 additions & 51 deletions
This file was deleted.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
jest.mock('cross-fetch', () => {
2+
const crossFetch = jest.requireActual('cross-fetch');
3+
4+
let attempt = 0;
5+
const mockFetch = () => {
6+
attempt++;
7+
if (attempt === 1) {
8+
// eslint-disable-next-line prefer-promise-reject-errors
9+
return Promise.reject('Intentional error for testing');
10+
}
11+
return Promise.resolve({
12+
ok: true,
13+
arrayBuffer: () => Promise.resolve(new Uint8Array([100, 101, 102, 103]).buffer)
14+
});
15+
};
16+
17+
return {
18+
...crossFetch,
19+
default: mockFetch,
20+
fetch: mockFetch
21+
};
22+
});
23+
24+
const FetchTool = require('../../src/FetchTool');
25+
26+
test('get() retries on error', async () => {
27+
const tool = new FetchTool();
28+
const result = await tool.get({url: 'url'});
29+
expect(new Uint8Array(result)).toStrictEqual(new Uint8Array([
30+
100, 101, 102, 103
31+
]));
32+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
jest.mock('cross-fetch', () => {
2+
const crossFetch = jest.requireActual('cross-fetch');
3+
4+
const mockFetch = () => Promise.resolve({
5+
// This is what fetch() resolves with in a WKWebView when requesting a file: URL from a file: URL.
6+
ok: false,
7+
status: 0,
8+
arrayBuffer: () => Promise.resolve(new Uint8Array([10, 20, 30, 40]).buffer)
9+
});
10+
11+
return {
12+
...crossFetch,
13+
default: mockFetch,
14+
fetch: mockFetch
15+
};
16+
});
17+
18+
const FetchTool = require('../../src/FetchTool');
19+
20+
test('get() returns success for status: 0, ok: false', async () => {
21+
const tool = new FetchTool();
22+
const result = await tool.get({url: 'url'});
23+
expect(new Uint8Array(result)).toStrictEqual(new Uint8Array([
24+
10, 20, 30, 40
25+
]));
26+
});

0 commit comments

Comments
 (0)