-
-
Notifications
You must be signed in to change notification settings - Fork 10.2k
/
hash-test.ts
153 lines (127 loc) · 4.54 KB
/
hash-test.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
/* eslint-disable jest/expect-expect */
import { JSDOM } from "jsdom";
import type { HashHistory } from "@remix-run/router";
import { createHashHistory } from "@remix-run/router";
import Listen from "./TestSequences/Listen";
import InitialLocationDefaultKey from "./TestSequences/InitialLocationDefaultKey";
import PushNewLocation from "./TestSequences/PushNewLocation";
import PushSamePath from "./TestSequences/PushSamePath";
import PushState from "./TestSequences/PushState";
import PushStateInvalid from "./TestSequences/PushStateInvalid";
import PushMissingPathname from "./TestSequences/PushMissingPathname";
import PushRelativePathnameWarning from "./TestSequences/PushRelativePathnameWarning";
import ReplaceNewLocation from "./TestSequences/ReplaceNewLocation";
import ReplaceSamePath from "./TestSequences/ReplaceSamePath";
import ReplaceState from "./TestSequences/ReplaceState";
import EncodedReservedCharacters from "./TestSequences/EncodedReservedCharacters";
import GoBack from "./TestSequences/GoBack";
import GoForward from "./TestSequences/GoForward";
import ListenPopOnly from "./TestSequences/ListenPopOnly";
// TODO: Do we still need this?
// const canGoWithoutReload = window.navigator.userAgent.indexOf('Firefox') === -1;
// const describeGo = canGoWithoutReload ? describe : describe.skip;
describe("a hash history", () => {
let history: HashHistory;
let dom: JSDOM;
beforeEach(() => {
// Need to use our own custom DOM in order to get a working history
dom = new JSDOM(`<!DOCTYPE html><p>History Example</p>`, {
url: "https://example.org/",
});
dom.window.history.replaceState(null, "", "#/");
history = createHashHistory({ window: dom.window as unknown as Window });
});
it("knows how to create hrefs from location objects", () => {
const href = history.createHref({
pathname: "/the/path",
search: "?the=query",
hash: "#the-hash",
});
expect(href).toEqual("#/the/path?the=query#the-hash");
});
it("knows how to create hrefs from strings", () => {
const href = history.createHref("/the/path?the=query#the-hash");
expect(href).toEqual("#/the/path?the=query#the-hash");
});
it("does not encode the generated path", () => {
const encodedHref = history.createHref({
pathname: "/%23abc",
});
expect(encodedHref).toEqual("#/%23abc");
const unencodedHref = history.createHref({
pathname: "/#abc",
});
expect(unencodedHref).toEqual("#/#abc");
});
describe("listen", () => {
it("does not immediately call listeners", () => {
Listen(history);
});
it("calls listeners only for POP actions", () => {
ListenPopOnly(history);
});
});
describe("the initial location", () => {
it('has the "default" key', () => {
InitialLocationDefaultKey(history);
});
});
describe("push a new path", () => {
it("calls change listeners with the new location", () => {
PushNewLocation(history);
});
});
describe("push the same path", () => {
it("calls change listeners with the new location", async () => {
await PushSamePath(history);
});
});
describe("push state", () => {
it("calls change listeners with the new location", () => {
PushState(history);
});
it("re-throws when using non-serializable state", () => {
PushStateInvalid(history, dom.window);
});
});
describe("push with no pathname", () => {
it("reuses the current location pathname", () => {
PushMissingPathname(history);
});
});
describe("push with a relative pathname", () => {
it("issues a warning", () => {
PushRelativePathnameWarning(history);
});
});
describe("replace a new path", () => {
it("calls change listeners with the new location", () => {
ReplaceNewLocation(history);
});
});
describe("replace the same path", () => {
it("calls change listeners with the new location", () => {
ReplaceSamePath(history);
});
});
describe("replace state", () => {
it("calls change listeners with the new location", () => {
ReplaceState(history);
});
});
describe("location created with encoded/unencoded reserved characters", () => {
it("produces different location objects", () => {
EncodedReservedCharacters(history);
});
});
describe("back", () => {
it("calls change listeners with the previous location", async () => {
await GoBack(history);
});
});
describe("forward", () => {
it("calls change listeners with the next location", async () => {
await GoForward(history);
});
});
});