-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathroute.spec.js
156 lines (145 loc) · 4.44 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
import { isSameRoute, isIncludedRoute } 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' }
}
expect(isSameRoute(a, b)).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' }
}
expect(isSameRoute(a, b)).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' } }
}
expect(isSameRoute(a, b)).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('can ignore query', () => {
const a = {
path: '/abc',
query: {}
}
const b = {
path: '/abc',
query: { foo: 'bar' }
}
const c = {
path: '/abc',
query: { foo: 'baz' }
}
const d = {
path: '/xyz',
query: { foo: 'bar' }
}
expect(isSameRoute(a, b)).toBe(false)
expect(isSameRoute(a, c)).toBe(false)
expect(isSameRoute(a, d)).toBe(false)
expect(isSameRoute(a, b, true)).toBe(true)
expect(isSameRoute(a, c, true)).toBe(true)
expect(isSameRoute(a, d, true)).toBe(false)
expect(isSameRoute(b, c)).toBe(false)
expect(isSameRoute(b, c, true)).toBe(true)
expect(isSameRoute(b, d, true)).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)
})
})
})