@trackunit/react-core-contexts-test 0.1.38-alpha-beca370860.0 → 0.1.42
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 +8 -0
- package/HookRenderer.js +8 -0
- package/index2.cjs +53 -14
- package/index2.js +53 -14
- package/package.json +4 -4
- package/src/ApolloMockedProviderWithError.d.ts +3 -0
- package/src/HookRenderer.d.ts +8 -0
- package/src/MockContextProviderBuilder.d.ts +14 -8
- package/src/mocks/mockCurrentUserContext.d.ts +6 -0
- package/src/mocks/mockUserSubscriptionContext.d.ts +7 -0
- package/src/utils/doNothing.d.ts +3 -0
- package/src/utils/queryFor.d.ts +1 -1
- package/src/utils/validateIrisApp.d.ts +2 -2
- package/src/utils/wait.d.ts +14 -0
package/HookRenderer.cjs
CHANGED
|
@@ -13,6 +13,14 @@ require('@apollo/client/link/error');
|
|
|
13
13
|
require('@apollo/client/testing');
|
|
14
14
|
require('graphql');
|
|
15
15
|
|
|
16
|
+
/**
|
|
17
|
+
* This is a wrapper around the react testing library renderHook function.
|
|
18
|
+
* It will make sure that the act function is called and that the promises are flushed.
|
|
19
|
+
*
|
|
20
|
+
* @param callback The callback to render the hook.
|
|
21
|
+
* @param getMockedCompositionRoot A function that will return the mocked composition root.
|
|
22
|
+
* @returns The rendered hook.
|
|
23
|
+
*/
|
|
16
24
|
const reactHooksRenderHook = (callback, getMockedCompositionRoot) => index.__awaiter(void 0, void 0, void 0, function* () {
|
|
17
25
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
18
26
|
const wrapper = ({ children }) => {
|
package/HookRenderer.js
CHANGED
|
@@ -11,6 +11,14 @@ import '@apollo/client/link/error';
|
|
|
11
11
|
import '@apollo/client/testing';
|
|
12
12
|
import 'graphql';
|
|
13
13
|
|
|
14
|
+
/**
|
|
15
|
+
* This is a wrapper around the react testing library renderHook function.
|
|
16
|
+
* It will make sure that the act function is called and that the promises are flushed.
|
|
17
|
+
*
|
|
18
|
+
* @param callback The callback to render the hook.
|
|
19
|
+
* @param getMockedCompositionRoot A function that will return the mocked composition root.
|
|
20
|
+
* @returns The rendered hook.
|
|
21
|
+
*/
|
|
14
22
|
const reactHooksRenderHook = (callback, getMockedCompositionRoot) => __awaiter(void 0, void 0, void 0, function* () {
|
|
15
23
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
16
24
|
const wrapper = ({ children }) => {
|
package/index2.cjs
CHANGED
|
@@ -56,6 +56,9 @@ const defaultOptions = {
|
|
|
56
56
|
errorPolicy: "all",
|
|
57
57
|
},
|
|
58
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* This is a wrapper around the MockedProvider that logs errors to the console.
|
|
61
|
+
*/
|
|
59
62
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
60
63
|
function ApolloMockedProviderWithError(props) {
|
|
61
64
|
const { mocks } = props, otherProps = __rest(props, ["mocks"]);
|
|
@@ -64,14 +67,14 @@ function ApolloMockedProviderWithError(props) {
|
|
|
64
67
|
if (graphQLErrors) {
|
|
65
68
|
// eslint-disable-next-line array-callback-return
|
|
66
69
|
graphQLErrors.map(({ message, locations, path }) => {
|
|
67
|
-
if (process.env
|
|
70
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
68
71
|
// eslint-disable-next-line no-console
|
|
69
72
|
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
|
|
70
73
|
}
|
|
71
74
|
});
|
|
72
75
|
}
|
|
73
76
|
if (networkError) {
|
|
74
|
-
if (process.env
|
|
77
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
75
78
|
// eslint-disable-next-line no-console
|
|
76
79
|
console.log(`[Network error]: ${networkError}`);
|
|
77
80
|
}
|
|
@@ -81,6 +84,9 @@ function ApolloMockedProviderWithError(props) {
|
|
|
81
84
|
return (jsxRuntime.jsx(testing.MockedProvider, Object.assign({}, otherProps, { link: link, defaultOptions: Object.assign(Object.assign({}, defaultOptions), { watchQuery: { fetchPolicy: "no-cache" } }) })));
|
|
82
85
|
}
|
|
83
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Do nothing
|
|
89
|
+
*/
|
|
84
90
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
91
|
const doNothing = () => {
|
|
86
92
|
/* Do nothing */
|
|
@@ -94,6 +100,12 @@ const mockAssetSortingContext = {
|
|
|
94
100
|
},
|
|
95
101
|
};
|
|
96
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Mocks the current user context
|
|
105
|
+
*
|
|
106
|
+
* @param overrides - The overrides to apply to the mock
|
|
107
|
+
* @returns {ICurrentUserContext} - Returns the mocked current user context
|
|
108
|
+
*/
|
|
97
109
|
const mockCurrentUserContext = (overrides = {}) => (Object.assign({ userName: "", userRole: "", customerId: 12345, userId: 154312, tasUserId: "751ea227-f199-4d00-925f-a608312c5e45", email: "", name: "", accountId: "", assumedUser: null, jobTitle: "", isAuthenticated: true, isVerified: true, isAccountOwner: true, subscriptionPackage: "EXPAND_FLEET_OWNER" }, overrides));
|
|
98
110
|
|
|
99
111
|
const mockEnvironmentContext = {
|
|
@@ -131,6 +143,13 @@ const mockToastContext = {
|
|
|
131
143
|
setIsManifestError: doNothing,
|
|
132
144
|
};
|
|
133
145
|
|
|
146
|
+
/**
|
|
147
|
+
* This is a mock for the UserSubscriptionContext.
|
|
148
|
+
*
|
|
149
|
+
* @param features - array of features
|
|
150
|
+
* @param packageType - package type
|
|
151
|
+
* @returns IUserSubscriptionContext
|
|
152
|
+
*/
|
|
134
153
|
const mockUserSubscriptionProviderValue = (features, packageType) => {
|
|
135
154
|
return {
|
|
136
155
|
numberOfDaysWithAccessToHistoricalData: 30,
|
|
@@ -140,6 +159,13 @@ const mockUserSubscriptionProviderValue = (features, packageType) => {
|
|
|
140
159
|
};
|
|
141
160
|
};
|
|
142
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Flushes all promises in the queue.
|
|
164
|
+
* This is useful when testing async code.
|
|
165
|
+
*
|
|
166
|
+
* @param waitTimeInMS - The amount of time to wait before resolving the promise.
|
|
167
|
+
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
168
|
+
*/
|
|
143
169
|
const flushPromises = (waitTimeInMS = 0) => {
|
|
144
170
|
return new Promise(resolve => {
|
|
145
171
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -152,6 +178,13 @@ const flushPromises = (waitTimeInMS = 0) => {
|
|
|
152
178
|
}
|
|
153
179
|
});
|
|
154
180
|
};
|
|
181
|
+
/**
|
|
182
|
+
* Flushes all promises in the queue.
|
|
183
|
+
* This is useful when testing async code.
|
|
184
|
+
*
|
|
185
|
+
* @param waitTimeInMS - The amount of time to wait before resolving the promise.
|
|
186
|
+
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
187
|
+
*/
|
|
155
188
|
const flushPromisesInAct = (waitTimeInMS = 0) => {
|
|
156
189
|
return react.act(() => {
|
|
157
190
|
return new Promise(resolve => {
|
|
@@ -188,7 +221,7 @@ class MockContextProviderBuilder {
|
|
|
188
221
|
/**
|
|
189
222
|
* Use this Environment Context.
|
|
190
223
|
*
|
|
191
|
-
* @param environmentContext
|
|
224
|
+
* @param environmentContext - The environment context to use.
|
|
192
225
|
*/
|
|
193
226
|
environment(environmentContext) {
|
|
194
227
|
this.selectedEnvironmentContext = environmentContext || mockEnvironmentContext;
|
|
@@ -197,7 +230,7 @@ class MockContextProviderBuilder {
|
|
|
197
230
|
/**
|
|
198
231
|
* Use this token.
|
|
199
232
|
*
|
|
200
|
-
* @param token
|
|
233
|
+
* @param token - The token to use.
|
|
201
234
|
*/
|
|
202
235
|
token(token) {
|
|
203
236
|
this.selectedTokenContext = { token };
|
|
@@ -206,7 +239,7 @@ class MockContextProviderBuilder {
|
|
|
206
239
|
/**
|
|
207
240
|
* Use this to pass in userSubscriptionPackage.
|
|
208
241
|
*
|
|
209
|
-
* @param developerSettingsContext
|
|
242
|
+
* @param developerSettingsContext - The developer settings context to use.
|
|
210
243
|
*/
|
|
211
244
|
userSubscriptionPackage(userSubscriptionPackage) {
|
|
212
245
|
if (userSubscriptionPackage) {
|
|
@@ -217,7 +250,7 @@ class MockContextProviderBuilder {
|
|
|
217
250
|
/**
|
|
218
251
|
* Use this to pass in SupportedFeatures.
|
|
219
252
|
*
|
|
220
|
-
* @param developerSettingsContext
|
|
253
|
+
* @param developerSettingsContext - The developer settings context to use.
|
|
221
254
|
*/
|
|
222
255
|
supportedFeatures(features) {
|
|
223
256
|
if (features) {
|
|
@@ -233,7 +266,7 @@ class MockContextProviderBuilder {
|
|
|
233
266
|
/**
|
|
234
267
|
* Assume this user
|
|
235
268
|
*
|
|
236
|
-
* @param assumeUser
|
|
269
|
+
* @param assumeUser - The user to assume.
|
|
237
270
|
*/
|
|
238
271
|
assumeUser(assumeUser) {
|
|
239
272
|
this.selectedAssumedUser = assumeUser;
|
|
@@ -242,7 +275,7 @@ class MockContextProviderBuilder {
|
|
|
242
275
|
/**
|
|
243
276
|
* Use this Router Props with the given mocks.
|
|
244
277
|
*
|
|
245
|
-
* @param routerProps
|
|
278
|
+
* @param routerProps - The router props to use.
|
|
246
279
|
*/
|
|
247
280
|
routerProps(routerProps) {
|
|
248
281
|
this.selectedRouterProps = routerProps || {};
|
|
@@ -251,7 +284,7 @@ class MockContextProviderBuilder {
|
|
|
251
284
|
/**
|
|
252
285
|
* Use this global selection.
|
|
253
286
|
*
|
|
254
|
-
* @param globalSelection
|
|
287
|
+
* @param globalSelection - The global selection to use.
|
|
255
288
|
*/
|
|
256
289
|
globalSelection(globalSelection) {
|
|
257
290
|
this.selectedGlobalSelection = { selection: globalSelection };
|
|
@@ -266,6 +299,9 @@ class MockContextProviderBuilder {
|
|
|
266
299
|
this.selectedAssetSortingContext = Object.assign(Object.assign({}, mockAssetSortingContext), overrides);
|
|
267
300
|
return this;
|
|
268
301
|
}
|
|
302
|
+
/**
|
|
303
|
+
* This will return the mocked composition root.
|
|
304
|
+
*/
|
|
269
305
|
renderHook(callback, parentElement) {
|
|
270
306
|
return __awaiter(this, void 0, void 0, function* () {
|
|
271
307
|
this.validateSuppliedMocks();
|
|
@@ -277,7 +313,7 @@ class MockContextProviderBuilder {
|
|
|
277
313
|
/**
|
|
278
314
|
* Use this Manager Apollo Context Provider with the given mocks.
|
|
279
315
|
*
|
|
280
|
-
* @param apolloMocks
|
|
316
|
+
* @param apolloMocks - The mocks to use for the ApolloProvider.
|
|
281
317
|
*/
|
|
282
318
|
apollo(apolloMocks) {
|
|
283
319
|
this.selectedApolloMocks = apolloMocks || [];
|
|
@@ -335,6 +371,9 @@ class MockContextProviderBuilder {
|
|
|
335
371
|
return (jsxRuntime.jsx(reactCoreHooks.EnvironmentContextProvider, Object.assign({ value: this.selectedEnvironmentContext }, { children: jsxRuntime.jsx(reactRouterDom.MemoryRouter, Object.assign({}, this.selectedRouterProps, { children: jsxRuntime.jsx(reactCoreHooks.CurrentUserProvider, Object.assign({ value: mockCurrentUserContext({ assumedUser: this.selectedAssumedUser }) }, { children: jsxRuntime.jsx(reactCoreHooks.UserSubscriptionProvider, Object.assign({ value: mockUserSubscriptionProviderValue(this.features, this.userSubscriptionPackageType) }, { children: jsxRuntime.jsx(reactCoreHooks.TokenProvider, Object.assign({ value: this.selectedTokenContext }, { children: jsxRuntime.jsx(reactCoreHooks.ToastProvider, Object.assign({ value: this.selectedToastContext }, { children: jsxRuntime.jsx(reactCoreHooks.GlobalSelectionProvider, Object.assign({ value: this.selectedGlobalSelection }, { children: jsxRuntime.jsx(reactCoreHooks.AssetSortingProvider, Object.assign({ value: this.selectedAssetSortingContext }, { children: jsxRuntime.jsx(ApolloMockedProviderWithError, Object.assign({ mocks: this.selectedApolloMocks, addTypename: false }, { children: addTestRootContainer ? (jsxRuntime.jsx(TestRoot, Object.assign({ "data-testid": "testRoot" }, { children: testChildren }))) : (jsxRuntime.jsx("div", { children: testChildren })) })) })) })) })) })) })) })) })) })));
|
|
336
372
|
}
|
|
337
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
*
|
|
376
|
+
*/
|
|
338
377
|
const rootContext = () => new MockContextProviderBuilder();
|
|
339
378
|
const TestRoot = tailwindStyledComponents.tw.div `
|
|
340
379
|
--tw-scale-x: 0.99;
|
|
@@ -353,7 +392,7 @@ const TestRoot = tailwindStyledComponents.tw.div `
|
|
|
353
392
|
* Note that *all* properties should be given a value, use `null` in place of `undefined`,
|
|
354
393
|
* otherwise nothing will be returned.
|
|
355
394
|
* @param error ApolloError object to be returned.
|
|
356
|
-
* @returns MockedResponse that can be passed to the mocked ApolloProvider.
|
|
395
|
+
* @returns {MockedResponse} that can be passed to the mocked ApolloProvider.
|
|
357
396
|
* @see [Testing React components using MockedProvider and associated APIs](https://www.apollographql.com/docs/react/development-testing/testing/)
|
|
358
397
|
*/
|
|
359
398
|
const queryFor = (document, variables, data, error) => {
|
|
@@ -363,7 +402,7 @@ const queryFor = (document, variables, data, error) => {
|
|
|
363
402
|
variables,
|
|
364
403
|
},
|
|
365
404
|
newData: () => {
|
|
366
|
-
if (process.env
|
|
405
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
367
406
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
368
407
|
const name = document.definitions[0].name.value;
|
|
369
408
|
// eslint-disable-next-line no-console
|
|
@@ -394,6 +433,7 @@ const queryFor = (document, variables, data, error) => {
|
|
|
394
433
|
* According to the single spa spec.
|
|
395
434
|
*
|
|
396
435
|
* Your test could look like this
|
|
436
|
+
*
|
|
397
437
|
* @example
|
|
398
438
|
* import * as IrisApp from "./index";
|
|
399
439
|
describe("App", () => {
|
|
@@ -402,9 +442,8 @@ const queryFor = (document, variables, data, error) => {
|
|
|
402
442
|
expect(result).toBeNull();
|
|
403
443
|
});
|
|
404
444
|
});
|
|
405
|
-
*
|
|
406
445
|
* @param irisApp Import the index ts(x) file as above and pass it in.
|
|
407
|
-
* @returns null if everything is good - otherwise a string telling you what is wrong.
|
|
446
|
+
* @returns {null|string} if everything is good - otherwise a string telling you what is wrong.
|
|
408
447
|
*/
|
|
409
448
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
410
449
|
const validateIrisApp = (irisApp) => __awaiter(void 0, void 0, void 0, function* () {
|
package/index2.js
CHANGED
|
@@ -54,6 +54,9 @@ const defaultOptions = {
|
|
|
54
54
|
errorPolicy: "all",
|
|
55
55
|
},
|
|
56
56
|
};
|
|
57
|
+
/**
|
|
58
|
+
* This is a wrapper around the MockedProvider that logs errors to the console.
|
|
59
|
+
*/
|
|
57
60
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
58
61
|
function ApolloMockedProviderWithError(props) {
|
|
59
62
|
const { mocks } = props, otherProps = __rest(props, ["mocks"]);
|
|
@@ -62,14 +65,14 @@ function ApolloMockedProviderWithError(props) {
|
|
|
62
65
|
if (graphQLErrors) {
|
|
63
66
|
// eslint-disable-next-line array-callback-return
|
|
64
67
|
graphQLErrors.map(({ message, locations, path }) => {
|
|
65
|
-
if (process.env
|
|
68
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
66
69
|
// eslint-disable-next-line no-console
|
|
67
70
|
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
|
|
68
71
|
}
|
|
69
72
|
});
|
|
70
73
|
}
|
|
71
74
|
if (networkError) {
|
|
72
|
-
if (process.env
|
|
75
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
73
76
|
// eslint-disable-next-line no-console
|
|
74
77
|
console.log(`[Network error]: ${networkError}`);
|
|
75
78
|
}
|
|
@@ -79,6 +82,9 @@ function ApolloMockedProviderWithError(props) {
|
|
|
79
82
|
return (jsx(MockedProvider, Object.assign({}, otherProps, { link: link, defaultOptions: Object.assign(Object.assign({}, defaultOptions), { watchQuery: { fetchPolicy: "no-cache" } }) })));
|
|
80
83
|
}
|
|
81
84
|
|
|
85
|
+
/**
|
|
86
|
+
* Do nothing
|
|
87
|
+
*/
|
|
82
88
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
83
89
|
const doNothing = () => {
|
|
84
90
|
/* Do nothing */
|
|
@@ -92,6 +98,12 @@ const mockAssetSortingContext = {
|
|
|
92
98
|
},
|
|
93
99
|
};
|
|
94
100
|
|
|
101
|
+
/**
|
|
102
|
+
* Mocks the current user context
|
|
103
|
+
*
|
|
104
|
+
* @param overrides - The overrides to apply to the mock
|
|
105
|
+
* @returns {ICurrentUserContext} - Returns the mocked current user context
|
|
106
|
+
*/
|
|
95
107
|
const mockCurrentUserContext = (overrides = {}) => (Object.assign({ userName: "", userRole: "", customerId: 12345, userId: 154312, tasUserId: "751ea227-f199-4d00-925f-a608312c5e45", email: "", name: "", accountId: "", assumedUser: null, jobTitle: "", isAuthenticated: true, isVerified: true, isAccountOwner: true, subscriptionPackage: "EXPAND_FLEET_OWNER" }, overrides));
|
|
96
108
|
|
|
97
109
|
const mockEnvironmentContext = {
|
|
@@ -129,6 +141,13 @@ const mockToastContext = {
|
|
|
129
141
|
setIsManifestError: doNothing,
|
|
130
142
|
};
|
|
131
143
|
|
|
144
|
+
/**
|
|
145
|
+
* This is a mock for the UserSubscriptionContext.
|
|
146
|
+
*
|
|
147
|
+
* @param features - array of features
|
|
148
|
+
* @param packageType - package type
|
|
149
|
+
* @returns IUserSubscriptionContext
|
|
150
|
+
*/
|
|
132
151
|
const mockUserSubscriptionProviderValue = (features, packageType) => {
|
|
133
152
|
return {
|
|
134
153
|
numberOfDaysWithAccessToHistoricalData: 30,
|
|
@@ -138,6 +157,13 @@ const mockUserSubscriptionProviderValue = (features, packageType) => {
|
|
|
138
157
|
};
|
|
139
158
|
};
|
|
140
159
|
|
|
160
|
+
/**
|
|
161
|
+
* Flushes all promises in the queue.
|
|
162
|
+
* This is useful when testing async code.
|
|
163
|
+
*
|
|
164
|
+
* @param waitTimeInMS - The amount of time to wait before resolving the promise.
|
|
165
|
+
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
166
|
+
*/
|
|
141
167
|
const flushPromises = (waitTimeInMS = 0) => {
|
|
142
168
|
return new Promise(resolve => {
|
|
143
169
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -150,6 +176,13 @@ const flushPromises = (waitTimeInMS = 0) => {
|
|
|
150
176
|
}
|
|
151
177
|
});
|
|
152
178
|
};
|
|
179
|
+
/**
|
|
180
|
+
* Flushes all promises in the queue.
|
|
181
|
+
* This is useful when testing async code.
|
|
182
|
+
*
|
|
183
|
+
* @param waitTimeInMS - The amount of time to wait before resolving the promise.
|
|
184
|
+
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
185
|
+
*/
|
|
153
186
|
const flushPromisesInAct = (waitTimeInMS = 0) => {
|
|
154
187
|
return act(() => {
|
|
155
188
|
return new Promise(resolve => {
|
|
@@ -186,7 +219,7 @@ class MockContextProviderBuilder {
|
|
|
186
219
|
/**
|
|
187
220
|
* Use this Environment Context.
|
|
188
221
|
*
|
|
189
|
-
* @param environmentContext
|
|
222
|
+
* @param environmentContext - The environment context to use.
|
|
190
223
|
*/
|
|
191
224
|
environment(environmentContext) {
|
|
192
225
|
this.selectedEnvironmentContext = environmentContext || mockEnvironmentContext;
|
|
@@ -195,7 +228,7 @@ class MockContextProviderBuilder {
|
|
|
195
228
|
/**
|
|
196
229
|
* Use this token.
|
|
197
230
|
*
|
|
198
|
-
* @param token
|
|
231
|
+
* @param token - The token to use.
|
|
199
232
|
*/
|
|
200
233
|
token(token) {
|
|
201
234
|
this.selectedTokenContext = { token };
|
|
@@ -204,7 +237,7 @@ class MockContextProviderBuilder {
|
|
|
204
237
|
/**
|
|
205
238
|
* Use this to pass in userSubscriptionPackage.
|
|
206
239
|
*
|
|
207
|
-
* @param developerSettingsContext
|
|
240
|
+
* @param developerSettingsContext - The developer settings context to use.
|
|
208
241
|
*/
|
|
209
242
|
userSubscriptionPackage(userSubscriptionPackage) {
|
|
210
243
|
if (userSubscriptionPackage) {
|
|
@@ -215,7 +248,7 @@ class MockContextProviderBuilder {
|
|
|
215
248
|
/**
|
|
216
249
|
* Use this to pass in SupportedFeatures.
|
|
217
250
|
*
|
|
218
|
-
* @param developerSettingsContext
|
|
251
|
+
* @param developerSettingsContext - The developer settings context to use.
|
|
219
252
|
*/
|
|
220
253
|
supportedFeatures(features) {
|
|
221
254
|
if (features) {
|
|
@@ -231,7 +264,7 @@ class MockContextProviderBuilder {
|
|
|
231
264
|
/**
|
|
232
265
|
* Assume this user
|
|
233
266
|
*
|
|
234
|
-
* @param assumeUser
|
|
267
|
+
* @param assumeUser - The user to assume.
|
|
235
268
|
*/
|
|
236
269
|
assumeUser(assumeUser) {
|
|
237
270
|
this.selectedAssumedUser = assumeUser;
|
|
@@ -240,7 +273,7 @@ class MockContextProviderBuilder {
|
|
|
240
273
|
/**
|
|
241
274
|
* Use this Router Props with the given mocks.
|
|
242
275
|
*
|
|
243
|
-
* @param routerProps
|
|
276
|
+
* @param routerProps - The router props to use.
|
|
244
277
|
*/
|
|
245
278
|
routerProps(routerProps) {
|
|
246
279
|
this.selectedRouterProps = routerProps || {};
|
|
@@ -249,7 +282,7 @@ class MockContextProviderBuilder {
|
|
|
249
282
|
/**
|
|
250
283
|
* Use this global selection.
|
|
251
284
|
*
|
|
252
|
-
* @param globalSelection
|
|
285
|
+
* @param globalSelection - The global selection to use.
|
|
253
286
|
*/
|
|
254
287
|
globalSelection(globalSelection) {
|
|
255
288
|
this.selectedGlobalSelection = { selection: globalSelection };
|
|
@@ -264,6 +297,9 @@ class MockContextProviderBuilder {
|
|
|
264
297
|
this.selectedAssetSortingContext = Object.assign(Object.assign({}, mockAssetSortingContext), overrides);
|
|
265
298
|
return this;
|
|
266
299
|
}
|
|
300
|
+
/**
|
|
301
|
+
* This will return the mocked composition root.
|
|
302
|
+
*/
|
|
267
303
|
renderHook(callback, parentElement) {
|
|
268
304
|
return __awaiter(this, void 0, void 0, function* () {
|
|
269
305
|
this.validateSuppliedMocks();
|
|
@@ -275,7 +311,7 @@ class MockContextProviderBuilder {
|
|
|
275
311
|
/**
|
|
276
312
|
* Use this Manager Apollo Context Provider with the given mocks.
|
|
277
313
|
*
|
|
278
|
-
* @param apolloMocks
|
|
314
|
+
* @param apolloMocks - The mocks to use for the ApolloProvider.
|
|
279
315
|
*/
|
|
280
316
|
apollo(apolloMocks) {
|
|
281
317
|
this.selectedApolloMocks = apolloMocks || [];
|
|
@@ -333,6 +369,9 @@ class MockContextProviderBuilder {
|
|
|
333
369
|
return (jsx(EnvironmentContextProvider, Object.assign({ value: this.selectedEnvironmentContext }, { children: jsx(MemoryRouter, Object.assign({}, this.selectedRouterProps, { children: jsx(CurrentUserProvider, Object.assign({ value: mockCurrentUserContext({ assumedUser: this.selectedAssumedUser }) }, { children: jsx(UserSubscriptionProvider, Object.assign({ value: mockUserSubscriptionProviderValue(this.features, this.userSubscriptionPackageType) }, { 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 })) })) })) })) })) })) })) })) })) })));
|
|
334
370
|
}
|
|
335
371
|
}
|
|
372
|
+
/**
|
|
373
|
+
*
|
|
374
|
+
*/
|
|
336
375
|
const rootContext = () => new MockContextProviderBuilder();
|
|
337
376
|
const TestRoot = tw.div `
|
|
338
377
|
--tw-scale-x: 0.99;
|
|
@@ -351,7 +390,7 @@ const TestRoot = tw.div `
|
|
|
351
390
|
* Note that *all* properties should be given a value, use `null` in place of `undefined`,
|
|
352
391
|
* otherwise nothing will be returned.
|
|
353
392
|
* @param error ApolloError object to be returned.
|
|
354
|
-
* @returns MockedResponse that can be passed to the mocked ApolloProvider.
|
|
393
|
+
* @returns {MockedResponse} that can be passed to the mocked ApolloProvider.
|
|
355
394
|
* @see [Testing React components using MockedProvider and associated APIs](https://www.apollographql.com/docs/react/development-testing/testing/)
|
|
356
395
|
*/
|
|
357
396
|
const queryFor = (document, variables, data, error) => {
|
|
@@ -361,7 +400,7 @@ const queryFor = (document, variables, data, error) => {
|
|
|
361
400
|
variables,
|
|
362
401
|
},
|
|
363
402
|
newData: () => {
|
|
364
|
-
if (process.env
|
|
403
|
+
if (process.env.VSCODE_INSPECTOR_OPTIONS || process.env.DEBUG) {
|
|
365
404
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
366
405
|
const name = document.definitions[0].name.value;
|
|
367
406
|
// eslint-disable-next-line no-console
|
|
@@ -392,6 +431,7 @@ const queryFor = (document, variables, data, error) => {
|
|
|
392
431
|
* According to the single spa spec.
|
|
393
432
|
*
|
|
394
433
|
* Your test could look like this
|
|
434
|
+
*
|
|
395
435
|
* @example
|
|
396
436
|
* import * as IrisApp from "./index";
|
|
397
437
|
describe("App", () => {
|
|
@@ -400,9 +440,8 @@ const queryFor = (document, variables, data, error) => {
|
|
|
400
440
|
expect(result).toBeNull();
|
|
401
441
|
});
|
|
402
442
|
});
|
|
403
|
-
*
|
|
404
443
|
* @param irisApp Import the index ts(x) file as above and pass it in.
|
|
405
|
-
* @returns null if everything is good - otherwise a string telling you what is wrong.
|
|
444
|
+
* @returns {null|string} if everything is good - otherwise a string telling you what is wrong.
|
|
406
445
|
*/
|
|
407
446
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
408
447
|
const validateIrisApp = (irisApp) => __awaiter(void 0, void 0, void 0, function* () {
|
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.42",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@apollo/client": "3.7.10",
|
|
12
12
|
"react": "18.2.0",
|
|
13
|
-
"@trackunit/react-core-contexts-api": "0.2.
|
|
14
|
-
"@trackunit/react-core-hooks": "0.2.
|
|
15
|
-
"@trackunit/tailwind-styled-components": "0.0.55
|
|
13
|
+
"@trackunit/react-core-contexts-api": "0.2.23",
|
|
14
|
+
"@trackunit/react-core-hooks": "0.2.38",
|
|
15
|
+
"@trackunit/tailwind-styled-components": "0.0.55",
|
|
16
16
|
"react-router-dom": "6.4.5",
|
|
17
17
|
"graphql": "15.8.0"
|
|
18
18
|
},
|
package/src/HookRenderer.d.ts
CHANGED
|
@@ -1,3 +1,11 @@
|
|
|
1
1
|
import { RenderHookResult } from "@testing-library/react/pure";
|
|
2
2
|
import * as React from "react";
|
|
3
|
+
/**
|
|
4
|
+
* This is a wrapper around the react testing library renderHook function.
|
|
5
|
+
* It will make sure that the act function is called and that the promises are flushed.
|
|
6
|
+
*
|
|
7
|
+
* @param callback The callback to render the hook.
|
|
8
|
+
* @param getMockedCompositionRoot A function that will return the mocked composition root.
|
|
9
|
+
* @returns The rendered hook.
|
|
10
|
+
*/
|
|
3
11
|
export declare const reactHooksRenderHook: <TProps, TResult>(callback: (props: TProps) => TResult, getMockedCompositionRoot: (children: React.ReactElement) => React.ReactElement) => Promise<RenderHookResult<TResult, TProps>>;
|
|
@@ -22,43 +22,43 @@ export declare class MockContextProviderBuilder {
|
|
|
22
22
|
/**
|
|
23
23
|
* Use this Environment Context.
|
|
24
24
|
*
|
|
25
|
-
* @param environmentContext
|
|
25
|
+
* @param environmentContext - The environment context to use.
|
|
26
26
|
*/
|
|
27
27
|
environment(environmentContext?: IEnvironmentContext): this;
|
|
28
28
|
/**
|
|
29
29
|
* Use this token.
|
|
30
30
|
*
|
|
31
|
-
* @param token
|
|
31
|
+
* @param token - The token to use.
|
|
32
32
|
*/
|
|
33
33
|
token(token: string): this;
|
|
34
34
|
/**
|
|
35
35
|
* Use this to pass in userSubscriptionPackage.
|
|
36
36
|
*
|
|
37
|
-
* @param developerSettingsContext
|
|
37
|
+
* @param developerSettingsContext - The developer settings context to use.
|
|
38
38
|
*/
|
|
39
39
|
userSubscriptionPackage(userSubscriptionPackage?: UserSubscriptionPackageType): this;
|
|
40
40
|
/**
|
|
41
41
|
* Use this to pass in SupportedFeatures.
|
|
42
42
|
*
|
|
43
|
-
* @param developerSettingsContext
|
|
43
|
+
* @param developerSettingsContext - The developer settings context to use.
|
|
44
44
|
*/
|
|
45
45
|
supportedFeatures(features?: string[]): this;
|
|
46
46
|
/**
|
|
47
47
|
* Assume this user
|
|
48
48
|
*
|
|
49
|
-
* @param assumeUser
|
|
49
|
+
* @param assumeUser - The user to assume.
|
|
50
50
|
*/
|
|
51
51
|
assumeUser(assumeUser: ICurrentUserContext["assumedUser"]): this;
|
|
52
52
|
/**
|
|
53
53
|
* Use this Router Props with the given mocks.
|
|
54
54
|
*
|
|
55
|
-
* @param routerProps
|
|
55
|
+
* @param routerProps - The router props to use.
|
|
56
56
|
*/
|
|
57
57
|
routerProps(routerProps?: MemoryRouterProps): this;
|
|
58
58
|
/**
|
|
59
59
|
* Use this global selection.
|
|
60
60
|
*
|
|
61
|
-
* @param globalSelection
|
|
61
|
+
* @param globalSelection - The global selection to use.
|
|
62
62
|
*/
|
|
63
63
|
globalSelection(globalSelection: IGlobalSelectionContext["selection"]): this;
|
|
64
64
|
/**
|
|
@@ -67,11 +67,14 @@ export declare class MockContextProviderBuilder {
|
|
|
67
67
|
* @param overrides - Override the default context.
|
|
68
68
|
*/
|
|
69
69
|
assetSorting(overrides: Partial<IAssetSortingContext>): this;
|
|
70
|
+
/**
|
|
71
|
+
* This will return the mocked composition root.
|
|
72
|
+
*/
|
|
70
73
|
renderHook<TProps, TResult>(callback: (props: TProps) => TResult, parentElement?: (children: React.ReactElement) => React.ReactElement): Promise<import("@testing-library/react").RenderHookResult<TResult, TProps>>;
|
|
71
74
|
/**
|
|
72
75
|
* Use this Manager Apollo Context Provider with the given mocks.
|
|
73
76
|
*
|
|
74
|
-
* @param apolloMocks
|
|
77
|
+
* @param apolloMocks - The mocks to use for the ApolloProvider.
|
|
75
78
|
*/
|
|
76
79
|
apollo(apolloMocks?: MockedResponse[]): this;
|
|
77
80
|
/**
|
|
@@ -101,4 +104,7 @@ export declare class MockContextProviderBuilder {
|
|
|
101
104
|
*/
|
|
102
105
|
protected getMockedCompositionRoot(testChildren: React.ReactNode, addTestRootContainer?: boolean): JSX.Element;
|
|
103
106
|
}
|
|
107
|
+
/**
|
|
108
|
+
*
|
|
109
|
+
*/
|
|
104
110
|
export declare const rootContext: () => MockContextProviderBuilder;
|
|
@@ -1,2 +1,8 @@
|
|
|
1
1
|
import { ICurrentUserContext } from "@trackunit/react-core-contexts-api";
|
|
2
|
+
/**
|
|
3
|
+
* Mocks the current user context
|
|
4
|
+
*
|
|
5
|
+
* @param overrides - The overrides to apply to the mock
|
|
6
|
+
* @returns {ICurrentUserContext} - Returns the mocked current user context
|
|
7
|
+
*/
|
|
2
8
|
export declare const mockCurrentUserContext: (overrides?: Partial<ICurrentUserContext>) => ICurrentUserContext;
|
|
@@ -1,2 +1,9 @@
|
|
|
1
1
|
import { IFeature, IUserSubscriptionContext, UserSubscriptionPackageType } from "@trackunit/react-core-contexts-api";
|
|
2
|
+
/**
|
|
3
|
+
* This is a mock for the UserSubscriptionContext.
|
|
4
|
+
*
|
|
5
|
+
* @param features - array of features
|
|
6
|
+
* @param packageType - package type
|
|
7
|
+
* @returns IUserSubscriptionContext
|
|
8
|
+
*/
|
|
2
9
|
export declare const mockUserSubscriptionProviderValue: (features: IFeature[], packageType: UserSubscriptionPackageType) => IUserSubscriptionContext;
|
package/src/utils/doNothing.d.ts
CHANGED
package/src/utils/queryFor.d.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { DocumentNode } from "graphql";
|
|
|
10
10
|
* Note that *all* properties should be given a value, use `null` in place of `undefined`,
|
|
11
11
|
* otherwise nothing will be returned.
|
|
12
12
|
* @param error ApolloError object to be returned.
|
|
13
|
-
* @returns MockedResponse that can be passed to the mocked ApolloProvider.
|
|
13
|
+
* @returns {MockedResponse} that can be passed to the mocked ApolloProvider.
|
|
14
14
|
* @see [Testing React components using MockedProvider and associated APIs](https://www.apollographql.com/docs/react/development-testing/testing/)
|
|
15
15
|
*/
|
|
16
16
|
export declare const queryFor: <Result extends {
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
* According to the single spa spec.
|
|
7
7
|
*
|
|
8
8
|
* Your test could look like this
|
|
9
|
+
*
|
|
9
10
|
* @example
|
|
10
11
|
* import * as IrisApp from "./index";
|
|
11
12
|
describe("App", () => {
|
|
@@ -14,8 +15,7 @@
|
|
|
14
15
|
expect(result).toBeNull();
|
|
15
16
|
});
|
|
16
17
|
});
|
|
17
|
-
*
|
|
18
18
|
* @param irisApp Import the index ts(x) file as above and pass it in.
|
|
19
|
-
* @returns null if everything is good - otherwise a string telling you what is wrong.
|
|
19
|
+
* @returns {null|string} if everything is good - otherwise a string telling you what is wrong.
|
|
20
20
|
*/
|
|
21
21
|
export declare const validateIrisApp: (irisApp: any) => Promise<"Missing a bootstrap function" | "Missing a mount function" | "Missing an unmount function" | "Update must be a function" | null>;
|
package/src/utils/wait.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Flushes all promises in the queue.
|
|
3
|
+
* This is useful when testing async code.
|
|
4
|
+
*
|
|
5
|
+
* @param waitTimeInMS - The amount of time to wait before resolving the promise.
|
|
6
|
+
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
7
|
+
*/
|
|
1
8
|
export declare const flushPromises: (waitTimeInMS?: number) => Promise<unknown>;
|
|
9
|
+
/**
|
|
10
|
+
* Flushes all promises in the queue.
|
|
11
|
+
* This is useful when testing async code.
|
|
12
|
+
*
|
|
13
|
+
* @param waitTimeInMS - The amount of time to wait before resolving the promise.
|
|
14
|
+
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
15
|
+
*/
|
|
2
16
|
export declare const flushPromisesInAct: (waitTimeInMS?: number) => Promise<unknown>;
|