Skip to content

Commit

Permalink
feat: cleanup guards in reset
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 26, 2021
1 parent eccffb2 commit 3e07bd3
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
} = options
const initialLocation = options.initialLocation || START_LOCATION

const { push, addRoute, replace } = router
const { push, addRoute, replace, beforeEach, beforeResolve } = router

const addRouteMock = jest.fn(
(
Expand Down Expand Up @@ -181,11 +181,26 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
router.replace = replaceMock
router.addRoute = addRouteMock

let guardRemovers: Array<() => void> = []
router.beforeEach = (...args) => {
const removeGuard = beforeEach(...args)
guardRemovers.push(removeGuard)
return removeGuard
}
router.beforeResolve = (...args) => {
const removeGuard = beforeResolve(...args)
guardRemovers.push(removeGuard)
return removeGuard
}

function reset() {
pushMock.mockClear()
replaceMock.mockClear()
addRouteMock.mockClear()

guardRemovers.forEach((remove) => remove())
guardRemovers = []

nextReturn = undefined
router.currentRoute.value =
initialLocation === START_LOCATION
Expand Down

0 comments on commit 3e07bd3

Please sign in to comment.