@servicetitan/hash-browser-router 40.2.2 → 40.3.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": "40.
|
|
3
|
+
"version": "40.3.0",
|
|
4
4
|
"description": "React router component using hash-based navigation",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -12,11 +12,15 @@
|
|
|
12
12
|
"sideEffects": false,
|
|
13
13
|
"files": [
|
|
14
14
|
"dist",
|
|
15
|
-
"src"
|
|
15
|
+
"src",
|
|
16
|
+
"!**/__tests__",
|
|
17
|
+
"!**/__mocks__",
|
|
18
|
+
"!**/__snapshots__",
|
|
19
|
+
"!**/*.test.*"
|
|
16
20
|
],
|
|
17
21
|
"devDependencies": {
|
|
18
|
-
"@servicetitan/react-ioc": "40.
|
|
19
|
-
"@servicetitan/web-components": "40.
|
|
22
|
+
"@servicetitan/react-ioc": "40.3.0",
|
|
23
|
+
"@servicetitan/web-components": "40.3.0",
|
|
20
24
|
"@testing-library/dom": "^10.4.2",
|
|
21
25
|
"@testing-library/react": "^16.3.3",
|
|
22
26
|
"@types/history": "~4.7.10",
|
|
@@ -27,8 +31,8 @@
|
|
|
27
31
|
"react-router-dom": "~5.3.0"
|
|
28
32
|
},
|
|
29
33
|
"peerDependencies": {
|
|
30
|
-
"@servicetitan/react-ioc": "40.
|
|
31
|
-
"@servicetitan/web-components": "40.
|
|
34
|
+
"@servicetitan/react-ioc": "40.3.0",
|
|
35
|
+
"@servicetitan/web-components": "40.3.0",
|
|
32
36
|
"history": "~4.10.1",
|
|
33
37
|
"react": ">=17.0.2",
|
|
34
38
|
"react-router-dom": "~5.3.0"
|
|
@@ -1,174 +0,0 @@
|
|
|
1
|
-
import { act, render } from '@testing-library/react';
|
|
2
|
-
import { FC } from 'react';
|
|
3
|
-
import { useHistory } from 'react-router-dom';
|
|
4
|
-
import { HashBrowserRouter } from '../hash-browser-router';
|
|
5
|
-
|
|
6
|
-
describe(`${HashBrowserRouter.name}`, () => {
|
|
7
|
-
const prompt = jest.fn().mockReturnValue(false);
|
|
8
|
-
let props: Parameters<typeof HashBrowserRouter>[0];
|
|
9
|
-
let history: ReturnType<typeof useHistory>;
|
|
10
|
-
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
props = {};
|
|
13
|
-
jest.resetAllMocks();
|
|
14
|
-
jest.useFakeTimers();
|
|
15
|
-
jest.spyOn(window.history, 'pushState');
|
|
16
|
-
jest.spyOn(window.history, 'replaceState');
|
|
17
|
-
jest.spyOn(window, 'dispatchEvent');
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
const CaptureHistory: FC = () => {
|
|
21
|
-
history = useHistory();
|
|
22
|
-
history.block(prompt);
|
|
23
|
-
return null;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const subject = () => {
|
|
27
|
-
render(
|
|
28
|
-
<HashBrowserRouter {...props}>
|
|
29
|
-
<CaptureHistory />
|
|
30
|
-
</HashBrowserRouter>
|
|
31
|
-
);
|
|
32
|
-
};
|
|
33
|
-
|
|
34
|
-
let location: string;
|
|
35
|
-
let locationObj: Partial<Location>;
|
|
36
|
-
|
|
37
|
-
function itPushesAndReplacesWith(
|
|
38
|
-
description: string,
|
|
39
|
-
{ url, prompt: promptLocation }: { url: () => string; prompt: () => Partial<Location> }
|
|
40
|
-
) {
|
|
41
|
-
test(`history.push pushes ${description}`, () => {
|
|
42
|
-
subject();
|
|
43
|
-
|
|
44
|
-
act(() => 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
|
-
|
|
62
|
-
act(() => history.replace(locationObj ?? location));
|
|
63
|
-
|
|
64
|
-
expect(window.history.replaceState).toHaveBeenCalledWith(
|
|
65
|
-
expect.anything(),
|
|
66
|
-
null,
|
|
67
|
-
url()
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
expect(prompt).toHaveBeenCalledWith(
|
|
71
|
-
expect.objectContaining(promptLocation()),
|
|
72
|
-
expect.anything()
|
|
73
|
-
);
|
|
74
|
-
|
|
75
|
-
jest.runOnlyPendingTimers();
|
|
76
|
-
expect(window.dispatchEvent).toHaveBeenCalledWith(
|
|
77
|
-
expect.objectContaining({ type: 'hashchange' })
|
|
78
|
-
);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
beforeEach(() => (location = '/foo?baz=qux')); // testing location with search
|
|
83
|
-
|
|
84
|
-
function getPathname(url: string) {
|
|
85
|
-
return RegExp(/^([^?]+)/).exec(url)![0];
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function getSearch(url: string) {
|
|
89
|
-
return RegExp(/(\?.+)$/).exec(url)![0];
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
itPushesAndReplacesWith('/#{location}', {
|
|
93
|
-
url: () => `/#${location}`,
|
|
94
|
-
prompt: () => ({
|
|
95
|
-
pathname: getPathname(location),
|
|
96
|
-
search: getSearch(location),
|
|
97
|
-
hash: '',
|
|
98
|
-
}),
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
describe('when location starts with "/#"', () => {
|
|
102
|
-
beforeEach(() => (location = '/#bar?baz=qux'));
|
|
103
|
-
|
|
104
|
-
itPushesAndReplacesWith('location', {
|
|
105
|
-
url: () => `${location}`,
|
|
106
|
-
prompt: () => ({
|
|
107
|
-
pathname: getPathname(location).replace('/#', ''),
|
|
108
|
-
search: getSearch(location),
|
|
109
|
-
hash: '',
|
|
110
|
-
}),
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
|
|
114
|
-
describe('with a basename', () => {
|
|
115
|
-
beforeEach(() => (props.basename = '/baz'));
|
|
116
|
-
|
|
117
|
-
itPushesAndReplacesWith('"/#{basename}{location}"', {
|
|
118
|
-
url: () => `/#${props.basename}${location}`,
|
|
119
|
-
prompt: () => ({
|
|
120
|
-
pathname: getPathname(location),
|
|
121
|
-
search: getSearch(location),
|
|
122
|
-
hash: '',
|
|
123
|
-
}),
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
|
|
127
|
-
describe('with location object', () => {
|
|
128
|
-
beforeEach(() => (locationObj = { pathname: '/foo', search: '?baz=qux' }));
|
|
129
|
-
|
|
130
|
-
itPushesAndReplacesWith('"/#{location.pathname}{location.search}"', {
|
|
131
|
-
url: () => `/#${locationObj.pathname}${locationObj.search}`,
|
|
132
|
-
prompt: () => ({
|
|
133
|
-
pathname: locationObj.pathname,
|
|
134
|
-
search: locationObj.search,
|
|
135
|
-
hash: '',
|
|
136
|
-
}),
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
describe('when location includes a hash', () => {
|
|
140
|
-
beforeEach(() => (locationObj.hash = '#bar'));
|
|
141
|
-
|
|
142
|
-
itPushesAndReplacesWith('/#{location.pathname}{location.search}{location.hash}', {
|
|
143
|
-
url: () => `${locationObj.pathname}${locationObj.search}${locationObj.hash}`,
|
|
144
|
-
prompt: () => ({
|
|
145
|
-
pathname: `${locationObj.hash!.replace('#', '')}`,
|
|
146
|
-
search: '',
|
|
147
|
-
hash: '',
|
|
148
|
-
}),
|
|
149
|
-
});
|
|
150
|
-
});
|
|
151
|
-
|
|
152
|
-
describe('with a basename', () => {
|
|
153
|
-
beforeEach(() => (props.basename = '/baz'));
|
|
154
|
-
|
|
155
|
-
itPushesAndReplacesWith('"/#{basename}{location.pathname}{location.search}', {
|
|
156
|
-
url: () => `/#${props.basename}${locationObj.pathname}${locationObj.search}`,
|
|
157
|
-
prompt: () => ({
|
|
158
|
-
pathname: locationObj.pathname,
|
|
159
|
-
search: locationObj.search,
|
|
160
|
-
hash: '',
|
|
161
|
-
}),
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
describe('when location has no pathname', () => {
|
|
165
|
-
beforeEach(() => delete locationObj.pathname);
|
|
166
|
-
|
|
167
|
-
itPushesAndReplacesWith('/', {
|
|
168
|
-
url: () => '/',
|
|
169
|
-
prompt: () => ({ pathname: '/', search: '', hash: '' }),
|
|
170
|
-
});
|
|
171
|
-
});
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
});
|