Skip to content

Commit 3e07bd3

Browse files
committed
feat: cleanup guards in reset
1 parent eccffb2 commit 3e07bd3

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/router.ts

+16-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
149149
} = options
150150
const initialLocation = options.initialLocation || START_LOCATION
151151

152-
const { push, addRoute, replace } = router
152+
const { push, addRoute, replace, beforeEach, beforeResolve } = router
153153

154154
const addRouteMock = jest.fn(
155155
(
@@ -181,11 +181,26 @@ export function createRouterMock(options: RouterMockOptions = {}): RouterMock {
181181
router.replace = replaceMock
182182
router.addRoute = addRouteMock
183183

184+
let guardRemovers: Array<() => void> = []
185+
router.beforeEach = (...args) => {
186+
const removeGuard = beforeEach(...args)
187+
guardRemovers.push(removeGuard)
188+
return removeGuard
189+
}
190+
router.beforeResolve = (...args) => {
191+
const removeGuard = beforeResolve(...args)
192+
guardRemovers.push(removeGuard)
193+
return removeGuard
194+
}
195+
184196
function reset() {
185197
pushMock.mockClear()
186198
replaceMock.mockClear()
187199
addRouteMock.mockClear()
188200

201+
guardRemovers.forEach((remove) => remove())
202+
guardRemovers = []
203+
189204
nextReturn = undefined
190205
router.currentRoute.value =
191206
initialLocation === START_LOCATION

0 commit comments

Comments
 (0)