-
Notifications
You must be signed in to change notification settings - Fork 79
/
StateHistory.ts
175 lines (150 loc) · 6.32 KB
/
StateHistory.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
const { afterEach, beforeEach, describe, it } = intern.getInterface('bdd');
const { assert } = intern.getPlugin('chai');
import { stub } from 'sinon';
import global from '../../../../src/shim/global';
import { add } from '../../../../src/has/has';
import { StateHistory } from '../../../../src/routing/history/StateHistory';
const onChange = stub();
describe('StateHistory', () => {
let sandbox: HTMLIFrameElement;
beforeEach(() => {
sandbox = document.createElement('iframe');
sandbox.src = '../../tests/routing/support/sandbox.html';
document.body.appendChild(sandbox);
return new Promise((resolve) => {
sandbox.addEventListener('load', function() {
resolve();
});
});
});
afterEach(() => {
document.body.removeChild(sandbox);
sandbox = null as any;
onChange.reset();
add('public-path', undefined, true);
global.window.__public_path__ = undefined;
});
it('initializes current path to current location', () => {
sandbox.contentWindow!.history.pushState({}, '', '/foo?bar');
assert.equal(new StateHistory({ onChange, window: sandbox.contentWindow! }).current, '/foo?bar');
});
it('location defers to the global object', () => {
assert.equal(new StateHistory({ onChange }).current, window.location.pathname + window.location.search);
});
it('prefixes path with leading slash if necessary', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
assert.equal(history.prefix('/foo'), '/foo');
assert.equal(history.prefix('foo'), '/foo');
});
it('prefixes path removes # characters', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
assert.equal(history.prefix('#/foo'), '/foo');
assert.equal(history.prefix('#foo'), '/foo');
});
it('update path', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
history.set('/foo');
assert.equal(history.current, '/foo');
assert.equal(sandbox.contentWindow!.location.pathname, '/foo');
});
it('update path, does not update path if path is set to the current value', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
const beforeLength = sandbox.contentWindow!.history.length;
history.set('/bar');
history.set('/bar');
const afterLength = sandbox.contentWindow!.history.length;
assert.equal(afterLength - beforeLength, 1);
});
it('update path, adds leading slash if necessary', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
history.set('foo');
assert.equal(history.current, '/foo');
assert.equal(sandbox.contentWindow!.location.pathname, '/foo');
});
it('emits change when path is updated', () => {
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
assert.deepEqual(onChange.firstCall.args, [
sandbox.contentWindow.location.pathname + sandbox.contentWindow.location.search
]);
history.set('/foo');
assert.deepEqual(onChange.secondCall.args, ['/foo']);
});
it('does not emit change if path is set to the current value', () => {
sandbox.contentWindow!.history.pushState({}, '', '/foo');
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
history.set('/foo');
assert.isTrue(onChange.calledOnce);
});
describe('with base', () => {
it('throws if base contains #', () => {
assert.throws(
() => {
new StateHistory({ onChange, base: '/foo#bar', window });
},
TypeError,
"base must not contain '#' or '?'"
);
});
it('throws if base contains ?', () => {
assert.throws(
() => {
new StateHistory({ onChange, base: '/foo?bar', window });
},
TypeError,
"base must not contain '#' or '?'"
);
});
it('initializes current path, taking out the base, with trailing slash', () => {
sandbox.contentWindow!.history.pushState({}, '', '/foo/bar?baz');
assert.equal(
new StateHistory({ onChange, base: '/foo/', window: sandbox.contentWindow! }).current,
'/bar?baz'
);
});
it('initializes current path, taking out the base, without trailing slash', () => {
sandbox.contentWindow!.history.pushState({}, '', '/foo/bar?baz');
assert.equal(
new StateHistory({ onChange, base: '/foo', window: sandbox.contentWindow! }).current,
'/bar?baz'
);
});
it("initializes current path to / if it's not a base suffix", () => {
sandbox.contentWindow!.history.pushState({}, '', '/foo/bar?baz');
assert.equal(new StateHistory({ onChange, base: '/thud/', window: sandbox.contentWindow! }).current, '/');
});
it('#prefix prefixes path with the base (with trailing slash)', () => {
const history = new StateHistory({ onChange, base: '/foo/', window: sandbox.contentWindow! });
assert.equal(history.prefix('/bar'), '/foo/bar');
assert.equal(history.prefix('bar'), '/foo/bar');
});
it('#prefix prefixes path with the base (without trailing slash)', () => {
const history = new StateHistory({ onChange, base: '/foo', window: sandbox.contentWindow! });
assert.equal(history.prefix('/bar'), '/foo/bar');
assert.equal(history.prefix('bar'), '/foo/bar');
});
it('#set expands the path with the base when pushing state, with trailing slash', () => {
const history = new StateHistory({ onChange, base: '/foo/', window: sandbox.contentWindow! });
history.set('/foo/bar');
assert.equal(history.current, '/bar');
assert.equal(sandbox.contentWindow!.location.pathname, '/foo/bar');
history.set('/foo/baz');
assert.equal(history.current, '/baz');
assert.equal(sandbox.contentWindow!.location.pathname, '/foo/baz');
});
it('#set expands the path with the base when pushing state, without trailing slash', () => {
const history = new StateHistory({ onChange, base: '/foo', window: sandbox.contentWindow! });
history.set('/foo/bar');
assert.equal(history.current, '/bar');
assert.equal(sandbox.contentWindow!.location.pathname, '/foo/bar');
history.set('/foo/baz');
assert.equal(history.current, '/baz');
assert.equal(sandbox.contentWindow!.location.pathname, '/foo/baz');
});
it('Assumes base set in window __public_path__ variable if not explicitly set', () => {
add('public-path', 'base/path', true);
global.window.__public_path__ = 'base/path';
const history = new StateHistory({ onChange, window: sandbox.contentWindow! });
assert.strictEqual(history.prefix('foo'), '/base/path/foo');
});
});
});