N
Nuxt7mo ago
dwol

Does anyone know how to mock/assert that navigateTo was called with the correct options?

Basically I have a function thats called after submitting a form and the function returns a navigateTo an external payment portal. how can I test that the navigateTo method was called with the correct options?
/// ~/utils/handlePaymentRedirect.ts
export function handlePaymentRedirect (payToken: number, filingId: number) {
const config = useRuntimeConfig()
const { locale } = useI18n()
const paymentUrl = config.public.paymentPortalUrl
const baseUrl = config.public.baseUrl
const returnUrl = encodeURIComponent(`${baseUrl}${locale.value}/submitted?filing_id=${filingId}`)
const payUrl = paymentUrl + payToken + '/' + returnUrl

return navigateTo(payUrl, { external: true })
}
/// ~/utils/handlePaymentRedirect.ts
export function handlePaymentRedirect (payToken: number, filingId: number) {
const config = useRuntimeConfig()
const { locale } = useI18n()
const paymentUrl = config.public.paymentPortalUrl
const baseUrl = config.public.baseUrl
const returnUrl = encodeURIComponent(`${baseUrl}${locale.value}/submitted?filing_id=${filingId}`)
const payUrl = paymentUrl + payToken + '/' + returnUrl

return navigateTo(payUrl, { external: true })
}
1 Reply
dwol
dwolOP7mo ago
For anyone else potentially wondering, I found this solution online.
const { navigateToMock } = vi.hoisted(() => ({ navigateToMock: vi.fn() }))
mockNuxtImport('navigateTo', () => navigateToMock)

describe('handlePaymentRedirect', () => {
it('should call navigateTo with the correct url', async () => {
await handlePaymentRedirect(123, 456)
const returnUrl = encodeURIComponent('https://baseurl.com/en-CA/submitted?filing_id=456')
const payUrl = 'https://paymentportal.com/' + '123' + '/' + returnUrl
expect(vi.mocked(navigateTo)).toBeCalledWith(payUrl, {
external: true
})
})
})
const { navigateToMock } = vi.hoisted(() => ({ navigateToMock: vi.fn() }))
mockNuxtImport('navigateTo', () => navigateToMock)

describe('handlePaymentRedirect', () => {
it('should call navigateTo with the correct url', async () => {
await handlePaymentRedirect(123, 456)
const returnUrl = encodeURIComponent('https://baseurl.com/en-CA/submitted?filing_id=456')
const payUrl = 'https://paymentportal.com/' + '123' + '/' + returnUrl
expect(vi.mocked(navigateTo)).toBeCalledWith(payUrl, {
external: true
})
})
})
Want results from more Discord servers?
Add your server