Skip to content

Commit

Permalink
Add a bit more context for errors in loaders (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbents authored May 15, 2023
1 parent 8b80e20 commit 37dc60d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
25 changes: 24 additions & 1 deletion src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,29 @@ test('should throw error on failed loading', async () => {

await promise
} catch (error) {
expect(error.message).toBe(`'RevolutCheckout' is failed to load`)
expect(error.message).toBe(
`'RevolutCheckout' failed to load: Network error encountered`
)
}
})

test('should throw error if RevolutCheckout is missing', async () => {
const { script, RevolutCheckout, TriggerSuccess } = setup()

const promise = RevolutCheckout('PROD_XXX')

expect(script).toHaveAttribute('id', 'revolut-checkout')
expect(script).toHaveAttribute('src', 'https://merchant.revolut.com/embed.js')

TriggerSuccess.mockImplementationOnce(() => {
// RevolutCheckout is not assigned to window
fireEvent.load(script)
})
TriggerSuccess()

await expect(promise).rejects.toEqual(
new Error(
`'RevolutCheckout' failed to load: RevolutCheckout is not a function`
)
)
})
8 changes: 4 additions & 4 deletions src/loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ export function RevolutCheckoutLoader(
document.head.appendChild(script)

return new Promise((resolve, reject) => {
function handleError() {
function handleError(reason: string) {
document.head.removeChild(script)

reject(new Error(`'RevolutCheckout' is failed to load`))
reject(new Error(`'RevolutCheckout' failed to load: ${reason}`))
}

function handleLoad() {
Expand All @@ -50,12 +50,12 @@ export function RevolutCheckoutLoader(
loaded = RevolutCheckout
delete window.RevolutCheckout
} else {
handleError()
handleError('RevolutCheckout is not a function')
}
}

script.onload = handleLoad
script.onerror = handleError
script.onerror = () => handleError('Network error encountered')
})
}

Expand Down
31 changes: 28 additions & 3 deletions src/payments.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ test('should not request new embed script and use loaded one', async () => {
publicToken: 'MERCHANT_PUBLIC_TOKEN_1',
})

await RevolutPayments('MERCHANT_PUBLIC_TOKEN_2')
await RevolutPayments({ publicToken: 'MERCHANT_PUBLIC_TOKEN_2' })
expect(MockRevolutPayments).toHaveBeenCalledWith({
publicToken: 'MERCHANT_PUBLIC_TOKEN_2',
})
Expand Down Expand Up @@ -186,7 +186,7 @@ test(`should use 'prod' by default`, async () => {
})
})

test.only('should throw error on failed loading', async () => {
test('should throw error on failed loading', async () => {
expect.assertions(1)

const { RevolutPayments, TriggerError } = setup()
Expand All @@ -200,6 +200,31 @@ test.only('should throw error on failed loading', async () => {

await promise
} catch (error) {
expect(error.message).toBe(`'RevolutPayments' failed to load`)
expect(error.message).toBe(
`'RevolutPayments' failed to load: Network error encountered`
)
}
})

test('should throw error if RevolutCheckout is missing', async () => {
const { script, RevolutPayments, TriggerSuccess } = setup()

const promise = RevolutPayments({
publicToken: 'MERCHANT_PUBLIC_TOKEN_PROD_XXX',
})

expect(script).toHaveAttribute('id', 'revolut-payments')
expect(script).toHaveAttribute('src', 'https://merchant.revolut.com/embed.js')

TriggerSuccess.mockImplementationOnce(() => {
// RevolutCheckout is not assigned to window
fireEvent.load(script)
})
TriggerSuccess()

await expect(promise).rejects.toEqual(
new Error(
`'RevolutPayments' failed to load: RevolutCheckout is not a function`
)
)
})
8 changes: 4 additions & 4 deletions src/paymentsLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ export function RevolutPaymentsLoader(
document.head.appendChild(script)

return new Promise((resolve, reject) => {
function handleError() {
function handleError(reason: string) {
document.head.removeChild(script)

reject(new Error(`'RevolutPayments' failed to load`))
reject(new Error(`'RevolutPayments' failed to load: ${reason}`))
}

function handleLoad() {
Expand All @@ -40,12 +40,12 @@ export function RevolutPaymentsLoader(
loadedPaymentInstance = RevolutCheckout.payments
delete window.RevolutCheckout
} else {
handleError()
handleError('RevolutCheckout is not a function')
}
}

script.onload = handleLoad
script.onerror = handleError
script.onerror = () => handleError('Network error encountered')
})
}

Expand Down

0 comments on commit 37dc60d

Please sign in to comment.