Skip to content

Commit

Permalink
feat: stub router link
Browse files Browse the repository at this point in the history
  • Loading branch information
posva committed Nov 11, 2020
1 parent 5fc6beb commit 83125d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
17 changes: 15 additions & 2 deletions __tests__/components.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ describe('components', () => {
it('stubs router link', async () => {
const wrapper = mount(
{
template: `<router-link>Hello</router-link>`,
template: `<router-link to="/">Hello</router-link>`,
}
// { global: { stubs: { RouterLink: true } } }
)

expect(wrapper.html()).toMatchInlineSnapshot(
`"<router-link>Hello</router-link>"`
`"<router-link-stub></router-link-stub>"`
)
})

it('can use real router-link', async () => {
const wrapper = mount(
{
template: `<router-link to="/about">About</router-link>`,
},
{ global: { stubs: { RouterLink: false } } }
)

expect(wrapper.html()).toMatchInlineSnapshot(
`"<a href=\\"/about\\" class=\\"\\">About</a>"`
)
})
})
29 changes: 26 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import {
createMemoryHistory,
createRouter,
createWebHashHistory,
routeLocationKey,
RouteLocationNormalizedLoaded,
RouteLocationRaw,
Router,
routerKey,
RouterLink,
RouterView,
} from 'vue-router'
import { config, VueWrapper } from '@vue/test-utils'
import { computed, ComputedRef, reactive, Ref } from 'vue'
Expand All @@ -28,15 +30,36 @@ export function addGlobalInjections(router: Router) {
config.global.mocks.$router = router
config.global.mocks.$route = route

config.global.components.RouterView = RouterView
config.global.components.RouterLink = RouterLink

config.global.stubs.RouterLink = true
config.global.stubs.RouterView = true

return { router, route }
}

export function createMockedRouter() {
export interface RouterMock extends Router {
/**
* Set a value to be returned on a navigation guard for the next navigation.
*
* @param returnValue value that will be returned on a simulated navigation
* guard
*/
setNextGuardReturn(
returnValue: Error | boolean | RouteLocationRaw | undefined
): void

/**
* Returns a Promise of the pending navigation. Resolves right away if there
* isn't any.
*/
getPendingNavigation(): ReturnType<Router['push']>
}

export function createMockedRouter(): RouterMock {
const router = createRouter({
history: createWebHashHistory(),
history: createMemoryHistory(),
routes: [
{
path: '/:pathMatch(.*)*',
Expand Down

0 comments on commit 83125d8

Please sign in to comment.