Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions packages/core/src/useScriptTag/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,38 @@ describe(useScriptTag, () => {
expect(scriptTagElement()).toBeInstanceOf(HTMLScriptElement)
})

// Drop the handler on the auto-load and jest fails this on the unhandled
// rejection, not on the assertion.
it('should report a failed auto-load through status, not an unhandled rejection', async () => {
const hook = renderHook(() => {
const [, status] = useScriptTag(src, () => {}, { immediate: true })

return { status }
})

await act(async () => {
scriptTagElement()!.dispatchEvent(new Event('error'))
})

expect(hook.result.current.status).toBe('error')
})

it('should still reject for callers that hold the load promise', async () => {
const hook = renderHook(() => {
const [, , load] = useScriptTag(src, () => {}, { immediate: true })

return { load }
})

const loading = hook.result.current.load()

await act(async () => {
scriptTagElement()!.dispatchEvent(new Event('error'))
})

await expect(loading).rejects.toThrow(`Failed to load script: ${src}`)
})

it('should re-use the same src for multiple loads', async () => {
const addChildListener = jest.spyOn(document.head, 'appendChild')

Expand Down
3 changes: 2 additions & 1 deletion packages/core/src/useScriptTag/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ export const useScriptTag: UseScriptTag = (

useMount(() => {
if (immediate && !manual) {
load()
// Nothing awaits this promise; a failing script is reported via `status`.
load().catch(noop)
}
})

Expand Down
Loading