]> git.ipfire.org Git - thirdparty/vuejs/router.git/commitdiff
test(matcher): normalizeRecord
authorEduardo San Martin Morote <posva13@gmail.com>
Thu, 6 Feb 2020 11:16:12 +0000 (12:16 +0100)
committerEduardo San Martin Morote <posva13@gmail.com>
Thu, 6 Feb 2020 11:16:12 +0000 (12:16 +0100)
__tests__/matcher/records.spec.ts [new file with mode: 0644]
src/matcher/index.ts

diff --git a/__tests__/matcher/records.spec.ts b/__tests__/matcher/records.spec.ts
new file mode 100644 (file)
index 0000000..c3d6ef7
--- /dev/null
@@ -0,0 +1,139 @@
+import { normalizeRouteRecord } from '../../src/matcher'
+
+describe('normalizeRouteRecord', () => {
+  it('transforms a single view into multiple views', () => {
+    const record = normalizeRouteRecord({
+      path: '/home',
+      component: {},
+    })
+    expect(record).toEqual({
+      beforeEnter: undefined,
+      children: undefined,
+      components: { default: {} },
+      leaveGuards: [],
+      meta: undefined,
+      name: undefined,
+      path: '/home',
+    })
+  })
+
+  it('keeps original values in single view', () => {
+    const beforeEnter = jest.fn()
+    const record = normalizeRouteRecord({
+      path: '/home',
+      beforeEnter,
+      children: [{ path: '/child' } as any],
+      meta: { foo: true },
+      name: 'name',
+      component: {},
+    })
+    expect(record).toEqual({
+      beforeEnter,
+      children: [{ path: '/child' }],
+      components: { default: {} },
+      leaveGuards: [],
+      meta: { foo: true },
+      name: 'name',
+      path: '/home',
+    })
+  })
+
+  it('keeps original values in redirect', () => {
+    const record = normalizeRouteRecord({
+      path: '/redirect',
+      redirect: '/home',
+      meta: { foo: true },
+      name: 'name',
+    })
+
+    expect(record).toEqual({
+      beforeEnter: expect.any(Function),
+      children: undefined,
+      components: {},
+      leaveGuards: [],
+      meta: { foo: true },
+      name: 'name',
+      path: '/redirect',
+    })
+  })
+
+  it('keeps original values in multiple views', () => {
+    const beforeEnter = jest.fn()
+    const record = normalizeRouteRecord({
+      path: '/home',
+      beforeEnter,
+      children: [{ path: '/child' } as any],
+      meta: { foo: true },
+      name: 'name',
+      components: { one: {} },
+    })
+    expect(record).toEqual({
+      beforeEnter,
+      children: [{ path: '/child' }],
+      components: { one: {} },
+      leaveGuards: [],
+      meta: { foo: true },
+      name: 'name',
+      path: '/home',
+    })
+  })
+
+  it('transforms a redirect record into a beforeEnter guard', () => {
+    const record = normalizeRouteRecord({
+      path: '/redirect',
+      redirect: '/home',
+    })
+    expect(record).toEqual({
+      beforeEnter: expect.any(Function),
+      children: undefined,
+      components: {},
+      leaveGuards: [],
+      meta: undefined,
+      name: undefined,
+      path: '/redirect',
+    })
+  })
+
+  it('beforeEnter is called with the string redirect', () => {
+    const record = normalizeRouteRecord({
+      path: '/redirect',
+      redirect: '/home',
+    })
+
+    let spy = jest.fn()
+    ;(record.beforeEnter as Function)({} as any, {} as any, spy)
+    expect(spy).toHaveBeenCalledTimes(1)
+    expect(spy).toHaveBeenCalledWith('/home')
+  })
+
+  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')
+  })
+})
index 79e8893d0a412df1ad8d94d03356c1d0fa64b28c..134b599a616a2d4b15bc4f496c56e1e4638affbe 100644 (file)
@@ -171,11 +171,11 @@ export function createRouterMatcher(
 
     const matched: MatcherLocationNormalized['matched'] = []
     let parentMatcher: RouteRecordMatcher | void = matcher
-    do {
+    while (parentMatcher) {
       // reversed order so parents are at the beginning
       matched.unshift(parentMatcher.record)
       parentMatcher = parentMatcher.parent
-    } while (parentMatcher)
+    }
 
     return {
       name,