-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
340 additions
and
123 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { describe, it } from 'vitest' | ||
import { createRouter, createWebHistory } from '../src' | ||
import { defineComponent, h } from 'vue' | ||
|
||
describe('createRouter', () => { | ||
const component = defineComponent({}) | ||
|
||
const WithProps = defineComponent({ | ||
props: { | ||
id: { | ||
type: String, | ||
required: true, | ||
}, | ||
}, | ||
}) | ||
|
||
const Foo = defineComponent({ | ||
props: { | ||
test: String, | ||
}, | ||
setup() { | ||
return { | ||
title: 'homepage', | ||
} | ||
}, | ||
render() { | ||
return h('div', `${this.title}: ${this.test}`) | ||
}, | ||
}) | ||
|
||
it('works', () => { | ||
createRouter({ | ||
history: createWebHistory(), | ||
routes: [ | ||
{ path: '/', component }, | ||
{ path: '/foo', component: Foo }, | ||
{ path: '/', component: WithProps }, | ||
], | ||
parseQuery: search => ({}), | ||
stringifyQuery: query => '', | ||
strict: true, | ||
end: true, | ||
sensitive: true, | ||
scrollBehavior(to, from, savedPosition) {}, | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { describe, it, expectTypeOf } from 'vitest' | ||
import type { | ||
RouteRecordName, | ||
ParamValue, | ||
ParamValueZeroOrMore, | ||
RouteRecordInfo, | ||
RouteLocationNormalizedTypedList, | ||
} from '../src' | ||
|
||
// TODO: could we move this to an .d.ts file that is only loaded for tests? | ||
// https://github.com/microsoft/TypeScript/issues/15300 | ||
type RouteNamedMap = { | ||
home: RouteRecordInfo<'/', '/', Record<never, never>, Record<never, never>> | ||
'/[other]': RouteRecordInfo< | ||
'/[other]', | ||
'/:other', | ||
{ other: ParamValue<true> }, | ||
{ other: ParamValue<false> } | ||
> | ||
'/[name]': RouteRecordInfo< | ||
'/[name]', | ||
'/:name', | ||
{ name: ParamValue<true> }, | ||
{ name: ParamValue<false> } | ||
> | ||
'/[...path]': RouteRecordInfo< | ||
'/[...path]', | ||
'/:path(.*)', | ||
{ path: ParamValue<true> }, | ||
{ path: ParamValue<false> } | ||
> | ||
'/deep/nesting/works/[[files]]+': RouteRecordInfo< | ||
'/deep/nesting/works/[[files]]+', | ||
'/deep/nesting/works/:files*', | ||
{ files?: ParamValueZeroOrMore<true> }, | ||
{ files?: ParamValueZeroOrMore<false> } | ||
> | ||
} | ||
|
||
describe('Route Location types', () => { | ||
it('RouteLocationNormalized', () => { | ||
function withRoute( | ||
fn: ( | ||
to: RouteLocationNormalizedTypedList<RouteNamedMap>[keyof RouteNamedMap] | ||
) => void | ||
): void | ||
function withRoute<Name extends keyof RouteNamedMap>( | ||
name: Name, | ||
fn: (to: RouteLocationNormalizedTypedList<RouteNamedMap>[Name]) => void | ||
): void | ||
function withRoute<Name extends RouteRecordName>(...args: unknown[]) {} | ||
|
||
withRoute('/[name]', to => { | ||
expectTypeOf(to.params).toEqualTypeOf<{ name: string }>() | ||
expectTypeOf(to.params).not.toEqualTypeOf<{ notExisting: string }>() | ||
expectTypeOf(to.params).not.toEqualTypeOf<{ other: string }>() | ||
}) | ||
|
||
withRoute('/[name]' as keyof RouteNamedMap, to => { | ||
// @ts-expect-error: no all params have this | ||
to.params.name | ||
if (to.name === '/[name]') { | ||
to.params.name | ||
// @ts-expect-error: no param other | ||
to.params.other | ||
} | ||
}) | ||
|
||
withRoute(to => { | ||
// @ts-expect-error: not all params object have a name | ||
to.params.name | ||
// @ts-expect-error: no route named like that | ||
if (to.name === '') { | ||
} | ||
if (to.name === '/[name]') { | ||
expectTypeOf(to.params).toEqualTypeOf<{ name: string }>() | ||
// @ts-expect-error: no param other | ||
to.params.other | ||
} | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,2 @@ | ||
export * from '../dist/vue-router' | ||
// export * from '../src' | ||
|
||
export function describe(_name: string, _fn: () => void): void | ||
export function expectType<T>(value: T): void | ||
export function expectError<T>(value: T): void | ||
export function expectAssignable<T, T2 extends T = T>(value: T2): void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Router, RouteLocationNormalizedLoaded, expectType } from './index' | ||
import { expectTypeOf } from 'vitest' | ||
import { Router, RouteLocationNormalizedLoaded } from './index' | ||
import { defineComponent } from 'vue' | ||
|
||
defineComponent({ | ||
methods: { | ||
doStuff() { | ||
expectType<Router>(this.$router) | ||
expectType<RouteLocationNormalizedLoaded>(this.$route) | ||
expectTypeOf<Router>(this.$router) | ||
expectTypeOf<RouteLocationNormalizedLoaded>(this.$route) | ||
}, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.