From 176df44ed84696e5b8dde7972b1bdcff71ab9f06 Mon Sep 17 00:00:00 2001 From: Muffin Date: Mon, 25 Mar 2024 17:10:20 -0500 Subject: [PATCH] Fix our tests --- test/unit/tw-fetch-tool.js | 51 ----------------------- test/unit/tw-get-retries-on-error.test.js | 32 ++++++++++++++ test/unit/tw-status-0-ok-false.test.js | 26 ++++++++++++ 3 files changed, 58 insertions(+), 51 deletions(-) delete mode 100644 test/unit/tw-fetch-tool.js create mode 100644 test/unit/tw-get-retries-on-error.test.js create mode 100644 test/unit/tw-status-0-ok-false.test.js diff --git a/test/unit/tw-fetch-tool.js b/test/unit/tw-fetch-tool.js deleted file mode 100644 index 7ef17505..00000000 --- a/test/unit/tw-fetch-tool.js +++ /dev/null @@ -1,51 +0,0 @@ -const test = require('tap').test; -const FetchTool = require('../../src/FetchTool'); - -test('get() returns success for status: 0, ok: false', t => { - const text = 'successful response'; - const encoding = 'utf-8'; - const encoded = new TextEncoder().encode(text); - const decoder = new TextDecoder(encoding); - - global.fetch = () => 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(encoded.buffer) - }); - - const tool = new FetchTool(); - - return t.resolves( - tool.get({url: 'url'}).then(result => { - t.equal(decoder.decode(result), text); - }) - ); -}); - -test('get() retries on error', t => { - const text = 'successful response'; - const encoding = 'utf-8'; - const encoded = new TextEncoder().encode(text); - const decoder = new TextDecoder(encoding); - - let attempt = 0; - global.fetch = () => { - 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(encoded.buffer) - }); - }; - - const tool = new FetchTool(); - return t.resolves( - tool.get({url: 'url'}).then(result => { - t.equal(decoder.decode(result), text); - }) - ); -}); diff --git a/test/unit/tw-get-retries-on-error.test.js b/test/unit/tw-get-retries-on-error.test.js new file mode 100644 index 00000000..4f8affe3 --- /dev/null +++ b/test/unit/tw-get-retries-on-error.test.js @@ -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 + ])); +}); diff --git a/test/unit/tw-status-0-ok-false.test.js b/test/unit/tw-status-0-ok-false.test.js new file mode 100644 index 00000000..b39d5c9f --- /dev/null +++ b/test/unit/tw-status-0-ok-false.test.js @@ -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 + ])); +});