Repository navigation
Expand file tree
/
Copy pathhistory.js
More file actions
156 lines (123 loc) · 4.91 KB
/
Copy pathhistory.js
File metadata and controls
156 lines (123 loc) · 4.91 KB
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
"use strict";
var jsdom = require("../..").jsdom;
exports["a default window should have a history object with correct default values"] = function (t) {
var window = jsdom().parentWindow;
t.ok(window.history);
t.strictEqual(window.history.state, null);
t.strictEqual(window.history.length, 1);
t.done();
};
exports["the history object should update correctly when calling pushState/replaceState"] = function (t) {
var window = jsdom("", {
url: "http://www.example.org/"
}).parentWindow;
window.addEventListener("popstate", function () {
t.fail("popstate should not fire as a result of a pushState() or replaceState() call");
});
// Absolute path
window.history.pushState({ foo: "one" }, "unused title", "/bar/baz#fuzz");
t.strictEqual(window.history.length, 2);
t.strictEqual(window.history.state.foo, "one");
t.strictEqual(window.location.pathname, "/bar/baz");
t.strictEqual(window.location.hash, "#fuzz");
window.history.pushState({ foo: "two" }, "unused title 2", "/bar/foo#boo");
t.strictEqual(window.history.length, 3);
t.strictEqual(window.history.state.foo, "two");
t.strictEqual(window.location.pathname, "/bar/foo");
t.strictEqual(window.location.hash, "#boo");
// Relative path
window.history.pushState({ foo: "three" }, "unused title 3", "fizz");
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "three");
t.strictEqual(window.location.pathname, "/bar/fizz");
t.strictEqual(window.location.hash, "");
window.history.replaceState({ foo: "four" }, "unused title 4", "/buzz");
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "four");
t.strictEqual(window.location.pathname, "/buzz");
t.done();
};
exports["the history object should update correctly when calling forward/back/go"] = function (t) {
var window = jsdom().parentWindow;
var initialPath = window.location.pathname;
[
[{ foo: "bar" }, "title 1", "/bar"],
[{ foo: "baz" }, "title 2", "/baz"],
[{ foo: "buzz" }, "title 3", "/buzz"]
].forEach(function (args) {
window.history.pushState.apply(window.history, args);
});
// Sanity check
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "buzz");
t.strictEqual(window.location.pathname, "/buzz");
// Test forward boundary
window.history.forward();
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "buzz");
t.strictEqual(window.location.pathname, "/buzz");
window.history.back();
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "baz");
t.strictEqual(window.location.pathname, "/baz");
window.history.back();
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "bar");
t.strictEqual(window.location.pathname, "/bar");
window.history.back();
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state, null);
t.strictEqual(window.location.pathname, initialPath);
// Test backward boundary
window.history.back();
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state, null);
t.strictEqual(window.location.pathname, initialPath);
window.history.go(2);
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "baz");
t.strictEqual(window.location.pathname, "/baz");
t.done();
};
exports["the history object should update correctly when calling pushState with index behind length"] = function (t) {
var window = jsdom().parentWindow;
[
[{ foo: "bar" }, "title 1", "/bar"],
[{ foo: "baz" }, "title 2", "/baz"],
[{ foo: "buzz" }, "title 3", "/buzz"]
].forEach(function (args) {
window.history.pushState.apply(window.history, args);
});
// Sanity check
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "buzz");
t.strictEqual(window.location.pathname, "/buzz");
window.history.go(-2);
t.strictEqual(window.history.length, 4);
t.strictEqual(window.history.state.foo, "bar");
t.strictEqual(window.location.pathname, "/bar");
// Call pushState when index is behind length
window.history.pushState({ foo: "bar-b" }, "title 2b", "/bar/b");
t.strictEqual(window.history.length, 3);
t.strictEqual(window.history.state.foo, "bar-b");
t.strictEqual(window.location.pathname, "/bar/b");
t.done();
};
exports["the history object should fire popstate on the window while navigating the history"] = function (t) {
var window = jsdom().parentWindow;
var eventFired = false;
var state = { foo: "bar" };
var eventState;
window.addEventListener("popstate", function (event) {
eventFired = true;
eventState = event.state;
});
window.history.pushState(state, "title", "bar");
window.history.pushState(null, "", "baz");
window.history.back();
setTimeout(function () {
t.ok(eventFired, "popstate event should be fired.");
t.strictEqual(state, eventState);
t.done();
}, 10);
};