NAVIGATION_TYPES.forEach(navigationMethod => {
describe(navigationMethod, () => {
- it('calls beforeEach guards on navigation', () => {
+ it('calls beforeEach guards on navigation', async () => {
const spy = jest.fn()
const router = createRouter({ routes })
router.beforeEach(spy)
- router[navigationMethod]('/foo')
+ spy.mockImplementationOnce((to, from, next) => {
+ next()
+ })
+ await router[navigationMethod]('/foo')
expect(spy).toHaveBeenCalledTimes(1)
})
const [p1, r1] = fakePromise()
const [p2, r2] = fakePromise()
const router = createRouter({ routes })
- const guard1 = jest.fn(async (to, from, next) => {
+ const guard1 = jest.fn()
+ let order = 0
+ guard1.mockImplementationOnce(async (to, from, next) => {
+ expect(order++).toBe(0)
await p1
next()
})
router.beforeEach(guard1)
- const guard2 = jest.fn(async (to, from, next) => {
+ const guard2 = jest.fn()
+ guard2.mockImplementationOnce(async (to, from, next) => {
+ expect(order++).toBe(1)
await p2
next()
})
router.beforeEach(guard2)
let navigation = router[navigationMethod]('/foo')
expect(router.currentRoute.fullPath).toBe('/')
- expect(guard1).toHaveBeenCalled()
+ expect(guard1).not.toHaveBeenCalled()
expect(guard2).not.toHaveBeenCalled()
- r1()
- // wait until the guard is called
- await tick()
- await tick()
- expect(guard2).toHaveBeenCalled()
- r2()
+ r1() // resolve the first guard
+ await tick() // wait a tick
+ expect(guard1).toHaveBeenCalled()
+ // we haven't resolved the second gurad yet
expect(router.currentRoute.fullPath).toBe('/')
+ r2()
await navigation
expect(guard2).toHaveBeenCalled()
expect(router.currentRoute.fullPath).toBe('/foo')