Skip to content

Commit

Permalink
Fix our tests
Browse files Browse the repository at this point in the history
  • Loading branch information
GarboMuffin committed Mar 25, 2024
1 parent 80b3b52 commit 176df44
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 51 deletions.
51 changes: 0 additions & 51 deletions test/unit/tw-fetch-tool.js

This file was deleted.

32 changes: 32 additions & 0 deletions test/unit/tw-get-retries-on-error.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
jest.mock('cross-fetch', () => {
const crossFetch = jest.requireActual('cross-fetch');

let attempt = 0;
const mockFetch = () => {
attempt++;
if (attempt === 1) {
// eslint-disable-next-line prefer-promise-reject-errors
return Promise.reject('Intentional error for testing');
}
return Promise.resolve({
ok: true,
arrayBuffer: () => Promise.resolve(new Uint8Array([100, 101, 102, 103]).buffer)
});
};

return {
...crossFetch,
default: mockFetch,
fetch: mockFetch
};
});

const FetchTool = require('../../src/FetchTool');

test('get() retries on error', async () => {
const tool = new FetchTool();
const result = await tool.get({url: 'url'});
expect(new Uint8Array(result)).toStrictEqual(new Uint8Array([
100, 101, 102, 103
]));
});
26 changes: 26 additions & 0 deletions test/unit/tw-status-0-ok-false.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
jest.mock('cross-fetch', () => {
const crossFetch = jest.requireActual('cross-fetch');

const mockFetch = () => Promise.resolve({
// This is what fetch() resolves with in a WKWebView when requesting a file: URL from a file: URL.
ok: false,
status: 0,
arrayBuffer: () => Promise.resolve(new Uint8Array([10, 20, 30, 40]).buffer)
});

return {
...crossFetch,
default: mockFetch,
fetch: mockFetch
};
});

const FetchTool = require('../../src/FetchTool');

test('get() returns success for status: 0, ok: false', async () => {
const tool = new FetchTool();
const result = await tool.get({url: 'url'});
expect(new Uint8Array(result)).toStrictEqual(new Uint8Array([
10, 20, 30, 40
]));
});

0 comments on commit 176df44

Please sign in to comment.