hash: to.hash + '-2',
}),
},
+ {
+ path: '/basic',
+ alias: '/basic-alias',
+ component: components.Foo,
+ },
+ {
+ path: '/aliases',
+ alias: ['/aliases1', '/aliases2'],
+ component: components.Nested,
+ children: [
+ {
+ path: 'one',
+ alias: ['o', 'o2'],
+ component: components.Foo,
+ children: [
+ { path: 'two', alias: ['t', 't2'], component: components.Bar },
+ ],
+ },
+ ],
+ },
]
async function newRouter({ history }: { history?: RouterHistory } = {}) {
)
})
+ // FIXME:
+ it.skip('navigates if the location does not exist', async () => {
+ const { router } = await newRouter()
+ const spy = jest.fn((to, from, next) => next())
+ router.beforeEach(spy)
+ await router.push('/idontexist')
+ spy.mockReset()
+ await router.push('/me-neither')
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ describe('alias', () => {
+ it('does not navigate to alias if already on original record', async () => {
+ const { router } = await newRouter()
+ const spy = jest.fn((to, from, next) => next())
+ router.beforeEach(spy)
+ await router.push('/basic')
+ spy.mockReset()
+ await router.push('/basic-alias')
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('does not navigate to alias with children if already on original record', async () => {
+ const { router } = await newRouter()
+ const spy = jest.fn((to, from, next) => next())
+ router.beforeEach(spy)
+ await router.push('/aliases')
+ spy.mockReset()
+ await router.push('/aliases1')
+ expect(spy).not.toHaveBeenCalled()
+ await router.push('/aliases2')
+ expect(spy).not.toHaveBeenCalled()
+ })
+
+ it('does not navigate to child alias if already on original record', async () => {
+ const { router } = await newRouter()
+ const spy = jest.fn((to, from, next) => next())
+ router.beforeEach(spy)
+ await router.push('/aliases/one')
+ spy.mockReset()
+ await router.push('/aliases1/one')
+ expect(spy).not.toHaveBeenCalled()
+ await router.push('/aliases2/one')
+ expect(spy).not.toHaveBeenCalled()
+ await router.push('/aliases2/o')
+ expect(spy).not.toHaveBeenCalled()
+ })
+ })
+
describe('navigation', () => {
async function checkNavigationCancelledOnPush(
target?: RouteLocation | false | ((vm: any) => void)
import {
extractComponentsGuards,
guardToPromiseFn,
- isSameLocationObject,
applyToParams,
isSameRouteRecord,
+ isSameLocationObject,
} from './utils'
import { useCallbacks } from './utils/callbacks'
import { encodeParam, decode } from './utils/encoding'
const force: boolean | undefined = to.force
// TODO: should we throw an error as the navigation was aborted
- if (!force && isSameLocation(from, toLocation)) return from
+ if (!force && isSameRouteLocation(from, toLocation)) return from
toLocation.redirectedFrom = redirectedFrom
return [leavingRecords, updatingRecords, enteringRecords]
}
-function isSameLocation(
- a: Immutable<RouteLocationNormalized>,
- b: Immutable<RouteLocationNormalized>
+// function isSameLocation(
+// a: Immutable<RouteLocationNormalized>,
+// b: Immutable<RouteLocationNormalized>
+// ): boolean {
+// return (
+// a.name === b.name &&
+// a.path === b.path &&
+// a.hash === b.hash &&
+// isSameLocationObject(a.query, b.query) &&
+// a.matched.length === b.matched.length &&
+// a.matched.every((record, i) => isSameRouteRecord(record, b.matched[i]))
+// )
+// }
+
+function isSameRouteLocation(
+ a: RouteLocationNormalized,
+ b: RouteLocationNormalized
): boolean {
+ let aLastIndex = a.matched.length - 1
+ let bLastIndex = b.matched.length - 1
+
return (
- a.name === b.name &&
- a.path === b.path &&
- a.hash === b.hash &&
- isSameLocationObject(a.query, b.query) &&
- a.matched.length === b.matched.length &&
- a.matched.every((record, i) => isSameRouteRecord(record, b.matched[i]))
+ aLastIndex > -1 &&
+ aLastIndex === bLastIndex &&
+ isSameRouteRecord(a.matched[aLastIndex], b.matched[bLastIndex]) &&
+ isSameLocationObject(a.params, b.params)
)
}