forked from remix-run/history
/
HashHistory-test.js
279 lines (231 loc) · 8.25 KB
/
HashHistory-test.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
import expect from "expect"
import createHistory from "../createHashHistory"
import { canUseDOM, supportsGoWithoutReloadUsingHash } from "../DOMUtils"
import * as TestSequences from "./TestSequences"
const describeHistory = canUseDOM ? describe : describe.skip
const canGoWithoutReload = canUseDOM && supportsGoWithoutReloadUsingHash()
const describeGo = canGoWithoutReload ? describe : describe.skip
describeHistory("a hash history", () => {
beforeEach(() => {
if (window.location.hash !== "") window.location.hash = ""
})
describe("by default", () => {
let history
beforeEach(() => {
history = createHistory()
})
describe("listen", () => {
it("does not immediately call listeners", done => {
TestSequences.Listen(history, done)
})
})
describe("the initial location", () => {
it("does not have a key", done => {
TestSequences.InitialLocationNoKey(history, done)
})
})
describe("push a new path", () => {
it("calls change listeners with the new location", done => {
TestSequences.PushNewLocation(history, done)
})
})
describe("push the same path", () => {
it("calls change listeners with the same location and emits a warning", done => {
TestSequences.PushSamePathWarning(history, done)
})
})
describe("push state", () => {
it("calls change listeners with the new location and emits a warning", done => {
TestSequences.PushStateWarning(history, done)
})
})
describe("push with no pathname", () => {
it("calls change listeners with the normalized location", done => {
TestSequences.PushMissingPathname(history, done)
})
})
describe("push with a relative pathname", () => {
it("calls change listeners with the normalized location", done => {
TestSequences.PushRelativePathname(history, done)
})
})
describe("push with a unicode path string", () => {
it("creates a location with decoded properties", done => {
TestSequences.PushUnicodeLocation(history, done)
})
})
describe("push with an encoded path string", () => {
it("creates a location object with decoded pathname", done => {
TestSequences.PushEncodedLocation(history, done)
})
})
describe("push with an invalid path string (bad percent-encoding)", () => {
it("throws an error", done => {
TestSequences.PushInvalidPathname(history, done)
})
})
describe("replace a new path", () => {
it("calls change listeners with the new location", done => {
TestSequences.ReplaceNewLocation(history, done)
})
})
describe("replace the same path", () => {
it("calls change listeners with the new location", done => {
TestSequences.ReplaceSamePath(history, done)
})
})
describe("replace state", () => {
it("calls change listeners with the new location and emits a warning", done => {
TestSequences.ReplaceStateWarning(history, done)
})
})
describe("replace with an invalid path string (bad percent-encoding)", () => {
it("throws an error", done => {
TestSequences.ReplaceInvalidPathname(history, done)
})
})
describe("location created by encoded and unencoded pathname", () => {
it("produces the same location.pathname", done => {
TestSequences.LocationPathnameAlwaysDecoded(history, done)
})
})
describe("location created with encoded/unencoded reserved characters", () => {
it("produces different location objects", done => {
TestSequences.EncodedReservedCharacters(history, done)
})
})
describeGo("goBack", () => {
it("calls change listeners with the previous location", done => {
TestSequences.GoBack(history, done)
})
})
describeGo("goForward", () => {
it("calls change listeners with the next location", done => {
TestSequences.GoForward(history, done)
})
})
describe("block", () => {
it("blocks all transitions", done => {
TestSequences.BlockEverything(history, done)
})
})
describeGo("block a POP without listening", () => {
it("receives the next location and action as arguments", done => {
TestSequences.BlockPopWithoutListening(history, done)
})
})
})
describe("that denies all transitions", () => {
const getUserConfirmation = (_, callback) => callback(false)
let history
beforeEach(() => {
history = createHistory({
getUserConfirmation
})
})
describe("clicking on a link (push)", () => {
it("does not update the location", done => {
TestSequences.DenyPush(history, done)
})
})
describeGo("clicking the back button (goBack)", () => {
it("does not update the location", done => {
TestSequences.DenyGoBack(history, done)
})
})
describeGo("clicking the forward button (goForward)", () => {
it("does not update the location", done => {
TestSequences.DenyGoForward(history, done)
})
})
})
describe("a transition hook", () => {
const getUserConfirmation = (_, callback) => callback(true)
let history
beforeEach(() => {
history = createHistory({
getUserConfirmation
})
})
it("receives the next location and action as arguments", done => {
TestSequences.TransitionHookArgs(history, done)
})
const itBackButton = canGoWithoutReload ? it : it.skip
itBackButton("is called when the back button is clicked", done => {
TestSequences.BackButtonTransitionHook(history, done)
})
it("cancels the transition when it returns false", done => {
TestSequences.ReturnFalseTransitionHook(history, done)
})
})
describe('"hashbang" hash path coding', () => {
let history
beforeEach(() => {
history = createHistory({
hashType: "hashbang"
})
})
it("properly encodes and decodes window.location.hash", done => {
TestSequences.HashbangHashPathCoding(history, done)
})
})
describe('"noslash" hash path coding', () => {
let history
beforeEach(() => {
history = createHistory({
hashType: "noslash"
})
})
it("properly encodes and decodes window.location.hash", done => {
TestSequences.NoslashHashPathCoding(history, done)
})
})
describe('"slash" hash path coding', () => {
let history
beforeEach(() => {
history = createHistory({
hashType: "slash"
})
})
it("properly encodes and decodes window.location.hash", done => {
TestSequences.SlashHashPathCoding(history, done)
})
})
describe("basename", () => {
it("strips the basename from the pathname", () => {
window.location.hash = "#/prefix/pathname"
const history = createHistory({ basename: "/prefix" })
expect(history.location.pathname).toEqual("/pathname")
})
it("is not case-sensitive", () => {
window.location.hash = "#/PREFIX/pathname"
const history = createHistory({ basename: "/prefix" })
expect(history.location.pathname).toEqual("/pathname")
})
it("does not strip partial prefix matches", () => {
window.location.hash = "#/prefixed/pathname"
const history = createHistory({ basename: "/prefix" })
expect(history.location.pathname).toEqual("/prefixed/pathname")
})
it("strips when path is only the prefix", () => {
window.location.hash = "#/prefix"
const history = createHistory({ basename: "/prefix" })
expect(history.location.pathname).toEqual("/")
})
it("strips with no pathname, but with a search string", () => {
window.location.hash = "#/prefix?a=b"
const history = createHistory({ basename: "/prefix" })
expect(history.location.pathname).toEqual("/")
})
it("strips with no pathname, but with a hash string", () => {
window.location.hash = "#/prefix#rest"
const history = createHistory({ basename: "/prefix" })
expect(history.location.pathname).toEqual("/")
})
it('allows URL with regex special characters', () => {
window.location.hash = '/prefix$special/hello'
const history = createHistory({ basename: '/prefix$special'})
expect(history.location.pathname).toEqual('/hello')
})
})
})