|
| 1 | +/** |
| 2 | + * Type-level assertions for route narrowing with `experimental.typedPages` (#3962). |
| 3 | + * |
| 4 | + * Never executed, only checked by this fixture's `test:types` script — an unused |
| 5 | + * `@ts-expect-error` directive fails the check. The `not.toBeAny()` assertions guard |
| 6 | + * against the vue-router augmentation silently resolving to `any` when it references |
| 7 | + * types without importing them (`skipLibCheck` swallows the resolution errors). |
| 8 | + */ |
| 9 | +import { describe, expectTypeOf, it } from 'vitest' |
| 10 | +import { useLocalePath, useLocaleRoute } from '#imports' |
| 11 | +import { useRoute, useRouter } from 'vue-router' |
| 12 | +import type { RouteLocationNamedI18n, RouteMapI18n } from 'vue-router' |
| 13 | + |
| 14 | +describe('vue-router augmentation', () => { |
| 15 | + it('resolves generated route maps', () => { |
| 16 | + expectTypeOf<RouteMapI18n>().not.toBeAny() |
| 17 | + expectTypeOf<keyof RouteMapI18n>().toEqualTypeOf<'index' | 'about' | 'slug'>() |
| 18 | + expectTypeOf<RouteLocationNamedI18n>().not.toBeAny() |
| 19 | + }) |
| 20 | + |
| 21 | + it('holds only localized names in vue-router route map', () => { |
| 22 | + const router = useRouter() |
| 23 | + router.push({ name: 'slug___en', params: { slug: 'ok' } }) |
| 24 | + // @ts-expect-error base name must not leak into vue-router's RouteMap |
| 25 | + router.push({ name: 'slug' }) |
| 26 | + // @ts-expect-error invalid route name |
| 27 | + router.push({ name: 'nope' }) |
| 28 | + }) |
| 29 | +}) |
| 30 | + |
| 31 | +describe('useLocalePath', () => { |
| 32 | + const localePath = useLocalePath() |
| 33 | + |
| 34 | + it('narrows route name strings', () => { |
| 35 | + localePath('about') |
| 36 | + localePath('about', 'nl') |
| 37 | + // @ts-expect-error invalid route name |
| 38 | + localePath('nope') |
| 39 | + }) |
| 40 | + |
| 41 | + it('narrows name and params of route objects', () => { |
| 42 | + localePath({ name: 'about' }) |
| 43 | + localePath({ name: 'slug', params: { slug: 'ok' } }) |
| 44 | + localePath({ name: 'about', query: { q: '1' }, hash: '#x' }) |
| 45 | + // @ts-expect-error invalid route name |
| 46 | + localePath({ name: 'nope' }) |
| 47 | + // @ts-expect-error wrong param type |
| 48 | + localePath({ name: 'slug', params: { slug: false } }) |
| 49 | + }) |
| 50 | + |
| 51 | + it('accepts plain string path objects and resolved routes', () => { |
| 52 | + localePath({ path: '/anything', query: { q: '1' } }) |
| 53 | + localePath(useRoute()) |
| 54 | + }) |
| 55 | +}) |
| 56 | + |
| 57 | +describe('useLocaleRoute', () => { |
| 58 | + const localeRoute = useLocaleRoute() |
| 59 | + |
| 60 | + it('narrows name and params of route objects', () => { |
| 61 | + // @ts-expect-error invalid route name |
| 62 | + localeRoute({ name: 'nope' }) |
| 63 | + // @ts-expect-error wrong param type |
| 64 | + localeRoute({ name: 'slug', params: { slug: false } }) |
| 65 | + }) |
| 66 | + |
| 67 | + it('narrows the resolved route to the given name', () => { |
| 68 | + const resolved = localeRoute({ name: 'slug', params: { slug: 'ok' } }) |
| 69 | + expectTypeOf(resolved).not.toBeAny() |
| 70 | + expectTypeOf(resolved!.name).toEqualTypeOf<'slug'>() |
| 71 | + expectTypeOf(resolved!.params).toEqualTypeOf<{ slug: string }>() |
| 72 | + }) |
| 73 | +}) |
| 74 | + |
| 75 | +describe('`NuxtLinkLocale` `to` prop', () => { |
| 76 | + it('narrows name and params of route objects', () => { |
| 77 | + expectTypeOf<{ name: 'slug'; params: { slug: 'ok' } }>().toExtend<RouteLocationNamedI18n>() |
| 78 | + expectTypeOf<{ path: '/anything' }>().toExtend<RouteLocationNamedI18n>() |
| 79 | + expectTypeOf<'about'>().toExtend<RouteLocationNamedI18n>() |
| 80 | + expectTypeOf<{ name: 'nope' }>().not.toExtend<RouteLocationNamedI18n>() |
| 81 | + expectTypeOf<{ name: 'slug'; params: { slug: boolean } }>().not.toExtend<RouteLocationNamedI18n>() |
| 82 | + expectTypeOf<'nope'>().not.toExtend<RouteLocationNamedI18n>() |
| 83 | + }) |
| 84 | +}) |
0 commit comments