|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + */ |
| 4 | +import { nextTick, ref } from 'vue' |
| 5 | +import { mount } from '@vue/test-utils' |
| 6 | +import { mockWarn } from 'jest-mock-warn' |
| 7 | +import { |
| 8 | + createMemoryHistory, |
| 9 | + createRouter, |
| 10 | + routeLocationKey, |
| 11 | + RouteLocationRaw, |
| 12 | + routerKey, |
| 13 | + useLink, |
| 14 | + UseLinkOptions, |
| 15 | +} from '../src' |
| 16 | + |
| 17 | +async function callUseLink(args: UseLinkOptions) { |
| 18 | + const router = createRouter({ |
| 19 | + history: createMemoryHistory(), |
| 20 | + routes: [ |
| 21 | + { |
| 22 | + path: '/', |
| 23 | + component: {}, |
| 24 | + name: 'root', |
| 25 | + }, |
| 26 | + { |
| 27 | + path: '/a', |
| 28 | + component: {}, |
| 29 | + name: 'a', |
| 30 | + }, |
| 31 | + { |
| 32 | + path: '/b', |
| 33 | + component: {}, |
| 34 | + name: 'b', |
| 35 | + }, |
| 36 | + ], |
| 37 | + }) |
| 38 | + |
| 39 | + await router.push('/') |
| 40 | + |
| 41 | + let link: ReturnType<typeof useLink> |
| 42 | + |
| 43 | + mount( |
| 44 | + { |
| 45 | + setup() { |
| 46 | + link = useLink(args) |
| 47 | + |
| 48 | + return () => '' |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + global: { |
| 53 | + provide: { |
| 54 | + [routerKey as any]: router, |
| 55 | + [routeLocationKey as any]: router.currentRoute.value, |
| 56 | + }, |
| 57 | + }, |
| 58 | + } |
| 59 | + ) |
| 60 | + |
| 61 | + return link! |
| 62 | +} |
| 63 | + |
| 64 | +describe('useLink', () => { |
| 65 | + describe('basic usage', () => { |
| 66 | + it('supports a string for "to"', async () => { |
| 67 | + const { href, route } = await callUseLink({ |
| 68 | + to: '/a', |
| 69 | + }) |
| 70 | + |
| 71 | + expect(href.value).toBe('/a') |
| 72 | + expect(route.value).toMatchObject({ name: 'a' }) |
| 73 | + }) |
| 74 | + |
| 75 | + it('supports an object for "to"', async () => { |
| 76 | + const { href, route } = await callUseLink({ |
| 77 | + to: { path: '/a' }, |
| 78 | + }) |
| 79 | + |
| 80 | + expect(href.value).toBe('/a') |
| 81 | + expect(route.value).toMatchObject({ name: 'a' }) |
| 82 | + }) |
| 83 | + |
| 84 | + it('supports a ref for "to"', async () => { |
| 85 | + const to = ref<RouteLocationRaw>('/a') |
| 86 | + |
| 87 | + const { href, route } = await callUseLink({ |
| 88 | + to, |
| 89 | + }) |
| 90 | + |
| 91 | + expect(href.value).toBe('/a') |
| 92 | + expect(route.value).toMatchObject({ name: 'a' }) |
| 93 | + |
| 94 | + to.value = { path: '/b' } |
| 95 | + |
| 96 | + await nextTick() |
| 97 | + |
| 98 | + expect(href.value).toBe('/b') |
| 99 | + expect(route.value).toMatchObject({ name: 'b' }) |
| 100 | + }) |
| 101 | + }) |
| 102 | + |
| 103 | + describe('warnings', () => { |
| 104 | + mockWarn() |
| 105 | + |
| 106 | + it('should warn when "to" is undefined', async () => { |
| 107 | + await callUseLink({ |
| 108 | + to: undefined as any, |
| 109 | + }) |
| 110 | + |
| 111 | + expect('Invalid value for prop "to" in useLink()').toHaveBeenWarned() |
| 112 | + expect( |
| 113 | + 'router.resolve() was passed an invalid location' |
| 114 | + ).toHaveBeenWarned() |
| 115 | + }) |
| 116 | + |
| 117 | + it('should warn when "to" is an undefined ref', async () => { |
| 118 | + await callUseLink({ |
| 119 | + to: ref(undefined as any), |
| 120 | + }) |
| 121 | + |
| 122 | + expect('Invalid value for prop "to" in useLink()').toHaveBeenWarned() |
| 123 | + expect( |
| 124 | + 'router.resolve() was passed an invalid location' |
| 125 | + ).toHaveBeenWarned() |
| 126 | + }) |
| 127 | + |
| 128 | + it('should warn when "to" changes to a null ref', async () => { |
| 129 | + const to = ref('/a') |
| 130 | + |
| 131 | + const { href, route } = await callUseLink({ |
| 132 | + to, |
| 133 | + }) |
| 134 | + |
| 135 | + expect(href.value).toBe('/a') |
| 136 | + expect(route.value).toMatchObject({ name: 'a' }) |
| 137 | + |
| 138 | + to.value = null as any |
| 139 | + |
| 140 | + await nextTick() |
| 141 | + |
| 142 | + expect('Invalid value for prop "to" in useLink()').toHaveBeenWarned() |
| 143 | + expect( |
| 144 | + 'router.resolve() was passed an invalid location' |
| 145 | + ).toHaveBeenWarned() |
| 146 | + }) |
| 147 | + }) |
| 148 | +}) |
0 commit comments