Skip to content

Commit

Permalink
test: improve for #1929
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Jul 18, 2023
1 parent 7f35d91 commit a1611b6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
10 changes: 8 additions & 2 deletions packages/router/__tests__/errors.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
START_LOCATION_NORMALIZED,
RouteLocationNormalized,
} from '../src/types'
import { mockWarn } from 'jest-mock-warn'

const routes: Readonly<RouteRecordRaw>[] = [
{ path: '/', component: components.Home },
Expand All @@ -40,6 +41,7 @@ function createRouter() {
}

describe('Errors & Navigation failures', () => {
mockWarn()
beforeEach(() => {
onError.mockReset()
afterEach.mockReset()
Expand Down Expand Up @@ -158,7 +160,7 @@ describe('Errors & Navigation failures', () => {
throw error
})

await expect(router.push('/foo')).rejects.toEqual(error)
await router.push('/foo').catch(() => {})

expect(afterEach).toHaveBeenCalledTimes(0)
expect(onError).toHaveBeenCalledTimes(1)
Expand Down Expand Up @@ -352,7 +354,11 @@ async function testError(
}
)

await expect(router.push(to)).rejects.toEqual(expectedError)
if (expectedError !== undefined) {
await expect(router.push(to)).rejects.toEqual(expectedError)
} else {
await router.push(to).catch(() => {})
}

expect(afterEach).toHaveBeenCalledTimes(0)
expect(onError).toHaveBeenCalledTimes(1)
Expand Down
2 changes: 1 addition & 1 deletion packages/router/__tests__/lazyLoading.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ describe('Lazy Loading', () => {
reject()
await router.push('/foo').catch(spy)

expect(spy).toHaveBeenCalled()
expect(spy).toHaveBeenCalledTimes(1)
expect('uncaught error').toHaveBeenWarned()

expect(router.currentRoute.value).toMatchObject({
Expand Down
27 changes: 16 additions & 11 deletions packages/router/src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ import { addDevtools } from './devtools'
* @param from - location we were navigating from when the error happened
* @internal
*/
export type _ErrorHandler = (
error: any,
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded
) => any
export interface _ErrorListener {
(
error: any,
to: RouteLocationNormalized,
from: RouteLocationNormalizedLoaded
): any
}
// resolve, reject arguments of Promise constructor
type OnReadyCallback = [() => void, (reason?: any) => void]

Expand Down Expand Up @@ -321,7 +323,7 @@ export interface Router {
*
* @param handler - error handler to register
*/
onError(handler: _ErrorHandler): () => void
onError(handler: _ErrorListener): () => void
/**
* Returns a Promise that resolves when the router has completed the initial
* navigation, which means it has resolved all async enter hooks and async
Expand Down Expand Up @@ -1082,18 +1084,19 @@ export function createRouter(options: RouterOptions): Router {
failure
)
})
// avoid warnings in the console about uncaught rejections, they are logged by triggerErrors
.catch(noop)
})
}

// Initialization and Errors

let readyHandlers = useCallbacks<OnReadyCallback>()
let errorHandlers = useCallbacks<_ErrorHandler>()
let errorListeners = useCallbacks<_ErrorListener>()
let ready: boolean

/**
* Trigger errorHandlers added via onError and throws the error as well
* Trigger errorListeners added via onError and throws the error as well
*
* @param error - error to throw
* @param to - location we were navigating to when the error happened
Expand All @@ -1106,7 +1109,7 @@ export function createRouter(options: RouterOptions): Router {
from: RouteLocationNormalizedLoaded
): Promise<unknown> {
markAsReady(error)
const list = errorHandlers.list()
const list = errorListeners.list()
if (list.length) {
list.forEach(handler => handler(error, to, from))
} else {
Expand All @@ -1115,6 +1118,7 @@ export function createRouter(options: RouterOptions): Router {
}
console.error(error)
}
// reject the error no matter there were error listeners or not
return Promise.reject(error)
}

Expand Down Expand Up @@ -1152,7 +1156,8 @@ export function createRouter(options: RouterOptions): Router {
from: RouteLocationNormalizedLoaded,
isPush: boolean,
isFirstNavigation: boolean
): Promise<any> {
): // the return is not meant to be used
Promise<unknown> {
const { scrollBehavior } = options
if (!isBrowser || !scrollBehavior) return Promise.resolve()

Expand Down Expand Up @@ -1195,7 +1200,7 @@ export function createRouter(options: RouterOptions): Router {
beforeResolve: beforeResolveGuards.add,
afterEach: afterGuards.add,

onError: errorHandlers.add,
onError: errorListeners.add,
isReady,

install(app: App) {
Expand Down

0 comments on commit a1611b6

Please sign in to comment.