@servicetitan/hash-browser-router 22.14.0 → 22.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@servicetitan/hash-browser-router",
|
|
3
|
-
"version": "22.
|
|
3
|
+
"version": "22.16.0",
|
|
4
4
|
"description": "",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -15,8 +15,9 @@
|
|
|
15
15
|
"src"
|
|
16
16
|
],
|
|
17
17
|
"devDependencies": {
|
|
18
|
-
"@servicetitan/react-ioc": "22.
|
|
19
|
-
"@servicetitan/web-components": "22.
|
|
18
|
+
"@servicetitan/react-ioc": "22.16.0",
|
|
19
|
+
"@servicetitan/web-components": "22.16.0",
|
|
20
|
+
"@testing-library/react": "^12.1.5",
|
|
20
21
|
"@types/history": "~4.7.10",
|
|
21
22
|
"@types/react": "~17.0.38",
|
|
22
23
|
"@types/react-router": "~5.1.17",
|
|
@@ -25,8 +26,8 @@
|
|
|
25
26
|
"react-router-dom": "~5.3.0"
|
|
26
27
|
},
|
|
27
28
|
"peerDependencies": {
|
|
28
|
-
"@servicetitan/react-ioc": "22.
|
|
29
|
-
"@servicetitan/web-components": "22.
|
|
29
|
+
"@servicetitan/react-ioc": "22.16.0",
|
|
30
|
+
"@servicetitan/web-components": "22.16.0",
|
|
30
31
|
"history": "~4.10.1",
|
|
31
32
|
"react": "~17.0.2",
|
|
32
33
|
"react-router-dom": "~5.3.0"
|
|
@@ -37,5 +38,5 @@
|
|
|
37
38
|
"cli": {
|
|
38
39
|
"webpack": false
|
|
39
40
|
},
|
|
40
|
-
"gitHead": "
|
|
41
|
+
"gitHead": "79adb1a1ec5d89601541349b01201a16bc2e2db9"
|
|
41
42
|
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { render } from '@testing-library/react';
|
|
2
|
+
import { FC } from 'react';
|
|
3
|
+
import { useHistory } from 'react-router-dom';
|
|
4
|
+
|
|
5
|
+
import { HashBrowserRouter } from '../hash-browser-router';
|
|
6
|
+
|
|
7
|
+
describe(`${HashBrowserRouter.name}`, () => {
|
|
8
|
+
const prompt = jest.fn().mockReturnValue(false);
|
|
9
|
+
let props: Parameters<typeof HashBrowserRouter>[0];
|
|
10
|
+
let history: ReturnType<typeof useHistory>;
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
props = {};
|
|
14
|
+
jest.resetAllMocks();
|
|
15
|
+
jest.useFakeTimers();
|
|
16
|
+
jest.spyOn(window.history, 'pushState');
|
|
17
|
+
jest.spyOn(window.history, 'replaceState');
|
|
18
|
+
jest.spyOn(window, 'dispatchEvent');
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
const CaptureHistory: FC = () => {
|
|
22
|
+
history = useHistory();
|
|
23
|
+
history.block(prompt);
|
|
24
|
+
return null;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const subject = () => {
|
|
28
|
+
render(
|
|
29
|
+
<HashBrowserRouter {...props}>
|
|
30
|
+
<CaptureHistory />
|
|
31
|
+
</HashBrowserRouter>
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
let location: string;
|
|
36
|
+
let locationObj: Partial<Location>;
|
|
37
|
+
|
|
38
|
+
function itPushesAndReplacesWith(
|
|
39
|
+
description: string,
|
|
40
|
+
{ url, prompt: promptLocation }: { url: () => string; prompt: () => Partial<Location> }
|
|
41
|
+
) {
|
|
42
|
+
test(`history.push pushes ${description}`, () => {
|
|
43
|
+
subject();
|
|
44
|
+
history.push(locationObj ?? location);
|
|
45
|
+
|
|
46
|
+
expect(prompt).toHaveBeenCalledWith(
|
|
47
|
+
expect.objectContaining(promptLocation()),
|
|
48
|
+
expect.anything()
|
|
49
|
+
);
|
|
50
|
+
|
|
51
|
+
expect(window.history.pushState).toHaveBeenCalledWith(expect.anything(), null, url());
|
|
52
|
+
|
|
53
|
+
jest.runOnlyPendingTimers();
|
|
54
|
+
expect(window.dispatchEvent).toHaveBeenCalledWith(
|
|
55
|
+
expect.objectContaining({ type: 'hashchange' })
|
|
56
|
+
);
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
test(`history.replace replaces with ${description}`, () => {
|
|
60
|
+
subject();
|
|
61
|
+
history.replace(locationObj ?? location);
|
|
62
|
+
|
|
63
|
+
expect(window.history.replaceState).toHaveBeenCalledWith(
|
|
64
|
+
expect.anything(),
|
|
65
|
+
null,
|
|
66
|
+
url()
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
expect(prompt).toHaveBeenCalledWith(
|
|
70
|
+
expect.objectContaining(promptLocation()),
|
|
71
|
+
expect.anything()
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
jest.runOnlyPendingTimers();
|
|
75
|
+
expect(window.dispatchEvent).toHaveBeenCalledWith(
|
|
76
|
+
expect.objectContaining({ type: 'hashchange' })
|
|
77
|
+
);
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
beforeEach(() => (location = '/foo'));
|
|
82
|
+
|
|
83
|
+
itPushesAndReplacesWith('/#{location}', {
|
|
84
|
+
url: () => `/#${location}`,
|
|
85
|
+
prompt: () => ({ pathname: location, search: '', hash: '' }),
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
describe('when location starts with "/#"', () => {
|
|
89
|
+
beforeEach(() => (location = '/#bar'));
|
|
90
|
+
|
|
91
|
+
itPushesAndReplacesWith('location', {
|
|
92
|
+
url: () => `${location}`,
|
|
93
|
+
prompt: () => ({ pathname: `${location.replace('/#', '')}`, search: '', hash: '' }),
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
describe('with a basename', () => {
|
|
98
|
+
beforeEach(() => (props.basename = '/baz'));
|
|
99
|
+
|
|
100
|
+
itPushesAndReplacesWith('"/#{basename}{location}"', {
|
|
101
|
+
url: () => `/#${props.basename}${location}`,
|
|
102
|
+
prompt: () => ({ pathname: location, search: '', hash: '' }),
|
|
103
|
+
});
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
describe('with location object', () => {
|
|
107
|
+
beforeEach(() => (locationObj = { pathname: '/foo?baz=qux' })); // testing pathname with search params
|
|
108
|
+
|
|
109
|
+
itPushesAndReplacesWith('"/#{location.pathname}"', {
|
|
110
|
+
url: () => `/#${locationObj.pathname}`,
|
|
111
|
+
prompt: () => ({
|
|
112
|
+
pathname: `${locationObj.pathname!.match(/^([^?]+)/)![0]}`,
|
|
113
|
+
search: `${locationObj.pathname!.match(/(\?.+)$/)![0]}`,
|
|
114
|
+
hash: '',
|
|
115
|
+
}),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe('when location includes a hash', () => {
|
|
119
|
+
beforeEach(() => (locationObj.hash = '#bar'));
|
|
120
|
+
|
|
121
|
+
itPushesAndReplacesWith('location', {
|
|
122
|
+
url: () => `${locationObj.pathname}${locationObj.hash}`,
|
|
123
|
+
prompt: () => ({
|
|
124
|
+
pathname: `${locationObj.hash!.replace('#', '')}`,
|
|
125
|
+
search: '',
|
|
126
|
+
hash: '',
|
|
127
|
+
}),
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('with a basename', () => {
|
|
132
|
+
beforeEach(() => (props.basename = '/baz'));
|
|
133
|
+
|
|
134
|
+
itPushesAndReplacesWith('"/#{basename}{location.pathname}', {
|
|
135
|
+
url: () => `/#${props.basename}${locationObj.pathname}`,
|
|
136
|
+
prompt: () => ({
|
|
137
|
+
pathname: `${locationObj.pathname!.match(/^([^?]+)/)![0]}`,
|
|
138
|
+
search: `${locationObj.pathname!.match(/(\?.+)$/)![0]}`,
|
|
139
|
+
hash: '',
|
|
140
|
+
}),
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('when location has no pathname', () => {
|
|
144
|
+
beforeEach(() => delete locationObj.pathname);
|
|
145
|
+
|
|
146
|
+
itPushesAndReplacesWith('/', {
|
|
147
|
+
url: () => '/',
|
|
148
|
+
prompt: () => ({ pathname: '/', search: '', hash: '' }),
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
});
|
|
153
|
+
});
|