-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathcreate-matcher.spec.js
154 lines (132 loc) · 4.81 KB
/
create-matcher.spec.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*eslint-disable no-undef*/
import { createMatcher } from '../../../src/create-matcher'
const routes = [
{ path: '/', name: 'home', component: { name: 'home' }},
{ path: '/foo', name: 'foo', component: { name: 'foo' }},
{ path: '/baz/:testparam', name: 'baz', component: { name: 'baz' }},
{ path: '/error/*', name: 'error', component: { name: 'error' }},
{ path: '*', props: true, name: 'notFound', component: { name: 'notFound' }}
]
describe('Creating Matcher', function () {
let match
beforeAll(function () {
spyOn(console, 'warn')
match = createMatcher(routes).match
})
beforeEach(function () {
console.warn.calls.reset()
process.env.NODE_ENV = 'production'
})
it('can add nested routes', function () {
const component = { name: 'fake' }
const matcher = createMatcher([
{
path: '/p',
name: 'parent',
children: [
{ path: 'a', name: 'a', component },
{
path: 'c',
name: 'child',
component,
children: [{ path: 'n', name: 'nested', component }]
}
]
},
{
// easier to debug tests
path: '*', name: 'not-found', component
}
])
matcher.addRoute({ path: '/b', name: 'b', component })
matcher.addRoute('parent', { path: 'b', name: 'p-b', component })
matcher.addRoute('child', { path: 'b', name: 'p-c-b', component })
expect(matcher.match('/b').name).toBe('b')
expect(matcher.match('/p/b').name).toBe('p-b')
expect(matcher.match('/p/c/b').name).toBe('p-c-b')
expect(matcher.match('/p/c/n').name).toBe('nested')
})
it('can get all routes', function () {
const component = { name: 'fake' }
const matcher = createMatcher([])
expect(matcher.getRoutes()).toEqual([])
matcher.addRoute({ path: '/b', name: 'b', component })
expect(matcher.getRoutes().length).toBe(1)
matcher.addRoute({ path: '/c', name: 'c', alias: ['/a', '/d'], component })
expect(matcher.getRoutes().length).toBe(4)
matcher.addRoute('b', { path: 'd', component })
expect(matcher.getRoutes().length).toBe(5)
matcher.addRoute('c', { path: 'd', component })
expect(matcher.getRoutes().length).toBe(8)
})
it('in development, has logged a warning if a named route does not exist', function () {
process.env.NODE_ENV = 'development'
const { name, matched } = match({ name: 'bar' }, routes[0])
expect(matched.length).toBe(0)
expect(name).toBe('bar')
expect(console.warn).toHaveBeenCalled()
expect(console.warn.calls.argsFor(0)[0]).toMatch(
"Route with name 'bar' does not exist"
)
})
it('in production, it has not logged this warning', function () {
match({ name: 'foo' }, routes[0])
expect(console.warn).not.toHaveBeenCalled()
})
it('matches named route with params without warning', function () {
process.env.NODE_ENV = 'development'
const { name, path, params } = match({
name: 'baz',
params: { testparam: 'testvalue' }
})
expect(console.warn).not.toHaveBeenCalled()
expect(name).toEqual('baz')
expect(path).toEqual('/baz/testvalue')
expect(params).toEqual({ testparam: 'testvalue' })
})
it('matches asterisk routes with a default param name without warning', function () {
process.env.NODE_ENV = 'development'
const { params } = match(
{ name: 'notFound', params: { pathMatch: '/not-found' }},
routes[0]
)
expect(console.warn).not.toHaveBeenCalled()
expect(params).toEqual({ pathMatch: '/not-found' })
})
it('matches partial asterisk routes with a default param name without warning', function () {
process.env.NODE_ENV = 'development'
const { params, path } = match(
{ name: 'error', params: { pathMatch: 'some' }},
routes[0]
)
expect(console.warn).not.toHaveBeenCalled()
expect(params).toEqual({ pathMatch: 'some' })
expect(path).toEqual('/error/some')
})
it('matches named catch-all route with empty pathMath param without warning', function () {
process.env.NODE_ENV = 'development'
match(
{ name: 'notFound', params: { pathMatch: '' }},
routes[0]
)
expect(console.warn).not.toHaveBeenCalled()
})
it('matches asterisk routes with a default param name', function () {
const { params } = match({ path: '/not-found' }, routes[0])
expect(params).toEqual({ pathMatch: '/not-found' })
})
it('allows an empty pathMatch', function () {
process.env.NODE_ENV = 'development'
const pathForErrorRoute = match(
{ name: 'error', params: { pathMatch: '' }},
routes[0]
).path
const pathForNotFoundRoute = match(
{ name: 'notFound', params: { pathMatch: '' }},
routes[0]
).path
expect(console.warn).not.toHaveBeenCalled()
expect(pathForErrorRoute).toEqual('/error/')
expect(pathForNotFoundRoute).toEqual('/')
})
})