Skip to content

Commit

Permalink
feat: spy on push
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 10, 2020
1 parent b875cc7 commit 20f5a19
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
23 changes: 23 additions & 0 deletions __tests__/navigations.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { mount } from '@vue/test-utils'
import { addGlobalInjections, createMockedRouter } from '../src'
import Test from './fixtures/Test'

describe('Navigations', () => {
beforeAll(() => {
const router = createMockedRouter()
addGlobalInjections(router)
})

it('can check calls', async () => {
const wrapper = mount(Test)

wrapper.vm.$router.push('/hey')
expect(wrapper.vm.$router.push).toHaveBeenCalledWith('/hey')
expect(wrapper.vm.$router.push).toHaveBeenCalledTimes(1)
})

it('reset calls between tests', async () => {
const wrapper = mount(Test)
expect(wrapper.vm.$router.push).toHaveBeenCalledTimes(0)
})
})
9 changes: 7 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
RouteLocationNormalizedLoaded,
Router,
routerKey,
START_LOCATION,
} from 'vue-router'
import { config, VueWrapper } from '@vue/test-utils'
import { computed, ComputedRef, reactive, Ref } from 'vue'
Expand Down Expand Up @@ -50,12 +49,18 @@ export function createMockedRouter() {
],
})

router.push = jest.fn((to) => {
const pushMock = jest.fn((to) => {
router.currentRoute.value = router.resolve(to)
// resolve pending navigation failure
return Promise.resolve()
})

router.push = pushMock

beforeEach(() => {
pushMock.mockClear()
})

// TODO: replace

return router
Expand Down

0 comments on commit 20f5a19

Please sign in to comment.