-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathroute.spec.js
109 lines (100 loc) · 3.23 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
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)
})
})
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)
})
})
})