@salesforce/pwa-kit-react-sdk 3.0.0-preview.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/CHANGELOG.md +86 -0
- package/LICENSE +14 -0
- package/README.md +37 -0
- package/package.json +82 -0
- package/scripts/file-utils.js +80 -0
- package/scripts/file-utils.test.js +189 -0
- package/scripts/setup-jsdom.js +20 -0
- package/scripts/version.js +22 -0
- package/ssr/browser/main.js +122 -0
- package/ssr/browser/main.test.js +54 -0
- package/ssr/server/react-rendering.js +405 -0
- package/ssr/server/react-rendering.test.js +708 -0
- package/ssr/universal/compatibility.js +31 -0
- package/ssr/universal/components/_app/index.js +35 -0
- package/ssr/universal/components/_app/index.test.js +20 -0
- package/ssr/universal/components/_app-config/index.js +88 -0
- package/ssr/universal/components/_app-config/index.test.js +21 -0
- package/ssr/universal/components/_document/index.js +93 -0
- package/ssr/universal/components/_document/index.test.js +58 -0
- package/ssr/universal/components/_error/index.js +56 -0
- package/ssr/universal/components/_error/index.test.js +28 -0
- package/ssr/universal/components/app-error-boundary/index.js +115 -0
- package/ssr/universal/components/app-error-boundary/index.test.js +109 -0
- package/ssr/universal/components/fetch-strategy/index.js +42 -0
- package/ssr/universal/components/route-component/index.js +409 -0
- package/ssr/universal/components/route-component/index.test.js +375 -0
- package/ssr/universal/components/switch/index.js +63 -0
- package/ssr/universal/components/throw-404/index.js +37 -0
- package/ssr/universal/components/throw-404/index.test.js +26 -0
- package/ssr/universal/components/with-correlation-id/index.js +36 -0
- package/ssr/universal/components/with-legacy-get-props/index.js +86 -0
- package/ssr/universal/components/with-legacy-get-props/index.test.js +35 -0
- package/ssr/universal/components/with-react-query/index.js +103 -0
- package/ssr/universal/components/with-react-query/index.test.js +44 -0
- package/ssr/universal/contexts/index.js +71 -0
- package/ssr/universal/contexts/index.test.js +101 -0
- package/ssr/universal/errors.js +34 -0
- package/ssr/universal/errors.test.js +20 -0
- package/ssr/universal/events.js +40 -0
- package/ssr/universal/events.test.js +39 -0
- package/ssr/universal/hooks/index.js +52 -0
- package/ssr/universal/routes.js +16 -0
- package/ssr/universal/utils.client.test.js +46 -0
- package/ssr/universal/utils.js +60 -0
- package/ssr/universal/utils.server.test.js +24 -0
- package/utils/assets.js +120 -0
- package/utils/assets.test.js +106 -0
- package/utils/url.js +39 -0
- package/utils/url.test.js +47 -0
- package/utils/uuidv4.client.js +21 -0
- package/utils/uuidv4.client.test.js +27 -0
- package/utils/warnings.js +81 -0
- package/utils/warnings.test.js +48 -0
|
@@ -0,0 +1,375 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = _interopRequireDefault(require("react"));
|
|
4
|
+
var _react2 = require("@testing-library/react");
|
|
5
|
+
var _index = require("./index");
|
|
6
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
7
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
8
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } /*
|
|
9
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
10
|
+
* All rights reserved.
|
|
11
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
12
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
13
|
+
*/
|
|
14
|
+
// TODO: The way mocks are set up in this file is kinda weird...
|
|
15
|
+
/* eslint-disable @typescript-eslint/no-var-requires */
|
|
16
|
+
const delay = t => new Promise(resolve => setTimeout(resolve, t));
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Return a mock that returns true, false, false, false, which is what
|
|
20
|
+
* we want when testing shouldGetProps – always returning true would cause
|
|
21
|
+
* an infinite loop.
|
|
22
|
+
*/
|
|
23
|
+
const trueOnceThenFalse = () => jest.fn().mockReturnValue(false).mockReturnValueOnce(true);
|
|
24
|
+
const falseOnceThenTrue = () => jest.fn().mockReturnValue(true).mockReturnValueOnce(false);
|
|
25
|
+
jest.mock('../_app-config', () => {
|
|
26
|
+
const React = require('react');
|
|
27
|
+
const PropTypes = require('prop-types');
|
|
28
|
+
const MockAppConfig = () => /*#__PURE__*/React.createElement("h1", null, "MockAppConfig");
|
|
29
|
+
MockAppConfig.freeze = jest.fn(() => ({
|
|
30
|
+
frozen: 'frozen'
|
|
31
|
+
}));
|
|
32
|
+
MockAppConfig.restore = jest.fn(() => undefined);
|
|
33
|
+
MockAppConfig.extraGetPropsArgs = jest.fn(() => ({
|
|
34
|
+
anotherArg: 'anotherArg'
|
|
35
|
+
}));
|
|
36
|
+
MockAppConfig.propTypes = {
|
|
37
|
+
children: PropTypes.node
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
__esModule: true,
|
|
41
|
+
default: MockAppConfig
|
|
42
|
+
};
|
|
43
|
+
});
|
|
44
|
+
jest.mock('../../routes', () => {
|
|
45
|
+
const React = require('react');
|
|
46
|
+
const PropTypes = require('prop-types');
|
|
47
|
+
const Component = ({
|
|
48
|
+
children
|
|
49
|
+
}) => /*#__PURE__*/React.createElement("div", null, /*#__PURE__*/React.createElement("h1", null, "This is the root"), children);
|
|
50
|
+
Component.propTypes = {
|
|
51
|
+
children: PropTypes.node
|
|
52
|
+
};
|
|
53
|
+
return [{
|
|
54
|
+
path: '',
|
|
55
|
+
component: Component,
|
|
56
|
+
exact: true
|
|
57
|
+
}];
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// NOTE: `react-router-dom` is being mocked because I was not able to get around the
|
|
61
|
+
// issue where you can't use a `withRoute` HoC outside of a Router component for this
|
|
62
|
+
// specific test. TODO: Revisit this, so that we don't have to mock `react-router-dom`
|
|
63
|
+
jest.mock('react-router-dom', () => {
|
|
64
|
+
const React = require('react');
|
|
65
|
+
const hoistNonReactStatic = require('hoist-non-react-statics');
|
|
66
|
+
const withRouter = Wrapped => {
|
|
67
|
+
const wrappedComponentName = Wrapped.displayName || Wrapped.name;
|
|
68
|
+
const WithRouter = props => /*#__PURE__*/React.createElement(Wrapped, props);
|
|
69
|
+
hoistNonReactStatic(WithRouter, Wrapped);
|
|
70
|
+
WithRouter.displayName = `withRouter(${wrappedComponentName})`;
|
|
71
|
+
return WithRouter;
|
|
72
|
+
};
|
|
73
|
+
return {
|
|
74
|
+
__esModule: true,
|
|
75
|
+
default: {},
|
|
76
|
+
withRouter
|
|
77
|
+
};
|
|
78
|
+
});
|
|
79
|
+
const getMockComponent = () => {
|
|
80
|
+
const MockComponent = () => /*#__PURE__*/_react.default.createElement("p", null, "MockComponent");
|
|
81
|
+
MockComponent.displayName = 'MockComponent';
|
|
82
|
+
MockComponent.shouldGetProps = trueOnceThenFalse();
|
|
83
|
+
MockComponent.getProps = jest.fn(() => {
|
|
84
|
+
return Promise.resolve();
|
|
85
|
+
});
|
|
86
|
+
MockComponent.getTemplateName = jest.fn(() => 'MockComponent');
|
|
87
|
+
return MockComponent;
|
|
88
|
+
};
|
|
89
|
+
beforeEach(() => {
|
|
90
|
+
delete global.__HYDRATING__;
|
|
91
|
+
});
|
|
92
|
+
describe('The routeComponent component', () => {
|
|
93
|
+
test('Is a higher-order component', () => {
|
|
94
|
+
const Mock = getMockComponent();
|
|
95
|
+
const Component = (0, _index.routeComponent)(Mock);
|
|
96
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
97
|
+
isHydrating: false
|
|
98
|
+
}));
|
|
99
|
+
expect(_react2.screen.getByText(/mockComponent/i)).toBeInTheDocument();
|
|
100
|
+
});
|
|
101
|
+
test('Should call getProps on components at the right times during updates/rendering', () => {
|
|
102
|
+
const Mock = getMockComponent();
|
|
103
|
+
const Component = (0, _index.routeComponent)(Mock);
|
|
104
|
+
Component.displayName = 'routeComponent';
|
|
105
|
+
expect(Mock.shouldGetProps.mock.calls).toHaveLength(0);
|
|
106
|
+
expect(Mock.getProps.mock.calls).toHaveLength(0);
|
|
107
|
+
let wrapper;
|
|
108
|
+
return Promise.resolve().then(() => {
|
|
109
|
+
// Mock Hydrating Start
|
|
110
|
+
global.__HYDRATING__ = true;
|
|
111
|
+
}).then(() => {
|
|
112
|
+
// Simulate the initial client-side mount (hydrating=true)
|
|
113
|
+
return new Promise(resolve => {
|
|
114
|
+
wrapper = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
115
|
+
history: {
|
|
116
|
+
location: {
|
|
117
|
+
pathname: '/home/'
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
onUpdateComplete: resolve
|
|
121
|
+
}));
|
|
122
|
+
});
|
|
123
|
+
}).then(() => {
|
|
124
|
+
expect(Mock.shouldGetProps.mock.calls).toHaveLength(0);
|
|
125
|
+
expect(Mock.getProps.mock.calls).toHaveLength(0);
|
|
126
|
+
}).then(() => {
|
|
127
|
+
// Mock Hydrating Complete
|
|
128
|
+
global.__HYDRATING__ = false;
|
|
129
|
+
}).then(() => {
|
|
130
|
+
// Simulate visiting a different URL, which should trigger shouldMount() and getProps()
|
|
131
|
+
return new Promise(resolve => {
|
|
132
|
+
wrapper.rerender( /*#__PURE__*/_react.default.createElement(Component, {
|
|
133
|
+
history: {
|
|
134
|
+
location: {
|
|
135
|
+
pathname: '/plp/'
|
|
136
|
+
}
|
|
137
|
+
},
|
|
138
|
+
onUpdateComplete: resolve
|
|
139
|
+
}));
|
|
140
|
+
});
|
|
141
|
+
}).then(() => {
|
|
142
|
+
expect(Mock.shouldGetProps.mock.calls).toHaveLength(1);
|
|
143
|
+
expect(Mock.getProps.mock.calls).toHaveLength(1);
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
test('Provides defaults for getProps(), shouldGetProps() and getTemplateName()', () => {
|
|
147
|
+
const ComponentWithoutStatics = () => /*#__PURE__*/_react.default.createElement("p", null, "ComponentWithoutStatics");
|
|
148
|
+
const Component = (0, _index.routeComponent)(ComponentWithoutStatics);
|
|
149
|
+
const l1 = {
|
|
150
|
+
pathname: '/location-one/'
|
|
151
|
+
};
|
|
152
|
+
const l2 = {
|
|
153
|
+
pathname: '/location-two/'
|
|
154
|
+
};
|
|
155
|
+
const checks = [Component.shouldGetProps({
|
|
156
|
+
previousLocation: l1,
|
|
157
|
+
location: l1
|
|
158
|
+
}).then(v => expect(v).toBe(false)), Component.shouldGetProps({
|
|
159
|
+
previousLocation: l1,
|
|
160
|
+
location: l2
|
|
161
|
+
}).then(v => expect(v).toBe(true)), Component.getTemplateName().then(v => expect(v).toBe('ComponentWithoutStatics')), Component.getProps().then(result => expect(result).toBeUndefined())];
|
|
162
|
+
expect.assertions(checks.length);
|
|
163
|
+
return Promise.all(checks);
|
|
164
|
+
});
|
|
165
|
+
test('Allows the wrapped component to override getProps(), shouldGetProps and getTemplateName()', () => {
|
|
166
|
+
class Mock extends _react.default.Component {
|
|
167
|
+
static shouldGetProps({
|
|
168
|
+
location
|
|
169
|
+
}) {
|
|
170
|
+
return location.pathname === '/should-get-props/';
|
|
171
|
+
}
|
|
172
|
+
static getProps() {
|
|
173
|
+
return Promise.resolve('overridden-get-props');
|
|
174
|
+
}
|
|
175
|
+
static getTemplateName() {
|
|
176
|
+
return 'overriden-template-name';
|
|
177
|
+
}
|
|
178
|
+
render() {
|
|
179
|
+
return /*#__PURE__*/_react.default.createElement("div", null, "Mock");
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
const Component = (0, _index.routeComponent)(Mock);
|
|
183
|
+
const l1 = {
|
|
184
|
+
pathname: '/location-one/'
|
|
185
|
+
};
|
|
186
|
+
const l2 = {
|
|
187
|
+
pathname: '/location-two/'
|
|
188
|
+
};
|
|
189
|
+
const l3 = {
|
|
190
|
+
pathname: '/should-get-props/'
|
|
191
|
+
};
|
|
192
|
+
const checks = [Component.shouldGetProps({
|
|
193
|
+
previousLocation: l1,
|
|
194
|
+
location: l1
|
|
195
|
+
}).then(v => expect(v).toBe(false)), Component.shouldGetProps({
|
|
196
|
+
previousLocation: l1,
|
|
197
|
+
location: l2
|
|
198
|
+
}).then(v => expect(v).toBe(false)), Component.shouldGetProps({
|
|
199
|
+
previousLocation: l1,
|
|
200
|
+
location: l3
|
|
201
|
+
}).then(v => expect(v).toBe(true)), Component.getTemplateName().then(v => expect(v).toBe('overriden-template-name')), Component.getProps().then(v => expect(v).toBe('overridden-get-props'))];
|
|
202
|
+
expect.assertions(checks.length);
|
|
203
|
+
return Promise.all(checks);
|
|
204
|
+
});
|
|
205
|
+
test(`Catches and calls onGetPropsError() when getProps throws`, () => {
|
|
206
|
+
const error = 'throwErrorText';
|
|
207
|
+
const Mock = () => /*#__PURE__*/_react.default.createElement("div", null, "Mock");
|
|
208
|
+
Mock.shouldGetProps = trueOnceThenFalse();
|
|
209
|
+
Mock.getProps = () => {
|
|
210
|
+
throw error;
|
|
211
|
+
};
|
|
212
|
+
const Component = (0, _index.routeComponent)(Mock, {}, true);
|
|
213
|
+
return new Promise(resolve => (0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
214
|
+
isHydrating: false,
|
|
215
|
+
onGetPropsError: resolve
|
|
216
|
+
}))).then(caught => expect(caught).toBe(error));
|
|
217
|
+
});
|
|
218
|
+
test(`Catches and calls onGetPropsError() when getProps rejects`, () => {
|
|
219
|
+
const errorText = 'rejectErrorText';
|
|
220
|
+
const Mock = () => /*#__PURE__*/_react.default.createElement("div", null, "Mock");
|
|
221
|
+
Mock.shouldGetProps = trueOnceThenFalse();
|
|
222
|
+
Mock.getProps = () => delay(10).then(() => Promise.reject(errorText));
|
|
223
|
+
const Component = (0, _index.routeComponent)(Mock);
|
|
224
|
+
return new Promise(resolve => (0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
225
|
+
isHydrating: false,
|
|
226
|
+
onGetPropsError: resolve
|
|
227
|
+
}))).then(caught => expect(caught).toBe(errorText));
|
|
228
|
+
});
|
|
229
|
+
test(`Passes props returned from getProps to the wrapped component`, /*#__PURE__*/_asyncToGenerator(function* () {
|
|
230
|
+
const initialProps = {
|
|
231
|
+
foo: 'bar'
|
|
232
|
+
};
|
|
233
|
+
const Mock = props => /*#__PURE__*/_react.default.createElement("div", null, /*#__PURE__*/_react.default.createElement("div", {
|
|
234
|
+
"data-testid": "props"
|
|
235
|
+
}, JSON.stringify(props)));
|
|
236
|
+
Mock.displayName = 'MockComponent';
|
|
237
|
+
Mock.getProps = () => delay(150).then(() => initialProps);
|
|
238
|
+
Mock.shouldGetProps = trueOnceThenFalse();
|
|
239
|
+
const Component = (0, _index.routeComponent)(Mock, {}, true);
|
|
240
|
+
yield new Promise(resolve => {
|
|
241
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
242
|
+
onUpdateComplete: () => {
|
|
243
|
+
resolve();
|
|
244
|
+
}
|
|
245
|
+
}));
|
|
246
|
+
});
|
|
247
|
+
yield (0, _react2.waitFor)(() => {
|
|
248
|
+
expect(_react2.screen.getByTestId('props').innerHTML).toEqual(JSON.stringify({
|
|
249
|
+
foo: 'bar',
|
|
250
|
+
isLoading: false
|
|
251
|
+
}));
|
|
252
|
+
});
|
|
253
|
+
}));
|
|
254
|
+
});
|
|
255
|
+
describe('getRoutes', () => {
|
|
256
|
+
test('wraps components with the routeComponent HOC', () => {
|
|
257
|
+
const mappedRoutes = (0, _index.getRoutes)();
|
|
258
|
+
expect(mappedRoutes).toHaveLength(2);
|
|
259
|
+
const [first, second] = mappedRoutes;
|
|
260
|
+
const expectedName = 'WithErrorHandling(withRouter(routeComponent(Component)))';
|
|
261
|
+
expect(first.component.displayName).toBe(expectedName);
|
|
262
|
+
const expected404Name = 'WithErrorHandling(withRouter(routeComponent(Throw404)))';
|
|
263
|
+
expect(second.component.displayName).toBe(expected404Name);
|
|
264
|
+
});
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* A race condition is created when a user clicks two links for the same
|
|
269
|
+
* component. If the getProps call for the second link resolves before the
|
|
270
|
+
* first, we want to make sure the results of the first are ignored. In this
|
|
271
|
+
* test we rerender returned from first rendering twice to trigger getProps calls and make sure only the
|
|
272
|
+
* second one updates the component.
|
|
273
|
+
*/
|
|
274
|
+
describe('Handles race conditions for getProps', () => {
|
|
275
|
+
test(`unresolved calls to 'getProps' are squashed by new new calls`, /*#__PURE__*/_asyncToGenerator(function* () {
|
|
276
|
+
const renderFunc = jest.fn();
|
|
277
|
+
|
|
278
|
+
// We can't inspect the render directly, so include this mock function
|
|
279
|
+
// in the render and inspect that
|
|
280
|
+
// eslint-disable-next-line react/prop-types
|
|
281
|
+
const MockComponent = ({
|
|
282
|
+
callId
|
|
283
|
+
}) => /*#__PURE__*/_react.default.createElement("p", null, renderFunc(callId));
|
|
284
|
+
// Skip getProps on mount by returning false the first time
|
|
285
|
+
MockComponent.shouldGetProps = falseOnceThenTrue();
|
|
286
|
+
MockComponent.getProps = jest.fn().mockImplementationOnce(() => Promise.resolve({
|
|
287
|
+
callId: 1
|
|
288
|
+
})).mockImplementationOnce(() => Promise.resolve({
|
|
289
|
+
callId: 2
|
|
290
|
+
}));
|
|
291
|
+
const Component = (0, _index.routeComponent)(MockComponent, {}, true);
|
|
292
|
+
let resolver = [];
|
|
293
|
+
let wrapper;
|
|
294
|
+
const onUpdateComplete = () => {
|
|
295
|
+
const r = resolver.pop();
|
|
296
|
+
if (r) {
|
|
297
|
+
r(wrapper);
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
yield new Promise(resolve => {
|
|
301
|
+
resolver.push(resolve);
|
|
302
|
+
wrapper = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
303
|
+
onUpdateComplete: onUpdateComplete
|
|
304
|
+
}));
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
// Update the wrappers props 2 times in succession, this will cause `getProps` to be called
|
|
308
|
+
// twice, but only the later should call `setStateAsync` causing a re-render.
|
|
309
|
+
const p1 = new Promise(resolve => {
|
|
310
|
+
resolver.push(resolve);
|
|
311
|
+
wrapper.rerender( /*#__PURE__*/_react.default.createElement(Component, {
|
|
312
|
+
onUpdateComplete: onUpdateComplete
|
|
313
|
+
}));
|
|
314
|
+
});
|
|
315
|
+
const p2 = new Promise(resolve => {
|
|
316
|
+
resolver.push(resolve);
|
|
317
|
+
wrapper.rerender( /*#__PURE__*/_react.default.createElement(Component, {
|
|
318
|
+
onUpdateComplete: onUpdateComplete
|
|
319
|
+
}));
|
|
320
|
+
});
|
|
321
|
+
yield Promise.all([p1, p2]);
|
|
322
|
+
expect(MockComponent.getProps.mock.calls).toHaveLength(2);
|
|
323
|
+
expect(renderFunc).not.toHaveBeenCalledWith(1);
|
|
324
|
+
expect(renderFunc).toHaveBeenCalledWith(2);
|
|
325
|
+
}));
|
|
326
|
+
});
|
|
327
|
+
describe('Uses preloaded props on initial clientside page load', () => {
|
|
328
|
+
test('Uses preloadedProps when hydrating', /*#__PURE__*/_asyncToGenerator(function* () {
|
|
329
|
+
global.__HYDRATING__ = true;
|
|
330
|
+
const preloadedProps = {
|
|
331
|
+
foo: 'bar'
|
|
332
|
+
};
|
|
333
|
+
const expectedPreloadedChildProps = {
|
|
334
|
+
foo: 'bar',
|
|
335
|
+
isLoading: false
|
|
336
|
+
};
|
|
337
|
+
const Mock = props => /*#__PURE__*/_react.default.createElement("div", {
|
|
338
|
+
"data-testid": "props"
|
|
339
|
+
}, JSON.stringify(props));
|
|
340
|
+
Mock.displayName = 'MockComponent';
|
|
341
|
+
const Component = (0, _index.routeComponent)(Mock, true, {});
|
|
342
|
+
yield new Promise(resolve => {
|
|
343
|
+
const wrapper = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
344
|
+
preloadedProps: preloadedProps,
|
|
345
|
+
onUpdateComplete: () => resolve(wrapper)
|
|
346
|
+
}));
|
|
347
|
+
});
|
|
348
|
+
yield (0, _react2.waitFor)(() => {
|
|
349
|
+
expect(_react2.screen.getByTestId('props').innerHTML).toEqual(JSON.stringify(expectedPreloadedChildProps));
|
|
350
|
+
});
|
|
351
|
+
}));
|
|
352
|
+
test('Does not use preloadedProps when not hydrating', /*#__PURE__*/_asyncToGenerator(function* () {
|
|
353
|
+
global.__HYDRATING__ = false;
|
|
354
|
+
const preloadedProps = {
|
|
355
|
+
foo: 'bar'
|
|
356
|
+
};
|
|
357
|
+
const expectedNotPreloadedChildProps = {
|
|
358
|
+
isLoading: false
|
|
359
|
+
};
|
|
360
|
+
const Mock = props => /*#__PURE__*/_react.default.createElement("div", {
|
|
361
|
+
"data-testid": "props"
|
|
362
|
+
}, JSON.stringify(props));
|
|
363
|
+
Mock.displayName = 'MockComponent';
|
|
364
|
+
const Component = (0, _index.routeComponent)(Mock, true, {});
|
|
365
|
+
yield new Promise(resolve => {
|
|
366
|
+
const wrapper = (0, _react2.render)( /*#__PURE__*/_react.default.createElement(Component, {
|
|
367
|
+
preloadedProps: preloadedProps,
|
|
368
|
+
onUpdateComplete: () => resolve(wrapper)
|
|
369
|
+
}));
|
|
370
|
+
});
|
|
371
|
+
yield (0, _react2.waitFor)(() => {
|
|
372
|
+
expect(_react2.screen.getByTestId('props').innerHTML).toEqual(JSON.stringify(expectedNotPreloadedChildProps));
|
|
373
|
+
});
|
|
374
|
+
}));
|
|
375
|
+
});
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
8
|
+
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
9
|
+
var _reactRouterDom = require("react-router-dom");
|
|
10
|
+
var _appErrorBoundary = _interopRequireDefault(require("../app-error-boundary"));
|
|
11
|
+
var _reactUid = require("react-uid");
|
|
12
|
+
const _excluded = ["component"];
|
|
13
|
+
/*
|
|
14
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
15
|
+
* All rights reserved.
|
|
16
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
17
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
18
|
+
*/
|
|
19
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
20
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
|
|
21
|
+
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
|
|
22
|
+
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
23
|
+
/**
|
|
24
|
+
* The Switch component packages up the bits of rendering that are shared between
|
|
25
|
+
* server and client-side. It's *mostly* a react-router Switch component, hence the
|
|
26
|
+
* name.
|
|
27
|
+
*
|
|
28
|
+
* This is for internal use only.
|
|
29
|
+
*
|
|
30
|
+
* @private
|
|
31
|
+
*/
|
|
32
|
+
const Switch = props => {
|
|
33
|
+
const {
|
|
34
|
+
error,
|
|
35
|
+
appState,
|
|
36
|
+
routes,
|
|
37
|
+
App
|
|
38
|
+
} = props;
|
|
39
|
+
return /*#__PURE__*/_react.default.createElement(_reactUid.UIDReset, null, /*#__PURE__*/_react.default.createElement(_appErrorBoundary.default, {
|
|
40
|
+
error: error
|
|
41
|
+
}, !error && /*#__PURE__*/_react.default.createElement(App, {
|
|
42
|
+
preloadedProps: appState.appProps
|
|
43
|
+
}, /*#__PURE__*/_react.default.createElement(_reactRouterDom.Switch, null, routes.map((route, i) => {
|
|
44
|
+
const {
|
|
45
|
+
component: Component
|
|
46
|
+
} = route,
|
|
47
|
+
routeProps = _objectWithoutProperties(route, _excluded);
|
|
48
|
+
return /*#__PURE__*/_react.default.createElement(_reactRouterDom.Route, _extends({
|
|
49
|
+
key: i
|
|
50
|
+
}, routeProps), /*#__PURE__*/_react.default.createElement(_reactUid.UIDFork, null, /*#__PURE__*/_react.default.createElement(Component, {
|
|
51
|
+
preloadedProps: appState.pageProps
|
|
52
|
+
})));
|
|
53
|
+
})))));
|
|
54
|
+
};
|
|
55
|
+
Switch.propTypes = {
|
|
56
|
+
error: _propTypes.default.object,
|
|
57
|
+
appState: _propTypes.default.object,
|
|
58
|
+
routes: _propTypes.default.array,
|
|
59
|
+
App: _propTypes.default.func,
|
|
60
|
+
preloadedProps: _propTypes.default.object
|
|
61
|
+
};
|
|
62
|
+
var _default = Switch;
|
|
63
|
+
exports.default = _default;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
8
|
+
var errors = _interopRequireWildcard(require("../../errors"));
|
|
9
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
10
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
11
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
12
|
+
/*
|
|
13
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
14
|
+
* All rights reserved.
|
|
15
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
16
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* This is designed to be put into a react-router config as a fallback
|
|
21
|
+
* with path="*", simply to trigger the normal error handling code we have.
|
|
22
|
+
* As a result, this component should never be rendered – it should
|
|
23
|
+
* trigger the error page instead.
|
|
24
|
+
*
|
|
25
|
+
* @private
|
|
26
|
+
*/
|
|
27
|
+
class Throw404 extends _react.default.Component {
|
|
28
|
+
static getProps() {
|
|
29
|
+
throw new errors.HTTPNotFound('Not found');
|
|
30
|
+
}
|
|
31
|
+
render() {
|
|
32
|
+
// This should be unreachable
|
|
33
|
+
return /*#__PURE__*/_react.default.createElement("div", null);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
var _default = Throw404;
|
|
37
|
+
exports.default = _default;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _react = _interopRequireDefault(require("react"));
|
|
4
|
+
var _react2 = require("@testing-library/react");
|
|
5
|
+
var _index = _interopRequireDefault(require("./index"));
|
|
6
|
+
var errors = _interopRequireWildcard(require("../../errors"));
|
|
7
|
+
function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
|
|
8
|
+
function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
/*
|
|
11
|
+
* Copyright (c) 2021, salesforce.com, inc.
|
|
12
|
+
* All rights reserved.
|
|
13
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
14
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
describe('Throw404', () => {
|
|
18
|
+
test('Renders correctly', () => {
|
|
19
|
+
(0, _react2.render)( /*#__PURE__*/_react.default.createElement(_index.default, null));
|
|
20
|
+
const content = document.querySelector('body').firstElementChild.innerHTML;
|
|
21
|
+
expect(content).toBe('<div></div>');
|
|
22
|
+
});
|
|
23
|
+
test('Throws on getProps', () => {
|
|
24
|
+
expect(() => _index.default.getProps()).toThrow(errors.HTTPNotFound);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.withCorrelationId = void 0;
|
|
7
|
+
var _react = _interopRequireDefault(require("react"));
|
|
8
|
+
var _hooks = require("../../hooks");
|
|
9
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
10
|
+
function _objectDestructuringEmpty(obj) { if (obj == null) throw new TypeError("Cannot destructure " + obj); }
|
|
11
|
+
function _extends() { _extends = Object.assign ? Object.assign.bind() : function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); } /*
|
|
12
|
+
* Copyright (c) 2022, salesforce.com, inc.
|
|
13
|
+
* All rights reserved.
|
|
14
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
15
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
16
|
+
*/
|
|
17
|
+
/**
|
|
18
|
+
* An HOC that injects the correlation id to a component
|
|
19
|
+
* @param Component
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
const withCorrelationId = Component => {
|
|
23
|
+
const wrappedComponentName = Component.displayName || Component.name;
|
|
24
|
+
const WrappedComponent = _ref => {
|
|
25
|
+
let passThroughProps = _extends({}, (_objectDestructuringEmpty(_ref), _ref));
|
|
26
|
+
const {
|
|
27
|
+
correlationId
|
|
28
|
+
} = (0, _hooks.useCorrelationId)();
|
|
29
|
+
return /*#__PURE__*/_react.default.createElement(Component, _extends({}, passThroughProps, {
|
|
30
|
+
correlationId: correlationId
|
|
31
|
+
}));
|
|
32
|
+
};
|
|
33
|
+
WrappedComponent.displayName = `withCorrelationId(${wrappedComponentName})`;
|
|
34
|
+
return WrappedComponent;
|
|
35
|
+
};
|
|
36
|
+
exports.withCorrelationId = withCorrelationId;
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.withLegacyGetProps = void 0;
|
|
7
|
+
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
|
|
8
|
+
var _fetchStrategy = require("../fetch-strategy");
|
|
9
|
+
var _react = _interopRequireDefault(require("react"));
|
|
10
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
11
|
+
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
|
|
12
|
+
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; } /*
|
|
13
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
14
|
+
* All rights reserved.
|
|
15
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
16
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
17
|
+
*/
|
|
18
|
+
const withLegacyGetProps = Wrapped => {
|
|
19
|
+
/* istanbul ignore next */
|
|
20
|
+
const wrappedComponentName = Wrapped.displayName || Wrapped.name;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* @private
|
|
24
|
+
*/
|
|
25
|
+
class WithLegacyGetProps extends _fetchStrategy.FetchStrategy {
|
|
26
|
+
render() {
|
|
27
|
+
return /*#__PURE__*/_react.default.createElement(Wrapped, this.props);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @private
|
|
32
|
+
*/
|
|
33
|
+
static doInitAppState({
|
|
34
|
+
App,
|
|
35
|
+
match,
|
|
36
|
+
route,
|
|
37
|
+
req,
|
|
38
|
+
res,
|
|
39
|
+
location
|
|
40
|
+
}) {
|
|
41
|
+
return _asyncToGenerator(function* () {
|
|
42
|
+
const {
|
|
43
|
+
params
|
|
44
|
+
} = match;
|
|
45
|
+
const components = [App, route.component];
|
|
46
|
+
const promises = components.map(c => c.getProps ? c.getProps({
|
|
47
|
+
req,
|
|
48
|
+
res,
|
|
49
|
+
params,
|
|
50
|
+
location
|
|
51
|
+
}) : Promise.resolve({}));
|
|
52
|
+
const [appProps, pageProps] = yield Promise.all(promises);
|
|
53
|
+
return {
|
|
54
|
+
appProps,
|
|
55
|
+
pageProps
|
|
56
|
+
};
|
|
57
|
+
})();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @private
|
|
62
|
+
*/
|
|
63
|
+
static getInitializers() {
|
|
64
|
+
var _Wrapped$getInitializ;
|
|
65
|
+
return [WithLegacyGetProps.doInitAppState, ...(((_Wrapped$getInitializ = Wrapped.getInitializers) === null || _Wrapped$getInitializ === void 0 ? void 0 : _Wrapped$getInitializ.call(Wrapped)) ?? [])];
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @private
|
|
70
|
+
*/
|
|
71
|
+
static getHOCsInUse() {
|
|
72
|
+
var _Wrapped$getHOCsInUse;
|
|
73
|
+
return [withLegacyGetProps, ...(((_Wrapped$getHOCsInUse = Wrapped.getHOCsInUse) === null || _Wrapped$getHOCsInUse === void 0 ? void 0 : _Wrapped$getHOCsInUse.call(Wrapped)) ?? [])];
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
WithLegacyGetProps.displayName = `withLegacyGetProps(${wrappedComponentName})`;
|
|
77
|
+
const exclude = {
|
|
78
|
+
doInitAppState: true,
|
|
79
|
+
getInitializers: true,
|
|
80
|
+
initAppState: true,
|
|
81
|
+
getHOCsInUse: true
|
|
82
|
+
};
|
|
83
|
+
(0, _hoistNonReactStatics.default)(WithLegacyGetProps, Wrapped, exclude);
|
|
84
|
+
return WithLegacyGetProps;
|
|
85
|
+
};
|
|
86
|
+
exports.withLegacyGetProps = withLegacyGetProps;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _index = require("./index");
|
|
4
|
+
var _react = require("@testing-library/react");
|
|
5
|
+
var _react2 = _interopRequireDefault(require("react"));
|
|
6
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
7
|
+
/*
|
|
8
|
+
* Copyright (c) 2022, Salesforce, Inc.
|
|
9
|
+
* All rights reserved.
|
|
10
|
+
* SPDX-License-Identifier: BSD-3-Clause
|
|
11
|
+
* For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
describe('withLegacyGetProps', function () {
|
|
15
|
+
test('Renders correctly', () => {
|
|
16
|
+
const Wrapped = () => /*#__PURE__*/_react2.default.createElement("p", null, "Hello world");
|
|
17
|
+
const Component = (0, _index.withLegacyGetProps)(Wrapped);
|
|
18
|
+
(0, _react.render)( /*#__PURE__*/_react2.default.createElement(Component, {
|
|
19
|
+
locals: {}
|
|
20
|
+
}));
|
|
21
|
+
expect(_react.screen.getByText(/Hello world/i)).toBeInTheDocument();
|
|
22
|
+
});
|
|
23
|
+
test(`Has working getInitializers method`, () => {
|
|
24
|
+
expect((0, _index.withLegacyGetProps)({}).getInitializers()).toHaveLength(1);
|
|
25
|
+
expect((0, _index.withLegacyGetProps)({
|
|
26
|
+
getInitializers: () => ['xyz']
|
|
27
|
+
}).getInitializers()).toHaveLength(2);
|
|
28
|
+
});
|
|
29
|
+
test(`Has working getHOCsInUse method`, () => {
|
|
30
|
+
expect((0, _index.withLegacyGetProps)({}).getHOCsInUse()).toHaveLength(1);
|
|
31
|
+
expect((0, _index.withLegacyGetProps)({
|
|
32
|
+
getHOCsInUse: () => ['xyz']
|
|
33
|
+
}).getHOCsInUse()).toHaveLength(2);
|
|
34
|
+
});
|
|
35
|
+
});
|