From 83125d88c351ff2e740ff02852f51a57a51f4616 Mon Sep 17 00:00:00 2001 From: Eduardo San Martin Morote Date: Wed, 11 Nov 2020 16:21:13 +0100 Subject: [PATCH] feat: stub router link --- __tests__/components.spec.ts | 17 +++++++++++++++-- src/index.ts | 29 ++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/__tests__/components.spec.ts b/__tests__/components.spec.ts index 4c73545..061a37c 100644 --- a/__tests__/components.spec.ts +++ b/__tests__/components.spec.ts @@ -10,13 +10,26 @@ describe('components', () => { it('stubs router link', async () => { const wrapper = mount( { - template: `Hello`, + template: `Hello`, } // { global: { stubs: { RouterLink: true } } } ) expect(wrapper.html()).toMatchInlineSnapshot( - `"Hello"` + `""` + ) + }) + + it('can use real router-link', async () => { + const wrapper = mount( + { + template: `About`, + }, + { global: { stubs: { RouterLink: false } } } + ) + + expect(wrapper.html()).toMatchInlineSnapshot( + `"About"` ) }) }) diff --git a/src/index.ts b/src/index.ts index 2bfad98..a37c4d9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' @@ -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 +} + +export function createMockedRouter(): RouterMock { const router = createRouter({ - history: createWebHashHistory(), + history: createMemoryHistory(), routes: [ { path: '/:pathMatch(.*)*',