forked from vuejs/router
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtml5.spec.ts
199 lines (179 loc) · 6.02 KB
/
html5.spec.ts
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
import { JSDOM } from 'jsdom'
import { createWebHistory } from '../../src/history/html5'
import { createDom } from '../utils'
// override the value of isBrowser because the variable is created before JSDOM
// is created
jest.mock('../../src/utils/env', () => ({
isBrowser: true,
}))
// These unit tests are supposed to tests very specific scenarios that are easier to setup
// on a unit test than an e2e tests
describe('History HTMl5', () => {
let dom: JSDOM
beforeAll(() => {
dom = createDom()
})
beforeEach(() => {
// empty the state to simulate an initial navigation by default
window.history.replaceState(null, '', '')
})
afterAll(() => {
dom.window.close()
})
afterEach(() => {
// ensure no base element is left after a test as only the first is
// respected
for (let element of Array.from(document.getElementsByTagName('base')))
element.remove()
})
it('handles a basic base', () => {
expect(createWebHistory().base).toBe('')
expect(createWebHistory('/').base).toBe('')
expect(createWebHistory('/#').base).toBe('/#')
expect(createWebHistory('#!').base).toBe('#!')
expect(createWebHistory('#other').base).toBe('#other')
})
it('handles a base tag', () => {
const baseEl = document.createElement('base')
baseEl.href = '/foo/'
document.head.appendChild(baseEl)
expect(createWebHistory().base).toBe('/foo')
})
it('handles a base tag with origin', () => {
const baseEl = document.createElement('base')
baseEl.href = 'https://example.com/foo/'
document.head.appendChild(baseEl)
expect(createWebHistory().base).toBe('/foo')
})
it('handles a base tag with origin without trailing slash', () => {
const baseEl = document.createElement('base')
baseEl.href = 'https://example.com/bar'
document.head.appendChild(baseEl)
expect(createWebHistory().base).toBe('/bar')
})
it('ignores base tag if base is provided', () => {
const baseEl = document.createElement('base')
baseEl.href = '/foo/'
document.head.appendChild(baseEl)
expect(createWebHistory('/bar/').base).toBe('/bar')
})
it('handles a non-empty base', () => {
expect(createWebHistory('/foo/').base).toBe('/foo')
expect(createWebHistory('/foo').base).toBe('/foo')
})
it('handles a single hash base', () => {
expect(createWebHistory('#').base).toBe('#')
expect(createWebHistory('#/').base).toBe('#')
expect(createWebHistory('#!/').base).toBe('#!')
expect(createWebHistory('#other/').base).toBe('#other')
})
it('handles a non-empty hash base', () => {
expect(createWebHistory('#/bar').base).toBe('#/bar')
expect(createWebHistory('#/bar/').base).toBe('#/bar')
expect(createWebHistory('#!/bar/').base).toBe('#!/bar')
expect(createWebHistory('#other/bar/').base).toBe('#other/bar')
})
it('prepends the host to support // urls', () => {
let history = createWebHistory()
let spy = jest.spyOn(window.history, 'pushState')
history.push('/foo')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'https://example.com/foo'
)
history.push('//foo')
expect(spy).toHaveBeenLastCalledWith(
expect.anything(),
expect.any(String),
'https://example.com//foo'
)
spy.mockRestore()
})
describe('specific to base containing a hash', () => {
it('calls push with hash part of the url with a base', () => {
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
let initialSpy = jest.spyOn(window.history, 'replaceState')
let history = createWebHistory('#')
// initial navigation
expect(initialSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#/'
)
let spy = jest.spyOn(window.history, 'pushState')
history.push('/foo')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#/foo'
)
spy.mockRestore()
initialSpy.mockRestore()
})
it('works with something after the hash in the base', () => {
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
let initialSpy = jest.spyOn(window.history, 'replaceState')
let history = createWebHistory('#something')
// initial navigation
expect(initialSpy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#something/'
)
let spy = jest.spyOn(window.history, 'pushState')
history.push('/foo')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#something/foo'
)
spy.mockRestore()
initialSpy.mockRestore()
})
it('works with #! and on a file with initial location', () => {
dom.reconfigure({ url: 'file:///usr/etc/index.html#!/foo' })
let spy = jest.spyOn(window.history, 'replaceState')
createWebHistory('#!')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#!/foo'
)
spy.mockRestore()
})
it('works with #other', () => {
dom.reconfigure({ url: 'file:///usr/etc/index.html' })
let spy = jest.spyOn(window.history, 'replaceState')
createWebHistory('#other')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#other/'
)
spy.mockRestore()
})
it('works with custom#other in domain', () => {
dom.reconfigure({ url: 'https://esm.dev/custom' })
let spy = jest.spyOn(window.history, 'replaceState')
createWebHistory('custom#other')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#other/'
)
spy.mockRestore()
})
it('works with #! and a host with initial location', () => {
dom.reconfigure({ url: 'https://esm.dev/#!/foo' })
let spy = jest.spyOn(window.history, 'replaceState')
createWebHistory('/#!')
expect(spy).toHaveBeenCalledWith(
expect.anything(),
expect.any(String),
'#!/foo'
)
spy.mockRestore()
})
})
})