@trackunit/react-core-contexts-test 0.1.68 → 0.1.70
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/HookRenderer.cjs +1 -0
- package/HookRenderer.js +1 -0
- package/index.cjs +8 -3
- package/index.js +3 -2
- package/index2.cjs +249 -195
- package/index2.js +232 -186
- package/package.json +2 -1
- package/src/{MockContextProviderBuilder.d.ts → TrackunitProvidersMockBuilder.d.ts} +73 -56
- package/src/debugger.d.ts +19 -7
- package/src/index.d.ts +5 -1
- package/src/mocks/mockCurrentUserContext.d.ts +1 -2
- package/src/mocks/mockUserSubscriptionContext.d.ts +2 -4
- /package/src/mocks/{mockIrisOemAppSDKContext.d.ts → mockOemBrandingContext.d.ts} +0 -0
package/index2.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { jsx } from 'react/jsx-runtime';
|
|
2
|
-
import { useRef, useEffect } from 'react';
|
|
2
|
+
import React, { useRef, useMemo, useEffect } from 'react';
|
|
3
|
+
import { AssetSortByProperty, SortOrder } from '@trackunit/react-core-contexts-api';
|
|
3
4
|
import { act, render } from '@testing-library/react';
|
|
4
|
-
import { AssetSortByProperty, SortOrder, UserSubscriptionPackage } from '@trackunit/react-core-contexts-api';
|
|
5
5
|
import { EnvironmentContextProvider, CurrentUserProvider, AnalyticsContext, UserSubscriptionProvider, OemBrandingContextProvider, TokenProvider, ToastProvider, GlobalSelectionProvider, AssetSortingProvider } from '@trackunit/react-core-hooks';
|
|
6
6
|
import { tw } from '@trackunit/tailwind-styled-components';
|
|
7
|
+
import { omit } from 'lodash';
|
|
7
8
|
import { MemoryRouter } from 'react-router-dom';
|
|
8
9
|
import { ApolloLink } from '@apollo/client';
|
|
9
10
|
import { onError } from '@apollo/client/link/error';
|
|
@@ -16,14 +17,20 @@ import { GraphQLError } from 'graphql';
|
|
|
16
17
|
* This is a hook version of the class component lifecycle method `componentDidUpdate`.
|
|
17
18
|
* ALWAYS wrap in your own object.
|
|
18
19
|
*
|
|
20
|
+
* @param id optional id to use for logging or it will guess the id from the stack trace
|
|
19
21
|
* @param propsToWatch all the props to watch for changes
|
|
22
|
+
* @example
|
|
23
|
+
* const propsToWatch = { foo: props.foo, bar: props.bar };
|
|
24
|
+
* useDebugger({ id: "MyComponent", propsToWatch });
|
|
20
25
|
*/
|
|
21
|
-
const
|
|
22
|
-
var _a;
|
|
26
|
+
const useDebugger = ({ id, propsToWatch, }) => {
|
|
23
27
|
const prevPropsRef = useRef(propsToWatch);
|
|
24
|
-
const
|
|
28
|
+
const uniqueId = useMemo(() => {
|
|
29
|
+
var _a;
|
|
30
|
+
return id || ((_a = new Error().stack) === null || _a === void 0 ? void 0 : _a.split("\n")[2].trim()) || "unknown-id";
|
|
31
|
+
}, [id]);
|
|
25
32
|
useEffect(() => {
|
|
26
|
-
const changedProps = Object.entries(propsToWatch
|
|
33
|
+
const changedProps = Object.entries(propsToWatch || {}).reduce((result, [key, value]) => {
|
|
27
34
|
if (prevPropsRef.current && prevPropsRef.current[key] !== value) {
|
|
28
35
|
result[key + ""] = [prevPropsRef.current[key], value];
|
|
29
36
|
}
|
|
@@ -33,7 +40,7 @@ const useLogPropsChanged = (propsToWatch) => {
|
|
|
33
40
|
// eslint-disable-next-line no-console
|
|
34
41
|
Object.keys(changedProps).forEach(changedProp => {
|
|
35
42
|
// eslint-disable-next-line no-console
|
|
36
|
-
console.log(
|
|
43
|
+
console.log(`${uniqueId} changed property: ${changedProp}`);
|
|
37
44
|
// JSON stringify is used to avoid console.table from logging the object reference
|
|
38
45
|
const result = JSON.parse(JSON.stringify(changedProps[changedProp]));
|
|
39
46
|
const result0 = result[0];
|
|
@@ -53,104 +60,44 @@ const useLogPropsChanged = (propsToWatch) => {
|
|
|
53
60
|
console.dir(changedProps);
|
|
54
61
|
}
|
|
55
62
|
prevPropsRef.current = propsToWatch;
|
|
56
|
-
}, [
|
|
63
|
+
}, [propsToWatch, uniqueId]);
|
|
57
64
|
};
|
|
58
65
|
/**
|
|
59
|
-
*
|
|
60
|
-
*
|
|
66
|
+
* Debugger component for debugging state changes and re-renders.
|
|
67
|
+
*
|
|
68
|
+
* This component will log when it is mounted, re-renders or unmounted.
|
|
61
69
|
* It will also log when any of its props change.
|
|
62
70
|
*
|
|
63
|
-
* @param
|
|
71
|
+
* @param id optional id to use for logging
|
|
72
|
+
* @param stop if true, will stop execution and open debugger
|
|
73
|
+
* @param logPropsChanges optional object with props to watch for changes
|
|
64
74
|
* @param children the children to render
|
|
65
75
|
*/
|
|
66
|
-
const Debugger = ({ logPropsChanges, children }) => {
|
|
67
|
-
|
|
76
|
+
const Debugger = ({ id, logPropsChanges, stop, children, }) => {
|
|
77
|
+
var _a, _b, _c, _d, _e, _f;
|
|
78
|
+
const uniqueId = id ||
|
|
79
|
+
((_e = (_d = (_c = (_b = (_a = React["__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED"]) === null || _a === void 0 ? void 0 : _a.ReactCurrentOwner) === null || _b === void 0 ? void 0 : _b.current) === null || _c === void 0 ? void 0 : _c._debugOwner) === null || _d === void 0 ? void 0 : _d.type) === null || _e === void 0 ? void 0 : _e.name) ||
|
|
80
|
+
((_f = new Error().stack) === null || _f === void 0 ? void 0 : _f.split("\n")[2].trim()) ||
|
|
81
|
+
"unknown-id";
|
|
82
|
+
useDebugger({ id: uniqueId, propsToWatch: logPropsChanges || {} });
|
|
68
83
|
// eslint-disable-next-line no-console
|
|
69
|
-
console.log(
|
|
84
|
+
console.log(`${uniqueId} Debugger is rendering`);
|
|
70
85
|
useEffect(() => {
|
|
71
86
|
// eslint-disable-next-line no-console
|
|
72
|
-
console.log(
|
|
87
|
+
console.log(`${uniqueId} Debugger is mounting`);
|
|
73
88
|
return () => {
|
|
74
89
|
// eslint-disable-next-line no-console
|
|
75
|
-
console.log(
|
|
90
|
+
console.log(`${uniqueId} Debugger is unmounting`);
|
|
76
91
|
};
|
|
92
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
77
93
|
}, []);
|
|
94
|
+
if (stop === true) {
|
|
95
|
+
// eslint-disable-next-line no-debugger
|
|
96
|
+
debugger;
|
|
97
|
+
}
|
|
78
98
|
return jsx("div", Object.assign({ className: "Debugger" }, { children: children }));
|
|
79
99
|
};
|
|
80
100
|
|
|
81
|
-
/******************************************************************************
|
|
82
|
-
Copyright (c) Microsoft Corporation.
|
|
83
|
-
|
|
84
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
|
85
|
-
purpose with or without fee is hereby granted.
|
|
86
|
-
|
|
87
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
88
|
-
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
89
|
-
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
90
|
-
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
91
|
-
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
92
|
-
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
93
|
-
PERFORMANCE OF THIS SOFTWARE.
|
|
94
|
-
***************************************************************************** */
|
|
95
|
-
|
|
96
|
-
function __rest(s, e) {
|
|
97
|
-
var t = {};
|
|
98
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
99
|
-
t[p] = s[p];
|
|
100
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
101
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
102
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
103
|
-
t[p[i]] = s[p[i]];
|
|
104
|
-
}
|
|
105
|
-
return t;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
function __awaiter(thisArg, _arguments, P, generator) {
|
|
109
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
110
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
111
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
112
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
113
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
114
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
const defaultOptions = {
|
|
119
|
-
mutate: {
|
|
120
|
-
errorPolicy: "all",
|
|
121
|
-
},
|
|
122
|
-
query: {
|
|
123
|
-
errorPolicy: "all",
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
/**
|
|
127
|
-
* This is a wrapper around the MockedProvider that logs errors to the console.
|
|
128
|
-
*/
|
|
129
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
130
|
-
function ApolloMockedProviderWithError(props) {
|
|
131
|
-
const { mocks } = props, otherProps = __rest(props, ["mocks"]);
|
|
132
|
-
const mockLink = new MockLink(mocks);
|
|
133
|
-
const errorLoggingLink = onError(({ graphQLErrors, networkError }) => {
|
|
134
|
-
if (graphQLErrors) {
|
|
135
|
-
// eslint-disable-next-line array-callback-return
|
|
136
|
-
graphQLErrors.map(({ message, locations, path }) => {
|
|
137
|
-
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
138
|
-
// eslint-disable-next-line no-console
|
|
139
|
-
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
|
|
140
|
-
}
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
if (networkError) {
|
|
144
|
-
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
145
|
-
// eslint-disable-next-line no-console
|
|
146
|
-
console.log(`[Network error]: ${networkError}`);
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
});
|
|
150
|
-
const link = ApolloLink.from([errorLoggingLink, mockLink]);
|
|
151
|
-
return (jsx(MockedProvider, Object.assign({}, otherProps, { link: link, defaultOptions: Object.assign(Object.assign({}, defaultOptions), { watchQuery: { fetchPolicy: "no-cache" } }) })));
|
|
152
|
-
}
|
|
153
|
-
|
|
154
101
|
/**
|
|
155
102
|
* Do nothing
|
|
156
103
|
*/
|
|
@@ -177,10 +124,26 @@ const mockAssetSortingContext = {
|
|
|
177
124
|
/**
|
|
178
125
|
* Mocks the current user context
|
|
179
126
|
*
|
|
180
|
-
* @param overrides - The overrides to apply to the mock
|
|
181
127
|
* @returns {ICurrentUserContext} - Returns the mocked current user context
|
|
182
128
|
*/
|
|
183
|
-
const mockCurrentUserContext =
|
|
129
|
+
const mockCurrentUserContext = {
|
|
130
|
+
userName: "",
|
|
131
|
+
userRole: "",
|
|
132
|
+
customerId: 12345,
|
|
133
|
+
userId: 154312,
|
|
134
|
+
tasUserId: "751ea227-f199-4d00-925f-a608312c5e45",
|
|
135
|
+
email: "",
|
|
136
|
+
name: "",
|
|
137
|
+
accountId: "",
|
|
138
|
+
assumedUser: null,
|
|
139
|
+
jobTitle: "",
|
|
140
|
+
isAuthenticated: true,
|
|
141
|
+
isVerified: true,
|
|
142
|
+
isTrackunitUser: false,
|
|
143
|
+
isAssuming: false,
|
|
144
|
+
isAccountOwner: true,
|
|
145
|
+
subscriptionPackage: "EXPAND_FLEET_OWNER",
|
|
146
|
+
};
|
|
184
147
|
|
|
185
148
|
const mockEnvironmentContext = {
|
|
186
149
|
auth: {
|
|
@@ -225,19 +188,88 @@ const mockToastContext = {
|
|
|
225
188
|
/**
|
|
226
189
|
* This is a mock for the UserSubscriptionContext.
|
|
227
190
|
*
|
|
228
|
-
* @param features - array of features
|
|
229
|
-
* @param packageType - package type
|
|
230
191
|
* @returns { IUserSubscriptionContext }- mock for the UserSubscriptionContext
|
|
231
192
|
*/
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
packageType,
|
|
238
|
-
};
|
|
193
|
+
const mockUserSubscriptionContext = {
|
|
194
|
+
numberOfDaysWithAccessToHistoricalData: 30,
|
|
195
|
+
numberOfDaysWithAccessToHistoricalInsights: 30,
|
|
196
|
+
features: [],
|
|
197
|
+
packageType: "EXPAND_FLEET_OWNER",
|
|
239
198
|
};
|
|
240
199
|
|
|
200
|
+
/******************************************************************************
|
|
201
|
+
Copyright (c) Microsoft Corporation.
|
|
202
|
+
|
|
203
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
204
|
+
purpose with or without fee is hereby granted.
|
|
205
|
+
|
|
206
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
207
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
208
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
209
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
210
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
211
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
212
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
213
|
+
***************************************************************************** */
|
|
214
|
+
|
|
215
|
+
function __rest(s, e) {
|
|
216
|
+
var t = {};
|
|
217
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
218
|
+
t[p] = s[p];
|
|
219
|
+
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
220
|
+
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
221
|
+
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
222
|
+
t[p[i]] = s[p[i]];
|
|
223
|
+
}
|
|
224
|
+
return t;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
228
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
229
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
230
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
231
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
232
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
233
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
const defaultOptions = {
|
|
238
|
+
mutate: {
|
|
239
|
+
errorPolicy: "all",
|
|
240
|
+
},
|
|
241
|
+
query: {
|
|
242
|
+
errorPolicy: "all",
|
|
243
|
+
},
|
|
244
|
+
};
|
|
245
|
+
/**
|
|
246
|
+
* This is a wrapper around the MockedProvider that logs errors to the console.
|
|
247
|
+
*/
|
|
248
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
249
|
+
function ApolloMockedProviderWithError(props) {
|
|
250
|
+
const { mocks } = props, otherProps = __rest(props, ["mocks"]);
|
|
251
|
+
const mockLink = new MockLink(mocks);
|
|
252
|
+
const errorLoggingLink = onError(({ graphQLErrors, networkError }) => {
|
|
253
|
+
if (graphQLErrors) {
|
|
254
|
+
// eslint-disable-next-line array-callback-return
|
|
255
|
+
graphQLErrors.map(({ message, locations, path }) => {
|
|
256
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
257
|
+
// eslint-disable-next-line no-console
|
|
258
|
+
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
if (networkError) {
|
|
263
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
264
|
+
// eslint-disable-next-line no-console
|
|
265
|
+
console.log(`[Network error]: ${networkError}`);
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
});
|
|
269
|
+
const link = ApolloLink.from([errorLoggingLink, mockLink]);
|
|
270
|
+
return (jsx(MockedProvider, Object.assign({}, otherProps, { link: link, defaultOptions: Object.assign(Object.assign({}, defaultOptions), { watchQuery: { fetchPolicy: "no-cache" } }) })));
|
|
271
|
+
}
|
|
272
|
+
|
|
241
273
|
/**
|
|
242
274
|
* Flushes all promises in the queue.
|
|
243
275
|
* This is useful when testing async code.
|
|
@@ -280,133 +312,147 @@ const flushPromisesInAct = (waitTimeInMS = 0) => {
|
|
|
280
312
|
};
|
|
281
313
|
|
|
282
314
|
/**
|
|
283
|
-
* This builder allows you to enable providers using the builder pattern, and then call 1 of either:
|
|
315
|
+
* This builder allows you to enable trackunit providers using the builder pattern, and then call 1 of either:
|
|
316
|
+
* For React Components:
|
|
284
317
|
* - render
|
|
285
|
-
*
|
|
318
|
+
* For React Hooks:
|
|
319
|
+
* - renderHook
|
|
320
|
+
* For Storybook:
|
|
321
|
+
* - storybook
|
|
286
322
|
*/
|
|
287
|
-
class
|
|
323
|
+
class TrackunitProvidersMockBuilder {
|
|
288
324
|
constructor() {
|
|
289
325
|
this.selectedEnvironmentContext = mockEnvironmentContext;
|
|
290
326
|
this.selectedTokenContext = { token: "fakeToken" };
|
|
291
|
-
this.userSubscriptionPackageType = UserSubscriptionPackage.INSIGHT;
|
|
292
|
-
this.features = [];
|
|
293
327
|
this.selectedApolloMocks = [];
|
|
294
328
|
this.selectedRouterProps = {};
|
|
295
329
|
this.selectedToastContext = mockToastContext;
|
|
296
330
|
this.selectedGlobalSelection = { selection: null };
|
|
297
331
|
this.selectedAssetSortingContext = mockAssetSortingContext;
|
|
298
|
-
this.selectedCurrentUserContext = mockCurrentUserContext
|
|
332
|
+
this.selectedCurrentUserContext = mockCurrentUserContext;
|
|
299
333
|
this.selectedAnalyticsContext = mockAnalyticsContext;
|
|
300
334
|
this.selectedOemBrandingContext = mockOemBrandingContext;
|
|
335
|
+
this.selectedUserSubscriptionContext = mockUserSubscriptionContext;
|
|
301
336
|
}
|
|
302
337
|
/**
|
|
303
338
|
* Use this Analytics Context.
|
|
339
|
+
* Defaults to mockAnalyticsContext.
|
|
304
340
|
*
|
|
341
|
+
* @see mockAnalyticsContext
|
|
305
342
|
* @param analyticsContext - The analytics context to use.
|
|
306
|
-
* @returns {
|
|
343
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
307
344
|
*/
|
|
345
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
308
346
|
analytics(analyticsContext) {
|
|
309
|
-
this.selectedAnalyticsContext = analyticsContext;
|
|
347
|
+
this.selectedAnalyticsContext = Object.assign(Object.assign({}, mockAnalyticsContext), analyticsContext);
|
|
310
348
|
return this;
|
|
311
349
|
}
|
|
312
350
|
/**
|
|
313
351
|
* Use this Environment Context.
|
|
352
|
+
* Defaults to mockEnvironmentContext.
|
|
314
353
|
*
|
|
354
|
+
* @see mockEnvironmentContext
|
|
315
355
|
* @param environmentContext - The environment context to use.
|
|
316
|
-
* @returns {
|
|
356
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
317
357
|
*/
|
|
318
358
|
environment(environmentContext) {
|
|
319
|
-
this.selectedEnvironmentContext =
|
|
359
|
+
this.selectedEnvironmentContext = Object.assign(Object.assign({}, mockEnvironmentContext), environmentContext);
|
|
320
360
|
return this;
|
|
321
361
|
}
|
|
322
362
|
/**
|
|
323
|
-
* Use this
|
|
363
|
+
* Use this to pass in a differerent current user.
|
|
364
|
+
* Defaults to mockCurrentUserContext.
|
|
324
365
|
*
|
|
325
|
-
* @
|
|
326
|
-
* @returns {
|
|
366
|
+
* @see mockCurrentUserContext
|
|
367
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
327
368
|
*/
|
|
328
|
-
|
|
329
|
-
this.
|
|
369
|
+
currentUser(currentUserContext) {
|
|
370
|
+
this.selectedCurrentUserContext = Object.assign(Object.assign({}, mockCurrentUserContext), currentUserContext);
|
|
330
371
|
return this;
|
|
331
372
|
}
|
|
332
373
|
/**
|
|
333
|
-
*
|
|
374
|
+
* Use this to pass in a differerent current user subscription.
|
|
375
|
+
* Defaults to mockUserSubscriptionContext.
|
|
334
376
|
*
|
|
335
|
-
* @
|
|
377
|
+
* @see mockUserSubscriptionContext
|
|
378
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
336
379
|
*/
|
|
337
|
-
|
|
338
|
-
|
|
380
|
+
userSubscription(userSubscription) {
|
|
381
|
+
var _a;
|
|
382
|
+
//TODO DONT SUPPORT THE WIERD WAY OF PASSING FEATURES
|
|
383
|
+
const featuresConverted = ((_a = userSubscription === null || userSubscription === void 0 ? void 0 : userSubscription.features) === null || _a === void 0 ? void 0 : _a.map(f => {
|
|
384
|
+
if (typeof f === "string") {
|
|
385
|
+
return { id: f, name: f };
|
|
386
|
+
}
|
|
387
|
+
return f;
|
|
388
|
+
})) || [];
|
|
389
|
+
this.selectedUserSubscriptionContext = Object.assign(Object.assign(Object.assign({}, mockUserSubscriptionContext), omit(userSubscription, "features")), { features: [...(mockUserSubscriptionContext.features || []), ...featuresConverted] });
|
|
339
390
|
return this;
|
|
340
391
|
}
|
|
341
392
|
/**
|
|
342
|
-
*
|
|
393
|
+
* Set global asset sorting context.
|
|
394
|
+
* Defaults to mockAssetSortingContext.
|
|
343
395
|
*
|
|
344
|
-
* @
|
|
345
|
-
* @
|
|
396
|
+
* @see mockAssetSortingContext
|
|
397
|
+
* @param assetSortingContext - Override the default context.
|
|
398
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
346
399
|
*/
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
this.userSubscriptionPackageType = userSubscriptionPackage;
|
|
350
|
-
}
|
|
400
|
+
assetSorting(assetSortingContext) {
|
|
401
|
+
this.selectedAssetSortingContext = Object.assign(Object.assign({}, mockAssetSortingContext), assetSortingContext);
|
|
351
402
|
return this;
|
|
352
403
|
}
|
|
353
404
|
/**
|
|
354
|
-
*
|
|
405
|
+
* Set OEM Branding context.
|
|
406
|
+
* Defaults to mockOemBrandingContext.
|
|
355
407
|
*
|
|
356
|
-
* @
|
|
357
|
-
* @
|
|
408
|
+
* @see mockOemBrandingContext
|
|
409
|
+
* @param oemBrandingContext - Override the default context.
|
|
358
410
|
*/
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
this.features = features.map(feature => {
|
|
362
|
-
return {
|
|
363
|
-
id: feature,
|
|
364
|
-
name: feature,
|
|
365
|
-
};
|
|
366
|
-
});
|
|
367
|
-
}
|
|
411
|
+
oemBrandingContext(oemBrandingContext) {
|
|
412
|
+
this.selectedOemBrandingContext = Object.assign(Object.assign({}, mockOemBrandingContext), oemBrandingContext);
|
|
368
413
|
return this;
|
|
369
414
|
}
|
|
370
415
|
/**
|
|
371
|
-
* Use this
|
|
416
|
+
* Use this ToastContext with the given mocks.
|
|
417
|
+
* Defaults to mockToastContext.
|
|
372
418
|
*
|
|
373
|
-
* @
|
|
374
|
-
* @
|
|
419
|
+
* @see mockToastContext
|
|
420
|
+
* @param toastContext - Override the default toast context.
|
|
375
421
|
*/
|
|
376
|
-
|
|
377
|
-
this.
|
|
422
|
+
toast(toastContext) {
|
|
423
|
+
this.selectedToastContext = Object.assign(Object.assign({}, mockToastContext), toastContext);
|
|
378
424
|
return this;
|
|
379
425
|
}
|
|
380
426
|
/**
|
|
381
427
|
* Use this global selection.
|
|
428
|
+
* Defaults to null.
|
|
382
429
|
*
|
|
383
430
|
* @param globalSelection - The global selection to use.
|
|
384
|
-
* @returns {
|
|
431
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
385
432
|
*/
|
|
386
433
|
globalSelection(globalSelection) {
|
|
387
434
|
this.selectedGlobalSelection = { selection: globalSelection };
|
|
388
435
|
return this;
|
|
389
436
|
}
|
|
390
437
|
/**
|
|
391
|
-
*
|
|
438
|
+
* Use this token.
|
|
392
439
|
*
|
|
393
|
-
* @param
|
|
394
|
-
* @returns {
|
|
440
|
+
* @param token - The token to use.
|
|
441
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
395
442
|
*/
|
|
396
|
-
|
|
397
|
-
this.
|
|
443
|
+
token(token) {
|
|
444
|
+
this.selectedTokenContext = { token };
|
|
398
445
|
return this;
|
|
399
446
|
}
|
|
400
447
|
/**
|
|
401
|
-
*
|
|
448
|
+
* Use this Router Props with the given mocks.
|
|
449
|
+
*
|
|
450
|
+
* @param routerProps - The router props to use.
|
|
451
|
+
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
402
452
|
*/
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
// This ensures correct act loading when using hooks
|
|
407
|
-
const hookRenderer = yield import('./HookRenderer.js');
|
|
408
|
-
return hookRenderer.reactHooksRenderHook(callback, children => this.getMockedCompositionRoot(parentElement ? parentElement(children) : children));
|
|
409
|
-
});
|
|
453
|
+
routerProps(routerProps) {
|
|
454
|
+
this.selectedRouterProps = routerProps;
|
|
455
|
+
return this;
|
|
410
456
|
}
|
|
411
457
|
/**
|
|
412
458
|
* Use this Manager Apollo Context Provider with the given mocks.
|
|
@@ -418,26 +464,38 @@ class MockContextProviderBuilder {
|
|
|
418
464
|
return this;
|
|
419
465
|
}
|
|
420
466
|
/**
|
|
421
|
-
*
|
|
467
|
+
* Validate the mocks that has been supplied to make sure they make sense.
|
|
468
|
+
* Note: This function is overriden in builders extending this one.
|
|
422
469
|
*
|
|
423
|
-
* @
|
|
470
|
+
* @returns {boolean} true or throws error if any invalid mocks
|
|
424
471
|
*/
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
return this;
|
|
472
|
+
validateSuppliedMocks() {
|
|
473
|
+
return true;
|
|
428
474
|
}
|
|
429
475
|
/**
|
|
430
|
-
*
|
|
476
|
+
* Make sure this represent the same structure as the main index.tsx does.
|
|
431
477
|
*
|
|
432
|
-
* @param
|
|
478
|
+
* @param testChildren - the child element being tested.
|
|
479
|
+
* @param addTestRootContainer - if you want to add a root container to the test.
|
|
433
480
|
*/
|
|
434
|
-
|
|
435
|
-
this.selectedOemBrandingContext
|
|
436
|
-
|
|
481
|
+
getMockedCompositionRoot(testChildren, addTestRootContainer = true) {
|
|
482
|
+
return (jsx(EnvironmentContextProvider, Object.assign({ value: this.selectedEnvironmentContext }, { children: jsx(MemoryRouter, Object.assign({}, this.selectedRouterProps, { children: jsx(CurrentUserProvider, Object.assign({ value: this.selectedCurrentUserContext }, { children: jsx(AnalyticsContext.Provider, Object.assign({ value: this.selectedAnalyticsContext }, { children: jsx(UserSubscriptionProvider, Object.assign({ value: this.selectedUserSubscriptionContext }, { children: jsx(OemBrandingContextProvider, Object.assign({ value: this.selectedOemBrandingContext }, { children: jsx(TokenProvider, Object.assign({ value: this.selectedTokenContext }, { children: jsx(ToastProvider, Object.assign({ value: this.selectedToastContext }, { children: jsx(GlobalSelectionProvider, Object.assign({ value: this.selectedGlobalSelection }, { children: jsx(AssetSortingProvider, Object.assign({ value: this.selectedAssetSortingContext }, { children: jsx(ApolloMockedProviderWithError, Object.assign({ mocks: this.selectedApolloMocks, addTypename: false }, { children: addTestRootContainer ? (jsx(TestRoot, Object.assign({ "data-testid": "testRoot" }, { children: testChildren }))) : (jsx("div", { children: testChildren })) })) })) })) })) })) })) })) })) })) })) })));
|
|
483
|
+
}
|
|
484
|
+
/**
|
|
485
|
+
* This will return the mocked composition root.
|
|
486
|
+
*/
|
|
487
|
+
renderHook(callback, parentElement) {
|
|
488
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
489
|
+
this.validateSuppliedMocks();
|
|
490
|
+
// This ensures correct act loading when using hooks and not loaded if this build is used for storybook
|
|
491
|
+
const hookRenderer = yield import('./HookRenderer.js');
|
|
492
|
+
return hookRenderer.reactHooksRenderHook(callback, children => this.getMockedCompositionRoot(parentElement ? parentElement(children) : children));
|
|
493
|
+
});
|
|
437
494
|
}
|
|
438
495
|
/**
|
|
439
|
-
* This will use
|
|
496
|
+
* This will use react-testing-library.render the child in the correct mocked hierarchy of context providers.
|
|
440
497
|
*
|
|
498
|
+
* @see https://testing-library.com/docs/react-testing-library/api#render
|
|
441
499
|
* @param child - the child element being tested.
|
|
442
500
|
*/
|
|
443
501
|
render(child) {
|
|
@@ -460,28 +518,16 @@ class MockContextProviderBuilder {
|
|
|
460
518
|
});
|
|
461
519
|
}
|
|
462
520
|
/**
|
|
463
|
-
*
|
|
464
|
-
* Note: This function is overriden in builders extending this one.
|
|
465
|
-
*
|
|
466
|
-
* @returns {boolean} true or throws error if any invalid mocks
|
|
467
|
-
*/
|
|
468
|
-
validateSuppliedMocks() {
|
|
469
|
-
return true;
|
|
470
|
-
}
|
|
471
|
-
/**
|
|
472
|
-
* Make sure this represent the same structure as the main index.tsx does.
|
|
473
|
-
*
|
|
474
|
-
* @param testChildren - the child element being tested.
|
|
475
|
-
* @param addTestRootContainer - if you want to add a root container to the test.
|
|
521
|
+
* This will return the children in the correct mocked hierarchy of context providers.
|
|
476
522
|
*/
|
|
477
|
-
|
|
478
|
-
return
|
|
523
|
+
storybook(child) {
|
|
524
|
+
return this.getMockedCompositionRoot(child, false);
|
|
479
525
|
}
|
|
480
526
|
}
|
|
481
527
|
/**
|
|
482
|
-
*
|
|
528
|
+
* This is the default mock builder for the TrackunitProviders.
|
|
483
529
|
*/
|
|
484
|
-
const trackunitProviders = () => new
|
|
530
|
+
const trackunitProviders = () => new TrackunitProvidersMockBuilder();
|
|
485
531
|
const TestRoot = tw.div `
|
|
486
532
|
--tw-scale-x: 0.99;
|
|
487
533
|
--tw-scale-y: 0.99;
|
|
@@ -569,4 +615,4 @@ const validateIrisApp = (irisApp) => __awaiter(void 0, void 0, void 0, function*
|
|
|
569
615
|
return null;
|
|
570
616
|
});
|
|
571
617
|
|
|
572
|
-
export { Debugger as D,
|
|
618
|
+
export { Debugger as D, TrackunitProvidersMockBuilder as T, __awaiter as _, mockAssetSortingContext as a, mockCurrentUserContext as b, mockEnvironmentContext as c, mockOemBrandingContext as d, mockToastContext as e, flushPromises as f, mockUserSubscriptionContext as g, doNothing as h, flushPromisesInAct as i, mockAnalyticsContext as m, queryFor as q, trackunitProviders as t, useDebugger as u, validateIrisApp as v };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-core-contexts-test",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.70",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
"@trackunit/react-core-hooks": "0.2.60",
|
|
14
14
|
"@trackunit/tailwind-styled-components": "0.0.56",
|
|
15
15
|
"graphql": "15.8.0",
|
|
16
|
+
"lodash": "4.17.21",
|
|
16
17
|
"react": "18.2.0",
|
|
17
18
|
"react-router-dom": "6.4.5"
|
|
18
19
|
},
|