Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
silesky committed Dec 30, 2022
1 parent 5ae11df commit a8a992b
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
24 changes: 24 additions & 0 deletions packages/browser/src/lib/__tests__/fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import unfetch from 'unfetch'
import { getGlobal } from '../get-global'
import { fetch } from '../fetch'

jest.mock('unfetch')
const unfetchMock = jest.mocked(unfetch).mockResolvedValue({} as Response)

jest.mock('../get-global')
const getGlobalMock = jest.mocked(getGlobal)

describe(fetch, () => {
it('should call native fetch if available', async () => {
const fetchSpy = jest.fn()
getGlobalMock.mockReturnValue({ fetch: fetchSpy } as any)
void fetch('http://foo.com')
expect(unfetchMock).not.toBeCalled()
expect(fetchSpy).toBeCalled()
})
it('should fall back to unfetch', async () => {
getGlobalMock.mockReturnValue(null)
void fetch('http://foo.com')
expect(unfetchMock).toBeCalled()
})
})
8 changes: 2 additions & 6 deletions packages/browser/src/lib/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@ import { getGlobal } from './get-global'
*/
export const fetch: typeof unfetch = (...args) => {
const global = getGlobal()
let fetch = unfetch

if (global && global.fetch) {
fetch = global.fetch
}
return fetch(...args)
const fn = (global && global.fetch) || unfetch
return fn(...args)
}

0 comments on commit a8a992b

Please sign in to comment.