Skip to content

Commit

Permalink
fix: skip initial guards with static redirect
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Renamed types by removing suffix Normalized and using Raw instead
  - `RouteLocation` -> `RouteLocationRaw`
  - `RouteLocationNormalized` -> `RouteLocation`
  - `RouteLocationNormalized` is now a location that can be displayed (not a static redirect)
  - `RouteLocationNormalizedResolved` -> `RouteLocationNormalizedLoaded`
  - `RouteRecord` -> `RouteRecordRaw`
  - `RouteRecordNormalized` -> `RouteRecord`
  - `RouteRecordNormalized` is now a record that is not a static redirect
  • Loading branch information
posva committed Mar 30, 2020
1 parent 861b967 commit c76bb93
Show file tree
Hide file tree
Showing 33 changed files with 375 additions and 349 deletions.
11 changes: 7 additions & 4 deletions __tests__/RouterLink.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { Link as RouterLink } from '../src/components/Link'
import {
START_LOCATION_NORMALIZED,
RouteQueryAndHash,
MatcherLocation,
MatcherLocationRaw,
RouteLocationNormalized,
RouteLocation,
} from '../src/types'
import { createMemoryHistory } from '../src'
import { mount, tick } from './mount'
Expand All @@ -25,15 +26,17 @@ const records = {

// fix the aliasOf
records.homeAlias = { aliasOf: records.home } as RouteRecordNormalized
records.parentAlias = { aliasOf: records.parent } as RouteRecordNormalized
records.parentAlias = {
aliasOf: records.parent,
} as RouteRecordNormalized
records.childAlias = { aliasOf: records.child } as RouteRecordNormalized

const locations: Record<
string,
{
string: string
normalized: RouteLocationNormalized
toResolve?: MatcherLocation & Required<RouteQueryAndHash>
toResolve?: MatcherLocationRaw & Required<RouteQueryAndHash>
}
> = {
basic: {
Expand Down Expand Up @@ -216,7 +219,7 @@ describe('RouterLink', () => {
function factory(
currentLocation: RouteLocationNormalized,
propsData: any,
resolvedLocation: RouteLocationNormalized,
resolvedLocation: RouteLocation,
template: string = `<RouterLink :to="to">a link</RouterLink>`
) {
const router = {
Expand Down
4 changes: 2 additions & 2 deletions __tests__/errors.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { createRouter as newRouter, createMemoryHistory } from '../src'
import { ErrorTypes } from '../src/errors'
import { components, tick } from './utils'
import { RouteRecord } from '../src/types'
import { RouteRecordRaw } from '../src/types'

const routes: RouteRecord[] = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: components.Home },
{ path: '/foo', component: components.Foo, name: 'Foo' },
{ path: '/to-foo', redirect: '/foo' },
Expand Down
13 changes: 7 additions & 6 deletions __tests__/extractComponentsGuards.spec.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { extractComponentsGuards } from '../src/utils'
import { START_LOCATION_NORMALIZED, RouteRecord } from '../src/types'
import { START_LOCATION_NORMALIZED, RouteRecordRaw } from '../src/types'
import { components } from './utils'
import { normalizeRouteRecord } from '../src/matcher'
import { RouteRecordNormalized } from 'src/matcher/types'

const beforeRouteEnter = jest.fn()

// stub those two
const to = START_LOCATION_NORMALIZED
const from = START_LOCATION_NORMALIZED

const NoGuard: RouteRecord = { path: '/', component: components.Home }
const SingleGuard: RouteRecord = {
const NoGuard: RouteRecordRaw = { path: '/', component: components.Home }
const SingleGuard: RouteRecordRaw = {
path: '/',
component: { ...components.Home, beforeRouteEnter },
}
const SingleGuardNamed: RouteRecord = {
const SingleGuardNamed: RouteRecordRaw = {
path: '/',
components: {
default: { ...components.Home, beforeRouteEnter },
Expand All @@ -30,14 +31,14 @@ beforeEach(() => {
})

async function checkGuards(
components: Exclude<RouteRecord, { redirect: any }>[],
components: Exclude<RouteRecordRaw, { redirect: any }>[],
n: number,
guardsLength: number = n
) {
beforeRouteEnter.mockClear()
const guards = await extractComponentsGuards(
// type is fine as we excluded RouteRecordRedirect in components argument
components.map(normalizeRouteRecord),
components.map(normalizeRouteRecord) as RouteRecordNormalized[],
'beforeRouteEnter',
to,
from
Expand Down
6 changes: 3 additions & 3 deletions __tests__/guards/component-beforeRouteEnter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RouterOptions, createRouter as newRouter } from '../../src/router'
import fakePromise from 'faked-promise'
import { createDom, noGuard } from '../utils'
import { RouteRecord, NavigationGuard } from '../../src/types'
import { RouteRecordRaw, NavigationGuard } from '../../src/types'
import { createWebHistory } from '../../src'

function createRouter(
options: Partial<RouterOptions> & { routes: RouteRecord[] }
options: Partial<RouterOptions> & { routes: RouteRecordRaw[] }
) {
return newRouter({
history: createWebHistory(),
Expand Down Expand Up @@ -35,7 +35,7 @@ const nested = {
nestedNestedParam: jest.fn(),
}

const routes: RouteRecord[] = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{
Expand Down
6 changes: 3 additions & 3 deletions __tests__/guards/component-beforeRouteLeave.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RouterOptions, createRouter as newRouter } from '../../src/router'
import { createDom, noGuard } from '../utils'
import { RouteRecord } from '../../src/types'
import { RouteRecordRaw } from '../../src/types'
import { createWebHistory } from '../../src'

// TODO: refactor in utils
function createRouter(
options: Partial<RouterOptions> & { routes: RouteRecord[] }
options: Partial<RouterOptions> & { routes: RouteRecordRaw[] }
) {
return newRouter({
history: createWebHistory(),
Expand All @@ -28,7 +28,7 @@ const nested = {
}
const beforeRouteLeave = jest.fn()

const routes: RouteRecord[] = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{
Expand Down
6 changes: 3 additions & 3 deletions __tests__/guards/component-beforeRouteUpdate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import fakePromise from 'faked-promise'
import { createDom, noGuard } from '../utils'
import { createRouter as newRouter, createWebHistory } from '../../src'
import { RouteRecord } from '../../src/types'
import { RouteRecordRaw } from '../../src/types'

function createRouter(
options: Partial<import('../../src/router').RouterOptions> & {
routes: import('../../src/types').RouteRecord[]
routes: import('../../src/types').RouteRecordRaw[]
}
) {
return newRouter({
Expand All @@ -18,7 +18,7 @@ const Home = { template: `<div>Home</div>` }
const Foo = { template: `<div>Foo</div>` }

const beforeRouteUpdate = jest.fn()
const routes: RouteRecord[] = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{
Expand Down
6 changes: 3 additions & 3 deletions __tests__/guards/global-after.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { createDom } from '../utils'
import { createWebHistory, createRouter as newRouter } from '../../src'
import { RouteRecordRaw } from 'src/types'

function createRouter(
options: Partial<import('../../src/router').RouterOptions> & {
routes: import('../../src/types').RouteRecord[]
routes: import('../../src/types').RouteRecordRaw[]
}
) {
return newRouter({
Expand All @@ -16,8 +17,7 @@ const Home = { template: `<div>Home</div>` }
const Foo = { template: `<div>Foo</div>` }
const Nested = { template: `<div>Nested<router-view/></div>` }

/** @type {import('../../src/types').RouteRecord[]} */
const routes = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{
Expand Down
8 changes: 4 additions & 4 deletions __tests__/guards/global-beforeEach.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RouterOptions } from '../../src/router'
import fakePromise from 'faked-promise'
import { createDom, tick, noGuard } from '../utils'
import { RouteRecord, RouteLocation } from '../../src/types'
import { RouteRecordRaw, RouteLocationRaw } from '../../src/types'
import { createWebHistory, createRouter as newRouter } from '../../src'

function createRouter(
options: Partial<RouterOptions> & { routes: RouteRecord[] }
options: Partial<RouterOptions> & { routes: RouteRecordRaw[] }
) {
return newRouter({
history: createWebHistory(),
Expand All @@ -17,7 +17,7 @@ const Home = { template: `<div>Home</div>` }
const Foo = { template: `<div>Foo</div>` }
const Nested = { template: `<div>Nested<router-view/></div>` }

const routes: RouteRecord[] = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{ path: '/other', component: Foo },
Expand Down Expand Up @@ -117,7 +117,7 @@ describe('router.beforeEach', () => {
expect(router.currentRoute.value.fullPath).toBe('/other')
})

async function assertRedirect(redirectFn: (i: string) => RouteLocation) {
async function assertRedirect(redirectFn: (i: string) => RouteLocationRaw) {
const spy = jest.fn()
const router = createRouter({ routes })
await router.push('/')
Expand Down
6 changes: 3 additions & 3 deletions __tests__/guards/route-beforeEnter.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RouterOptions, createRouter as newRouter } from '../../src/router'
import fakePromise from 'faked-promise'
import { createDom, noGuard, tick } from '../utils'
import { RouteRecord } from '../../src/types'
import { RouteRecordRaw } from '../../src/types'
import { createWebHistory } from '../../src'

function createRouter(
options: Partial<RouterOptions> & { routes: RouteRecord[] }
options: Partial<RouterOptions> & { routes: RouteRecordRaw[] }
) {
return newRouter({
history: createWebHistory(),
Expand All @@ -28,7 +28,7 @@ const nested = {
nestedNestedParam: jest.fn(),
}

const routes: RouteRecord[] = [
const routes: RouteRecordRaw[] = [
{ path: '/', component: Home },
{ path: '/home', component: Home, beforeEnter },
{ path: '/foo', component: Foo },
Expand Down
4 changes: 2 additions & 2 deletions __tests__/matcher/addingRemoving.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { createRouterMatcher } from '../../src/matcher'
import { MatcherLocationNormalized } from '../../src/types'
import { MatcherLocation } from '../../src/types'

const currentLocation = { path: '/' } as MatcherLocationNormalized
const currentLocation = { path: '/' } as MatcherLocation
// @ts-ignore
const component: RouteComponent = null

Expand Down
69 changes: 5 additions & 64 deletions __tests__/matcher/records.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,12 @@ describe('normalizeRouteRecord', () => {
})

expect(record).toEqual({
beforeEnter: expect.any(Function),
children: [],
aliasOf: undefined,
components: {},
leaveGuards: [],
instances: {},
meta: { foo: true },
name: 'name',
path: '/redirect',
props: false,
redirect: '/home',
})
})

Expand All @@ -88,65 +84,10 @@ describe('normalizeRouteRecord', () => {
})
})

it('transforms a redirect record into a beforeEnter guard', () => {
const record = normalizeRouteRecord({
path: '/redirect',
redirect: '/home',
})
expect(record).toEqual({
beforeEnter: expect.any(Function),
children: [],
aliasOf: undefined,
components: {},
leaveGuards: [],
instances: {},
meta: {},
name: undefined,
path: '/redirect',
props: false,
})
})

it('beforeEnter is called with the string redirect', () => {
const record = normalizeRouteRecord({
path: '/redirect',
redirect: '/home',
})
// TODO: move to router
it.todo('beforeEnter is called with the string redirect')

let spy = jest.fn()
;(record.beforeEnter as Function)({} as any, {} as any, spy)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('/home')
})
it.todo('beforeEnter is called with object redirect')

it('beforeEnter is called with object redirect', () => {
const record = normalizeRouteRecord({
path: '/redirect',
redirect: { name: 'home' },
})

let spy = jest.fn()
;(record.beforeEnter as Function)({} as any, {} as any, spy)
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith({ name: 'home' })
})

it('function redirect is invoked by beforeEnter', () => {
const redirect = jest.fn(() => '/home')
const record = normalizeRouteRecord({
path: '/redirect',
redirect,
})

let spy = jest.fn()
;(record.beforeEnter as Function)(
{ path: '/redirect' } as any,
{} as any,
spy
)
expect(redirect).toHaveBeenCalledTimes(1)
expect(redirect).toHaveBeenCalledWith({ path: '/redirect' })
expect(spy).toHaveBeenCalledTimes(1)
expect(spy).toHaveBeenCalledWith('/home')
})
it.todo('function redirect is invoked by beforeEnter')
})

0 comments on commit c76bb93

Please sign in to comment.