/
extractComponentsGuards.spec.ts
110 lines (97 loc) 路 3.08 KB
/
extractComponentsGuards.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { extractComponentsGuards } from '../../src/navigationGuards'
import { START_LOCATION_NORMALIZED, RouteRecordRaw } from '../../src/types'
import { components } from '../utils'
import { normalizeRouteRecord } from '../../src/matcher'
import { RouteRecordNormalized } from 'src/matcher/types'
import { mockWarn } from 'jest-mock-warn'
const beforeRouteEnter = jest.fn()
// stub those two
const to = START_LOCATION_NORMALIZED
const from = START_LOCATION_NORMALIZED
const NoGuard: RouteRecordRaw = { path: '/', component: components.Home }
const InvalidRoute: RouteRecordRaw = {
path: '/',
// @ts-ignore: intended error
component: null,
}
const WrongLazyRoute: RouteRecordRaw = {
path: '/',
// @ts-ignore: intended error
component: Promise.resolve(components.Home),
}
const SingleGuard: RouteRecordRaw = {
path: '/',
component: { ...components.Home, beforeRouteEnter },
}
const SingleGuardNamed: RouteRecordRaw = {
path: '/',
components: {
default: { ...components.Home, beforeRouteEnter },
other: { ...components.Foo, beforeRouteEnter },
},
}
const ErrorLazyLoad: RouteRecordRaw = {
path: '/',
component: () => Promise.reject(new Error('custom')),
}
beforeEach(() => {
beforeRouteEnter.mockReset()
beforeRouteEnter.mockImplementation((to, from, next) => {
next()
})
})
async function checkGuards(
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) as RouteRecordNormalized[],
'beforeRouteEnter',
to,
from
)
expect(guards).toHaveLength(guardsLength)
for (const guard of guards) {
expect(guard).toBeInstanceOf(Function)
expect(await guard())
}
expect(beforeRouteEnter).toHaveBeenCalledTimes(n)
}
describe('extractComponentsGuards', () => {
mockWarn()
it('extracts guards from one single component', async () => {
await checkGuards([SingleGuard], 1)
})
it('extracts guards from multiple components (named views)', async () => {
await checkGuards([SingleGuardNamed], 2)
})
it('handles no guards', async () => {
await checkGuards([NoGuard], 0)
})
it('handles mixed things', async () => {
await checkGuards([SingleGuard, SingleGuardNamed], 3)
await checkGuards([SingleGuard, SingleGuard], 2)
await checkGuards([SingleGuardNamed, SingleGuardNamed], 4)
})
it('throws if component is null', async () => {
// @ts-ignore
await expect(checkGuards([InvalidRoute], 2)).rejects.toHaveProperty(
'message',
expect.stringMatching('Invalid route component')
)
expect('is not a valid component').toHaveBeenWarned()
})
it('warns wrong lazy component', async () => {
await checkGuards([WrongLazyRoute], 0, 1)
expect('Promise instead of a function').toHaveBeenWarned()
})
it('rejects if lazy load fails', async () => {
await expect(checkGuards([ErrorLazyLoad], 0, 1)).rejects.toHaveProperty(
'message',
'custom'
)
})
})