forked from scratchfoundation/scratch-storage
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
80b3b52
commit 176df44
Showing
3 changed files
with
58 additions
and
51 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
])); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
])); | ||
}); |