forked from vuejs/vue-router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-map.spec.js
199 lines (175 loc) · 5.71 KB
/
create-map.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*eslint-disable no-undef*/
import { createRouteMap } from '../../../src/create-route-map'
const Home = { template: '<div>This is Home</div>' }
const Foo = { template: '<div>This is Foo</div>' }
const FooBar = { template: '<div>This is FooBar</div>' }
const Foobar = { template: '<div>This is foobar</div>' }
const Bar = { template: '<div>This is Bar <router-view></router-view></div>' }
const Baz = { template: '<div>This is Baz</div>' }
const routes = [
{ path: '/', name: 'home', component: Home },
{ path: '/foo', name: 'foo', component: Foo },
{ path: '*', name: 'wildcard', component: Baz },
{
path: '/bar',
name: 'bar',
component: Bar,
children: [
{
path: '',
component: Baz,
name: 'bar.baz'
}
]
},
{
path: '/bar-redirect',
name: 'bar-redirect',
redirect: { name: 'bar-redirect.baz' },
component: Bar,
children: [
{
path: '',
component: Baz,
name: 'bar-redirect.baz'
}
]
}
]
describe('Creating Route Map', function () {
let maps
beforeAll(function () {
spyOn(console, 'warn')
maps = createRouteMap(routes)
})
beforeEach(function () {
console.warn.calls.reset()
process.env.NODE_ENV = 'production'
})
it('has a pathMap object for default subroute at /bar/', function () {
expect(maps.pathMap['/bar/']).not.toBeUndefined()
})
it('has a pathList which places wildcards at the end', () => {
expect(maps.pathList).toEqual(['', '/foo', '/bar/', '/bar', '/bar-redirect/', '/bar-redirect', '*'])
})
it('has a nameMap object for default subroute at \'bar.baz\'', function () {
expect(maps.nameMap['bar.baz']).not.toBeUndefined()
})
it('in development, has logged a warning concerning named route of parent and default subroute', function () {
process.env.NODE_ENV = 'development'
maps = createRouteMap(routes)
expect(console.warn).toHaveBeenCalledTimes(1)
expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Named Route \'bar\'')
})
it('in development, throws if path is missing', function () {
process.env.NODE_ENV = 'development'
expect(() => {
maps = createRouteMap([{ component: Bar }])
}).toThrowError(/"path" is required/)
})
it('in production, it has not logged this warning', function () {
maps = createRouteMap(routes)
expect(console.warn).not.toHaveBeenCalled()
})
it('in development, warn duplicate param keys', () => {
process.env.NODE_ENV = 'development'
maps = createRouteMap([
{
path: '/foo/:id', component: Foo,
children: [
{ path: 'bar/:id', component: Bar }
]
}
])
expect(console.warn).toHaveBeenCalled()
expect(console.warn.calls.argsFor(0)[0]).toMatch('vue-router] Duplicate param keys in route with path: "/foo/:id/bar/:id"')
})
it('in development, warn if a path is missing a leading slash', function () {
process.env.NODE_ENV = 'development'
maps = createRouteMap([
{ path: 'bar', name: 'bar', component: Bar }
])
expect(console.warn).toHaveBeenCalledTimes(1)
expect(console.warn.calls.argsFor(0)[0]).toEqual('[vue-router] Non-nested routes must include a leading slash character. Fix the following routes: \n- bar')
})
it('in development, it does not log the missing leading slash when routes are valid', function () {
process.env.NODE_ENV = 'development'
maps = createRouteMap([
{ path: '/bar', name: 'bar', component: Bar }
])
expect(console.warn).not.toHaveBeenCalled()
})
it('in production, it does not log the missing leading slash warning', function () {
process.env.NODE_ENV = 'production'
maps = createRouteMap([
{ path: 'bar', name: 'bar', component: Bar }
])
expect(console.warn).not.toHaveBeenCalled()
})
describe('path-to-regexp options', function () {
const routes = [
{ path: '/foo', name: 'foo', component: Foo },
{ path: '/bar', name: 'bar', component: Bar, caseSensitive: false },
{ path: '/FooBar', name: 'FooBar', component: FooBar, caseSensitive: true },
{ path: '/foobar', name: 'foobar', component: Foobar, caseSensitive: true }
]
it('caseSensitive option in route', function () {
const { nameMap } = createRouteMap(routes)
expect(nameMap.FooBar.regex.ignoreCase).toBe(false)
expect(nameMap.bar.regex.ignoreCase).toBe(true)
expect(nameMap.foo.regex.ignoreCase).toBe(true)
})
it('pathToRegexpOptions option in route', function () {
const { nameMap } = createRouteMap([
{
name: 'foo',
path: '/foo',
component: Foo,
pathToRegexpOptions: {
sensitive: true
}
},
{
name: 'bar',
path: '/bar',
component: Bar,
pathToRegexpOptions: {
sensitive: false
}
}
])
expect(nameMap.foo.regex.ignoreCase).toBe(false)
expect(nameMap.bar.regex.ignoreCase).toBe(true)
})
it('caseSensitive over pathToRegexpOptions in route', function () {
const { nameMap } = createRouteMap([
{
name: 'foo',
path: '/foo',
component: Foo,
caseSensitive: true,
pathToRegexpOptions: {
sensitive: false
}
}
])
expect(nameMap.foo.regex.ignoreCase).toBe(false)
})
it('keeps trailing slashes with strict mode', function () {
const { pathList } = createRouteMap([
{
path: '/foo/',
component: Foo,
pathToRegexpOptions: {
strict: true
}
},
{
path: '/bar/',
component: Foo
}
])
expect(pathList).toEqual(['/foo/', '/bar'])
})
})
})