From: Eduardo San Martin Morote Date: Sat, 5 Sep 2020 14:29:25 +0000 (+0200) Subject: test: test about extending RouterLink X-Git-Tag: v4.0.0-beta.10~62 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=244b5fdea147a1790b6aac1fa2dc7cbe9d67431b;p=thirdparty%2Fvuejs%2Frouter.git test: test about extending RouterLink --- diff --git a/__tests__/RouterLink.spec.ts b/__tests__/RouterLink.spec.ts index 1b7606c4..c2af1951 100644 --- a/__tests__/RouterLink.spec.ts +++ b/__tests__/RouterLink.spec.ts @@ -1,7 +1,7 @@ /** * @jest-environment jsdom */ -import { RouterLink } from '../src/RouterLink' +import { RouterLink, RouterLinkProps } from '../src/RouterLink' import { START_LOCATION_NORMALIZED, RouteQueryAndHash, @@ -10,7 +10,7 @@ import { } from '../src/types' import { createMemoryHistory, RouterOptions } from '../src' import { mount, createMockedRoute } from './mount' -import { nextTick } from 'vue' +import { defineComponent, nextTick, PropType } from 'vue' import { RouteRecordNormalized } from '../src/matcher/types' import { routerKey } from '../src/injectionSymbols' import { tick } from './utils' @@ -858,5 +858,97 @@ describe('RouterLink', () => { expect(wrapper.html()).not.toContain('') }) + + describe('Extending RouterLink', () => { + const AppLink = defineComponent({ + template: ` + + + + + + + + + `, + components: { RouterLink }, + name: 'AppLink', + + // @ts-ignore + props: { + ...((RouterLink as any).props as RouterLinkProps), + inactiveClass: String as PropType, + }, + + computed: { + isExternalLink(): boolean { + // @ts-ignore + return typeof this.to === 'string' && this.to.startsWith('http') + }, + }, + }) + + async function factoryCustom( + currentLocation: RouteLocationNormalized, + propsData: any, + resolvedLocation: RouteLocationResolved, + slotTemplate: string = '' + ) { + const route = createMockedRoute(currentLocation) + const router = { + history: createMemoryHistory(), + createHref(to: RouteLocationNormalized): string { + return this.history.base + to.fullPath + }, + options: {} as Partial, + resolve: jest.fn(), + push: jest.fn().mockResolvedValue(resolvedLocation), + } + router.resolve.mockReturnValueOnce(resolvedLocation) + + const wrapper = await mount(AppLink, { + propsData, + provide: { + [routerKey as any]: router, + ...route.provides, + }, + slots: { default: slotTemplate }, + }) + + return { router, wrapper, route } + } + + it('can extend RouterLink with inactive class', async () => { + const { wrapper } = await factoryCustom( + locations.basic.normalized, + { + to: locations.basic.string, + inactiveClass: 'inactive', + activeClass: 'active', + }, + locations.foo.normalized + ) + + expect(wrapper.find('a')!.className).toEqual('inactive') + }) + + it('can extend RouterLink with external link', async () => { + const { wrapper } = await factoryCustom( + locations.basic.normalized, + { + to: 'https://esm.dev', + }, + locations.foo.normalized + ) + + expect(wrapper.find('a')!.className).toEqual('') + expect(wrapper.find('a')!.href).toEqual('https://esm.dev/') + }) + }) }) })