|
| 1 | +/** |
| 2 | + * @vitest-environment happy-dom |
| 3 | + */ |
| 4 | +import { enableAutoUnmount, flushPromises, mount } from '@vue/test-utils' |
| 5 | +import { defineComponent, h, ref } from 'vue' |
| 6 | +import { afterEach, describe, expect, it } from 'vitest' |
| 7 | +import { experimental_createRouter, normalizeRouteRecord } from './router' |
| 8 | +import { createFixedResolver } from './route-resolver/resolver-fixed' |
| 9 | +import { |
| 10 | + MatcherPatternPathDynamic, |
| 11 | + MatcherPatternPathStatic, |
| 12 | +} from './route-resolver/matchers/matcher-pattern' |
| 13 | +import { MatcherPatternQueryParam } from './route-resolver/matchers/matcher-pattern-query' |
| 14 | +import { PARAM_PARSER_INT } from './route-resolver/matchers/param-parsers' |
| 15 | +import { createMemoryHistory } from '../history/memory' |
| 16 | +import { RouterView } from '../RouterView' |
| 17 | +import { RouterLink, useLink, type UseLinkOptions } from './RouterLink' |
| 18 | +import { mockWarn } from '../tests/vitest-mock-warn' |
| 19 | + |
| 20 | +enableAutoUnmount(afterEach) |
| 21 | +mockWarn() |
| 22 | + |
| 23 | +const Page = defineComponent({ render: () => h('p', 'page') }) |
| 24 | +const Layout = defineComponent({ render: () => h(RouterView) }) |
| 25 | + |
| 26 | +function createTestRouter(options: { linkActiveClass?: string } = {}) { |
| 27 | + const home = normalizeRouteRecord({ |
| 28 | + name: 'home', |
| 29 | + path: new MatcherPatternPathStatic('/'), |
| 30 | + components: { default: Page }, |
| 31 | + }) |
| 32 | + const users = normalizeRouteRecord({ |
| 33 | + name: 'users', |
| 34 | + path: new MatcherPatternPathStatic('/users'), |
| 35 | + components: { default: Layout }, |
| 36 | + }) |
| 37 | + // index child: shares the path matcher of its parent |
| 38 | + const usersIndex = normalizeRouteRecord({ |
| 39 | + name: 'users-index', |
| 40 | + path: users.path, |
| 41 | + components: { default: Page }, |
| 42 | + parent: users, |
| 43 | + }) |
| 44 | + const user = normalizeRouteRecord({ |
| 45 | + name: 'user', |
| 46 | + path: new MatcherPatternPathDynamic( |
| 47 | + /^\/users\/([^/]+?)$/i, |
| 48 | + { id: [PARAM_PARSER_INT] }, |
| 49 | + ['users', 1] |
| 50 | + ), |
| 51 | + components: { default: Page }, |
| 52 | + parent: users, |
| 53 | + }) |
| 54 | + const about = normalizeRouteRecord({ |
| 55 | + name: 'about', |
| 56 | + path: new MatcherPatternPathStatic('/about'), |
| 57 | + components: { default: Page }, |
| 58 | + }) |
| 59 | + const aboutAlias = normalizeRouteRecord({ |
| 60 | + ...about, |
| 61 | + path: new MatcherPatternPathStatic('/about-alias'), |
| 62 | + aliasOf: about, |
| 63 | + }) |
| 64 | + const search = normalizeRouteRecord({ |
| 65 | + name: 'search', |
| 66 | + path: new MatcherPatternPathStatic('/search'), |
| 67 | + query: [ |
| 68 | + new MatcherPatternQueryParam( |
| 69 | + 'page', |
| 70 | + 'page', |
| 71 | + 'value', |
| 72 | + PARAM_PARSER_INT, |
| 73 | + 1 |
| 74 | + ), |
| 75 | + ], |
| 76 | + components: { default: Page }, |
| 77 | + }) |
| 78 | + |
| 79 | + return experimental_createRouter({ |
| 80 | + history: createMemoryHistory(), |
| 81 | + resolver: createFixedResolver([ |
| 82 | + home, |
| 83 | + usersIndex, |
| 84 | + users, |
| 85 | + user, |
| 86 | + about, |
| 87 | + aboutAlias, |
| 88 | + search, |
| 89 | + ]), |
| 90 | + ...options, |
| 91 | + }) |
| 92 | +} |
| 93 | + |
| 94 | +async function factory( |
| 95 | + template: string, |
| 96 | + { |
| 97 | + initial = '/', |
| 98 | + router = createTestRouter(), |
| 99 | + }: { initial?: string; router?: ReturnType<typeof createTestRouter> } = {} |
| 100 | +) { |
| 101 | + await router.push(initial) |
| 102 | + const wrapper = mount( |
| 103 | + defineComponent({ components: { RouterLink, RouterView }, template }), |
| 104 | + { global: { plugins: [router] } } |
| 105 | + ) |
| 106 | + await flushPromises() |
| 107 | + const go = async (to: string) => { |
| 108 | + await router.push(to) |
| 109 | + await flushPromises() |
| 110 | + } |
| 111 | + return { router, wrapper, go } |
| 112 | +} |
| 113 | + |
| 114 | +describe('experimental RouterLink', () => { |
| 115 | + it('renders the href of absolute and named locations', async () => { |
| 116 | + const { wrapper } = await factory(` |
| 117 | + <RouterLink id="a" to="/about">a</RouterLink> |
| 118 | + <RouterLink id="b" :to="{ name: 'user', params: { id: 2 } }">b</RouterLink> |
| 119 | + <RouterLink id="c" :to="{ path: '/search', query: { page: ['3'] } }">c</RouterLink> |
| 120 | + `) |
| 121 | + expect(wrapper.find('#a').attributes('href')).toBe('/about') |
| 122 | + expect(wrapper.find('#b').attributes('href')).toBe('/users/2') |
| 123 | + expect(wrapper.find('#c').attributes('href')).toBe('/search?page=3') |
| 124 | + }) |
| 125 | + |
| 126 | + it('sets exact active classes and aria-current on the current route', async () => { |
| 127 | + const { wrapper, go } = await factory( |
| 128 | + `<RouterLink to="/about">a</RouterLink>` |
| 129 | + ) |
| 130 | + const a = () => wrapper.find('a') |
| 131 | + expect(a().classes()).toEqual([]) |
| 132 | + expect(a().attributes('aria-current')).toBeUndefined() |
| 133 | + |
| 134 | + await go('/about') |
| 135 | + expect(a().classes()).toEqual([ |
| 136 | + 'router-link-active', |
| 137 | + 'router-link-exact-active', |
| 138 | + ]) |
| 139 | + expect(a().attributes('aria-current')).toBe('page') |
| 140 | + |
| 141 | + await go('/') |
| 142 | + expect(a().classes()).toEqual([]) |
| 143 | + }) |
| 144 | + |
| 145 | + it('is active but not exact active on a child route', async () => { |
| 146 | + const { wrapper } = await factory( |
| 147 | + `<RouterLink :to="{ name: 'users' }">users</RouterLink>`, |
| 148 | + { initial: '/users/2' } |
| 149 | + ) |
| 150 | + expect(wrapper.find('a').classes()).toEqual(['router-link-active']) |
| 151 | + }) |
| 152 | + |
| 153 | + it('compares params', async () => { |
| 154 | + const { wrapper, go } = await factory( |
| 155 | + `<RouterLink to="/users/1">1</RouterLink>`, |
| 156 | + { initial: '/users/2' } |
| 157 | + ) |
| 158 | + expect(wrapper.find('a').classes()).toEqual([]) |
| 159 | + await go('/users/1') |
| 160 | + expect(wrapper.find('a').classes()).toContain('router-link-exact-active') |
| 161 | + }) |
| 162 | + |
| 163 | + describe('index child', () => { |
| 164 | + it('is active through the parent on a sibling route', async () => { |
| 165 | + const { wrapper, go } = await factory( |
| 166 | + `<RouterLink to="/users">users</RouterLink>`, |
| 167 | + { initial: '/users/2' } |
| 168 | + ) |
| 169 | + expect(wrapper.find('a').classes()).toEqual(['router-link-active']) |
| 170 | + await go('/users') |
| 171 | + expect(wrapper.find('a').classes()).toEqual([ |
| 172 | + 'router-link-active', |
| 173 | + 'router-link-exact-active', |
| 174 | + ]) |
| 175 | + }) |
| 176 | + |
| 177 | + it('is not active when the parent itself is displayed', async () => { |
| 178 | + const { wrapper, router } = await factory( |
| 179 | + `<RouterLink to="/users">users</RouterLink>` |
| 180 | + ) |
| 181 | + await router.push({ name: 'users' }) |
| 182 | + await flushPromises() |
| 183 | + expect(router.currentRoute.value.matched).toHaveLength(1) |
| 184 | + expect(wrapper.find('a').classes()).toEqual([]) |
| 185 | + }) |
| 186 | + }) |
| 187 | + |
| 188 | + it('is exact active on an alias of the target', async () => { |
| 189 | + const { wrapper } = await factory( |
| 190 | + `<RouterLink to="/about">a</RouterLink>`, |
| 191 | + { |
| 192 | + initial: '/about-alias', |
| 193 | + } |
| 194 | + ) |
| 195 | + expect(wrapper.find('a').classes()).toContain('router-link-exact-active') |
| 196 | + }) |
| 197 | + |
| 198 | + it('compares declared query params but ignores other query values', async () => { |
| 199 | + const { wrapper } = await factory( |
| 200 | + ` |
| 201 | + <RouterLink id="p2" to="/search?page=2">2</RouterLink> |
| 202 | + <RouterLink id="p3" to="/search?page=3">3</RouterLink> |
| 203 | + <RouterLink id="other" to="/search?page=3&q=vue">3</RouterLink> |
| 204 | + `, |
| 205 | + { initial: '/search?page=3' } |
| 206 | + ) |
| 207 | + expect(wrapper.find('#p2').classes()).toEqual([]) |
| 208 | + expect(wrapper.find('#p3').classes()).toContain('router-link-exact-active') |
| 209 | + expect(wrapper.find('#other').classes()).toContain( |
| 210 | + 'router-link-exact-active' |
| 211 | + ) |
| 212 | + }) |
| 213 | + |
| 214 | + it('uses the class props, then the router options', async () => { |
| 215 | + const { wrapper } = await factory( |
| 216 | + ` |
| 217 | + <RouterLink id="a" to="/about">a</RouterLink> |
| 218 | + <RouterLink id="b" to="/about" active-class="on" exact-active-class="exact">b</RouterLink> |
| 219 | + `, |
| 220 | + { |
| 221 | + initial: '/about', |
| 222 | + router: createTestRouter({ linkActiveClass: 'global-on' }), |
| 223 | + } |
| 224 | + ) |
| 225 | + expect(wrapper.find('#a').classes()).toEqual([ |
| 226 | + 'global-on', |
| 227 | + 'router-link-exact-active', |
| 228 | + ]) |
| 229 | + expect(wrapper.find('#b').classes()).toEqual(['on', 'exact']) |
| 230 | + }) |
| 231 | + |
| 232 | + it('navigates on click', async () => { |
| 233 | + const { wrapper, router } = await factory( |
| 234 | + `<RouterLink to="/about">a</RouterLink>` |
| 235 | + ) |
| 236 | + await wrapper.find('a').trigger('click') |
| 237 | + await flushPromises() |
| 238 | + expect(router.currentRoute.value.path).toBe('/about') |
| 239 | + }) |
| 240 | + |
| 241 | + it('does not navigate on a click with a modifier key', async () => { |
| 242 | + const { wrapper, router } = await factory( |
| 243 | + `<RouterLink to="/about">a</RouterLink>` |
| 244 | + ) |
| 245 | + await wrapper.find('a').trigger('click', { ctrlKey: true }) |
| 246 | + await flushPromises() |
| 247 | + expect(router.currentRoute.value.path).toBe('/') |
| 248 | + }) |
| 249 | + |
| 250 | + it('replaces the current entry with replace', async () => { |
| 251 | + const { wrapper, router, go } = await factory( |
| 252 | + `<RouterLink to="/about" replace>a</RouterLink>` |
| 253 | + ) |
| 254 | + await go('/users/1') |
| 255 | + await wrapper.find('a').trigger('click') |
| 256 | + await flushPromises() |
| 257 | + expect(router.currentRoute.value.path).toBe('/about') |
| 258 | + router.back() |
| 259 | + await flushPromises() |
| 260 | + expect(router.currentRoute.value.path).toBe('/') |
| 261 | + }) |
| 262 | + |
| 263 | + it('renders only the slot with custom', async () => { |
| 264 | + const { wrapper } = await factory( |
| 265 | + ` |
| 266 | + <RouterLink to="/about" custom v-slot="{ href, isActive, isExactActive, route }"> |
| 267 | + <span>{{ href }} {{ isActive }} {{ isExactActive }} {{ route.name }}</span> |
| 268 | + </RouterLink> |
| 269 | + `, |
| 270 | + { initial: '/about' } |
| 271 | + ) |
| 272 | + expect(wrapper.find('a').exists()).toBe(false) |
| 273 | + expect(wrapper.html()).toBe('<span>/about true true about</span>') |
| 274 | + }) |
| 275 | + |
| 276 | + it('compares against the route of the closest RouterView', async () => { |
| 277 | + const router = createTestRouter() |
| 278 | + await router.push('/users/1') |
| 279 | + const shown = router.resolve('/users/2') |
| 280 | + const Child = defineComponent({ |
| 281 | + components: { RouterLink }, |
| 282 | + template: `<RouterLink to="/users/2">2</RouterLink>`, |
| 283 | + }) |
| 284 | + const wrapper = mount( |
| 285 | + defineComponent({ |
| 286 | + components: { RouterView, Child }, |
| 287 | + setup: () => ({ shown }), |
| 288 | + template: `<RouterView :route="shown"><Child /></RouterView>`, |
| 289 | + }), |
| 290 | + { global: { plugins: [router] } } |
| 291 | + ) |
| 292 | + await flushPromises() |
| 293 | + expect(wrapper.find('a').classes()).toContain('router-link-exact-active') |
| 294 | + }) |
| 295 | + |
| 296 | + it('warns on an invalid to', async () => { |
| 297 | + const router = createTestRouter() |
| 298 | + await router.push('/') |
| 299 | + // throws after the warning, as in production |
| 300 | + expect(() => |
| 301 | + mount( |
| 302 | + defineComponent({ |
| 303 | + components: { RouterLink }, |
| 304 | + template: `<RouterLink :to="null">a</RouterLink>`, |
| 305 | + }), |
| 306 | + { global: { plugins: [router] } } |
| 307 | + ) |
| 308 | + ).toThrow() |
| 309 | + expect('VUE_ROUTER_R0050').toHaveBeenWarned() |
| 310 | + expect('Invalid prop').toHaveBeenWarned() |
| 311 | + }) |
| 312 | + |
| 313 | + it.todo('resolves relative locations against the current route') |
| 314 | +}) |
| 315 | + |
| 316 | +describe('experimental useLink', () => { |
| 317 | + async function withLink(options: UseLinkOptions, initial = '/') { |
| 318 | + let link!: ReturnType<typeof useLink> |
| 319 | + const router = createTestRouter() |
| 320 | + await router.push(initial) |
| 321 | + mount( |
| 322 | + defineComponent({ |
| 323 | + setup() { |
| 324 | + link = useLink(options) |
| 325 | + return () => null |
| 326 | + }, |
| 327 | + }), |
| 328 | + { global: { plugins: [router] } } |
| 329 | + ) |
| 330 | + return { link, router } |
| 331 | + } |
| 332 | + |
| 333 | + it('follows a ref and a getter', async () => { |
| 334 | + const id = ref(1) |
| 335 | + const { link } = await withLink({ |
| 336 | + to: () => ({ name: 'user', params: { id: id.value } }), |
| 337 | + }) |
| 338 | + expect(link.href.value).toBe('/users/1') |
| 339 | + id.value = 2 |
| 340 | + expect(link.href.value).toBe('/users/2') |
| 341 | + |
| 342 | + const to = ref('/about') |
| 343 | + const { link: other } = await withLink({ to }, '/about') |
| 344 | + expect(other.isExactActive.value).toBe(true) |
| 345 | + to.value = '/' |
| 346 | + expect(other.isActive.value).toBe(false) |
| 347 | + }) |
| 348 | + |
| 349 | + it('navigates with navigate()', async () => { |
| 350 | + const { link, router } = await withLink({ to: '/about' }) |
| 351 | + await link.navigate() |
| 352 | + expect(router.currentRoute.value.path).toBe('/about') |
| 353 | + }) |
| 354 | +}) |
0 commit comments