From: Eduardo San Martin Morote Date: Fri, 13 Mar 2020 16:25:15 +0000 (+0100) Subject: test: add tests for aliases X-Git-Tag: v4.0.0-alpha.2~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=173ba480dc8b28b3cfbe899ed4856fffc4a7889e;p=thirdparty%2Fvuejs%2Frouter.git test: add tests for aliases --- diff --git a/__tests__/matcher/__snapshots__/resolve.spec.ts.snap b/__tests__/matcher/__snapshots__/resolve.spec.ts.snap index 1c563264..4592f7d0 100644 --- a/__tests__/matcher/__snapshots__/resolve.spec.ts.snap +++ b/__tests__/matcher/__snapshots__/resolve.spec.ts.snap @@ -5,14 +5,6 @@ exports[`Router Matcher resolve LocationAsName throws if the named route does no {"name":"Home"}] `; -exports[`Router Matcher resolve LocationAsRelative redirects throws if relative location when redirecting 1`] = ` -[InvalidRouteMatch: Cannot redirect using a relative location: -{ - "params": {} -} -Use the function redirect and explicitely provide a name] -`; - exports[`Router Matcher resolve LocationAsRelative throws if the current named route does not exists 1`] = ` [NoRouteMatchError: No match for {"params":{"a":"foo"}} diff --git a/__tests__/matcher/resolve.spec.ts b/__tests__/matcher/resolve.spec.ts index 51fb50a5..5b868b68 100644 --- a/__tests__/matcher/resolve.spec.ts +++ b/__tests__/matcher/resolve.spec.ts @@ -5,7 +5,6 @@ import { RouteRecord, MatcherLocation, MatcherLocationNormalized, - MatcherLocationRedirect, } from '../../src/types' import { MatcherLocationNormalizedLoose } from '../utils' @@ -125,8 +124,73 @@ describe('Router Matcher', () => { ) }) - it.todo('multiple aliases') - it.todo('resolve named child with parent with alias') + it('multiple aliases', () => { + const record = { + path: '/', + alias: ['/home', '/start'], + name: 'Home', + components, + meta: { foo: true }, + } + + assertRecordMatch( + record, + { path: '/' }, + { + name: 'Home', + path: '/', + params: {}, + meta: { foo: true }, + matched: [ + { + path: '/', + name: 'Home', + components, + aliasOf: undefined, + meta: { foo: true }, + }, + ], + } + ) + assertRecordMatch( + record, + { path: '/home' }, + { + name: 'Home', + path: '/home', + params: {}, + meta: { foo: true }, + matched: [ + { + path: '/home', + name: 'Home', + components, + aliasOf: expect.objectContaining({ name: 'Home', path: '/' }), + meta: { foo: true }, + }, + ], + } + ) + assertRecordMatch( + record, + { path: '/start' }, + { + name: 'Home', + path: '/start', + params: {}, + meta: { foo: true }, + matched: [ + { + path: '/start', + name: 'Home', + components, + aliasOf: expect.objectContaining({ name: 'Home', path: '/' }), + meta: { foo: true }, + }, + ], + } + ) + }) it('resolves the original record by name', () => { assertRecordMatch( @@ -757,189 +821,6 @@ describe('Router Matcher', () => { ) }) - // TODO: replace tests with a transformation check to the `beforeEnter` guard - describe.skip('redirects', () => { - function assertRedirect( - records: RouteRecord[], - location: MatcherLocation, - expected: MatcherLocationNormalized | MatcherLocationRedirect, - currentLocation: MatcherLocationNormalized = START_LOCATION_NORMALIZED - ) { - const matcher = createRouterMatcher(records, {}) - const resolved = matcher.resolve(location, currentLocation) - expect(resolved).toEqual(expected) - return resolved - } - - it('resolves a redirect string', () => { - const records = [ - { path: '/home', components }, - { path: '/redirect', redirect: '/home' }, - ] - assertRedirect( - records, - { - name: undefined, - path: '/redirect', - }, - { - redirect: '/home', - normalizedLocation: { - path: '/redirect', - params: {}, - name: undefined, - matched: [], - meta: {}, - }, - } - ) - }) - - it('resolves a redirect function that returns a string', () => { - const redirect = () => '/home' - const records = [ - { path: '/home', components }, - { path: '/redirect', redirect }, - ] - assertRedirect( - records, - { - name: undefined, - path: '/redirect', - }, - { - redirect, - normalizedLocation: { - path: '/redirect', - params: {}, - name: undefined, - matched: [], - meta: {}, - }, - } - ) - }) - - it('resolves a redirect function that returns an object route', () => { - const redirect = () => { - path: '/home' - } - const records = [ - { path: '/home', components }, - { path: '/redirect', redirect }, - ] - assertRedirect( - records, - { - name: undefined, - path: '/redirect', - }, - { - redirect, - normalizedLocation: { - path: '/redirect', - params: {}, - name: undefined, - matched: [], - meta: {}, - }, - } - ) - }) - - it('resolves a redirect as an object', () => { - const records = [ - { path: '/home', components }, - { path: '/redirect', redirect: { path: 'home' } }, - ] - assertRedirect( - records, - { - name: undefined, - path: '/redirect', - }, - { - redirect: { path: 'home' }, - normalizedLocation: { - path: '/redirect', - params: {}, - name: undefined, - matched: [], - meta: {}, - }, - } - ) - }) - - it('works with a named location', () => { - const records = [ - { path: '/home', components }, - { path: '/redirect', name: 'redirect', redirect: { path: 'home' } }, - ] - assertRedirect( - records, - { - name: 'redirect', - }, - { - redirect: { path: 'home' }, - normalizedLocation: { - path: '/redirect', - params: {}, - name: 'redirect', - matched: [], - meta: {}, - }, - } - ) - }) - - it('throws if relative location when redirecting', () => { - expect( - assertErrorMatch( - { path: '/redirect', redirect: '/home' }, - { params: {} }, - { - path: '/redirect', - params: {}, - matched: [], - name: undefined, - meta: {}, - } - ) - ).toMatchSnapshot() - }) - - it('normalize a location when redirecting', () => { - const redirect = (to: any) => ({ name: 'b', params: to.params }) - const records = [ - { path: '/home', components }, - { - path: '/a/:a', - name: 'a', - redirect, - }, - { path: '/b/:a', name: 'b', components }, - ] - assertRedirect( - records, - { - path: '/a/foo', - }, - { - redirect, - normalizedLocation: { - path: '/a/foo', - params: { a: 'foo' }, - name: 'a', - matched: [], - meta: {}, - }, - } - ) - }) - }) - it('throws if the current named route does not exists', () => { const record = { path: '/', components } const start = {