-
-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathlocation.spec.js
133 lines (121 loc) · 3.96 KB
/
location.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
import { normalizeLocation } from '../../../src/util/location'
describe('Location utils', () => {
describe('normalizeLocation', () => {
it('string', () => {
const loc = normalizeLocation('/abc?foo=bar&baz=qux#hello')
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/abc')
expect(loc.hash).toBe('#hello')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
foo: 'bar',
baz: 'qux'
}))
})
it('empty string', function () {
const loc = normalizeLocation('', { path: '/abc' })
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/abc')
expect(loc.hash).toBe('')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({}))
})
it('undefined', function () {
const loc = normalizeLocation({}, { path: '/abc' })
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/abc')
expect(loc.hash).toBe('')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({}))
})
it('relative', () => {
const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', {
path: '/root/next'
})
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/root/abc')
expect(loc.hash).toBe('#hello')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
foo: 'bar',
baz: 'qux'
}))
})
it('relative append', () => {
const loc = normalizeLocation('abc?foo=bar&baz=qux#hello', {
path: '/root/next'
}, true)
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/root/next/abc')
expect(loc.hash).toBe('#hello')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
foo: 'bar',
baz: 'qux'
}))
})
it('relative query & hash', () => {
const loc = normalizeLocation('?foo=bar&baz=qux#hello', {
path: '/root/next'
})
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/root/next')
expect(loc.hash).toBe('#hello')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
foo: 'bar',
baz: 'qux'
}))
})
it('relative params (named)', () => {
const loc = normalizeLocation({ params: { lang: 'fr' }}, {
name: 'hello',
params: { lang: 'en', id: 'foo' }
})
expect(loc._normalized).toBe(true)
expect(loc.name).toBe('hello')
expect(loc.params).toEqual({ lang: 'fr', id: 'foo' })
})
it('relative params (non-named)', () => {
const loc = normalizeLocation({ params: { lang: 'fr' }}, {
path: '/en/foo',
params: { lang: 'en', id: 'foo' },
matched: [{ path: '/:lang/:id' }]
})
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/fr/foo')
})
it('relative append', () => {
const loc = normalizeLocation({ path: 'a' }, { path: '/b' }, true)
expect(loc.path).toBe('/b/a')
const loc2 = normalizeLocation({ path: 'a', append: true }, { path: '/b' })
expect(loc2.path).toBe('/b/a')
})
it('object', () => {
const loc = normalizeLocation({
path: '/abc?foo=bar#hello',
query: { baz: 'qux' },
hash: 'lol'
})
expect(loc._normalized).toBe(true)
expect(loc.path).toBe('/abc')
expect(loc.hash).toBe('#lol')
expect(JSON.stringify(loc.query)).toBe(JSON.stringify({
foo: 'bar',
baz: 'qux'
}))
})
it('skip normalized', () => {
const loc1 = {
_normalized: true,
path: '/abc?foo=bar#hello',
query: { baz: 'qux' },
hash: 'lol'
}
const loc2 = normalizeLocation(loc1)
expect(loc1).toBe(loc2)
})
it('creates copies when not normalized', () => {
const l1 = { name: 'foo' }
expect(normalizeLocation(l1)).not.toBe(l1)
const l2 = { path: '/foo' }
expect(normalizeLocation(l2)).not.toBe(l2)
const l3 = { path: '/foo', query: { foo: 'foo' }}
expect(normalizeLocation(l3)).not.toBe(l3)
})
})
})