-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathroute.spec.js
190 lines (178 loc) · 5.62 KB
/
route.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
import { isSameRoute, isIncludedRoute, createPlainRoute } from '../../../src/util/route'
describe('Route utils', () => {
describe('isSameRoute', () => {
it('path', () => {
const a = {
path: '/a',
hash: '#hi',
query: { foo: 'bar', arr: [1, 2] }
}
const b = {
path: '/a/', // Allow trailing slash
hash: '#hi',
query: { arr: ['1', '2'], foo: 'bar' }
}
const c = {
path: '/a/', // Allow trailing slash
hash: '#hi',
query: { foo: 'bar', arr: [1, 2] }
}
expect(isSameRoute(a, b)).toBe(false)
expect(isSameRoute(a, c)).toBe(true)
})
it('name', () => {
const a = {
path: '/abc',
name: 'a',
hash: '#hi',
query: { foo: 'bar', arr: [1, 2] }
}
const b = {
name: 'a',
hash: '#hi',
query: { arr: ['1', '2'], foo: 'bar' }
}
const c = {
name: 'a',
hash: '#hi',
query: { foo: 'bar', arr: [1, 2] }
}
expect(isSameRoute(a, b)).toBe(false)
expect(isSameRoute(a, c)).toBe(true)
})
it('nested query', () => {
const a = {
path: '/abc',
query: { foo: { bar: 'bar' }, arr: [1, 2] }
}
const b = {
path: '/abc',
query: { arr: [1, 2], foo: { bar: 'bar' }}
}
const c = {
path: '/abc',
query: { arr: [1, 2], foo: { bar: 'not bar' }}
}
const d = {
path: '/abc',
query: { foo: { bar: 'bar' }, arr: [1, 2] }
}
expect(isSameRoute(a, b)).toBe(false)
expect(isSameRoute(a, d)).toBe(true)
expect(isSameRoute(a, c)).toBe(false)
})
it('queries with null values', () => {
const a = {
path: '/abc',
query: { foo: null }
}
const b = {
path: '/abc',
query: { foo: null }
}
const c = {
path: '/abc',
query: { foo: 5 }
}
expect(() => isSameRoute(a, b)).not.toThrow()
expect(() => isSameRoute(a, c)).not.toThrow()
expect(isSameRoute(a, b)).toBe(true)
expect(isSameRoute(a, c)).toBe(false)
})
it('queries with undefined values', () => {
const a = {
path: '/abc',
query: { a: 'x' }
}
const b = {
path: '/abc',
query: { id: undefined }
}
const c = {
path: '/abc',
query: {}
}
expect(() => isSameRoute(a, b)).not.toThrow()
expect(() => isSameRoute(a, c)).not.toThrow()
expect(() => isSameRoute(b, c)).not.toThrow()
expect(isSameRoute(a, b)).toBe(false)
expect(isSameRoute(a, c)).toBe(false)
// NOTE: in reality this should be true but because we check queries as
// objects, they are different objects. We should check queries as their
// string representation instead
expect(isSameRoute(b, c)).toBe(false)
expect(isSameRoute(c, b)).toBe(false)
})
})
describe('isIncludedRoute', () => {
it('path', () => {
const a = { path: '/a/b' }
const b = { path: '/a' }
const c = { path: '/a/b/c' }
const d = { path: '/a/b/' }
expect(isIncludedRoute(a, b)).toBe(true)
expect(isIncludedRoute(a, c)).toBe(false)
expect(isIncludedRoute(a, d)).toBe(true)
})
it('with hash', () => {
const a = { path: '/a/b', hash: '#a' }
const b = { path: '/a' }
const c = { path: '/a', hash: '#a' }
const d = { path: '/a', hash: '#b' }
expect(isIncludedRoute(a, b)).toBe(true)
expect(isIncludedRoute(a, c)).toBe(true)
expect(isIncludedRoute(a, d)).toBe(false)
})
it('with query', () => {
const a = { path: '/a/b', query: { foo: 'bar', baz: 'qux' }}
const b = { path: '/a', query: {}}
const c = { path: '/a', query: { foo: 'bar' }}
const d = { path: '/a', query: { foo: 'bar', a: 'b' }}
expect(isIncludedRoute(a, b)).toBe(true)
expect(isIncludedRoute(a, c)).toBe(true)
expect(isIncludedRoute(a, d)).toBe(false)
})
it('with both', () => {
const a = { path: '/a/b', query: { foo: 'bar', baz: 'qux' }, hash: '#a' }
const b = { path: '/a', query: {}}
const c = { path: '/a', query: { foo: 'bar' }}
const d = { path: '/a', query: { foo: 'bar' }, hash: '#b' }
const e = { path: '/a', query: { a: 'b' }, hash: '#a' }
expect(isIncludedRoute(a, b)).toBe(true)
expect(isIncludedRoute(a, c)).toBe(true)
expect(isIncludedRoute(a, d)).toBe(false)
expect(isIncludedRoute(a, e)).toBe(false)
})
it('trailing slash', () => {
const a = { path: '/users' }
const b = { path: '/user' }
const c = { path: '/users/' }
expect(isIncludedRoute(a, b)).toBe(false)
expect(isIncludedRoute(a, c)).toBe(true)
const d = { path: '/users/hello/world' }
const e = { path: '/users/hello' }
const f = { path: '/users/hello-world' }
expect(isIncludedRoute(d, e)).toBe(true)
expect(isIncludedRoute(d, f)).toBe(false)
})
})
describe('createPlainRoute', () => {
it('path', () => {
const a = { path: '/a?arr=1&foo=bar&arr=2#hi' }
const b = { path: '/a?arr=1&arr=2&foo=bar#hi' }
const c = { path: '/a?foo=bar&arr=1&arr=2#hi' }
const route = {
path: '/a',
hash: '#hi',
query: { foo: 'bar', arr: [1, 2] }
}
const aRoute = createPlainRoute(a)
const bRoute = createPlainRoute(b)
const cRoute = createPlainRoute(c)
expect(isSameRoute(aRoute, route)).toBe(false)
expect(isSameRoute(bRoute, route)).toBe(false)
expect(isSameRoute(bRoute, route)).toBe(false)
expect(isSameRoute(cRoute, route)).toBe(true)
})
})
})