@trackunit/react-core-contexts-test 0.1.284 → 0.1.285
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.js +7 -3
- package/HookRenderer.esm.js +8 -4
- package/index.cjs.js +58 -12
- package/index.esm.js +58 -12
- package/package.json +1 -1
- package/src/ApolloMockedProviderWithError.d.ts +0 -1
- package/src/HookRenderer.d.ts +1 -1
- package/src/TrackunitProvidersMockBuilder.d.ts +9 -4
- package/src/rerenderProvider/RerenderComponent.d.ts +7 -0
- package/src/rerenderProvider/RerenderProvider.d.ts +18 -0
package/HookRenderer.cjs.js
CHANGED
|
@@ -1,19 +1,19 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var react = require('@testing-library/react');
|
|
4
|
-
var pure = require('@testing-library/react/pure');
|
|
5
4
|
var index = require('./index.cjs.js');
|
|
6
5
|
require('react/jsx-runtime');
|
|
7
6
|
require('@trackunit/react-core-hooks');
|
|
8
7
|
require('lodash/omit');
|
|
9
8
|
require('@apollo/client');
|
|
9
|
+
require('@apollo/client/dev');
|
|
10
10
|
require('@apollo/client/link/error');
|
|
11
11
|
require('@apollo/client/testing');
|
|
12
12
|
require('@trackunit/react-core-contexts-api');
|
|
13
13
|
require('@trackunit/shared-utils');
|
|
14
|
+
require('react');
|
|
14
15
|
require('@apollo/client/react');
|
|
15
16
|
require('@tanstack/react-router');
|
|
16
|
-
require('react');
|
|
17
17
|
require('graphql');
|
|
18
18
|
|
|
19
19
|
/**
|
|
@@ -34,11 +34,15 @@ const reactHooksRenderHook = async (callback, getMockedCompositionRoot) => {
|
|
|
34
34
|
// This is added here to make storybook work and to ensure the right act is loaded for hooks.
|
|
35
35
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
36
36
|
await react.act(async () => {
|
|
37
|
-
renderedHook = await
|
|
37
|
+
renderedHook = await react.renderHook(callback, { wrapper });
|
|
38
38
|
});
|
|
39
39
|
await react.act(async () => {
|
|
40
40
|
await index.flushPromises();
|
|
41
|
+
// rerender to make sure that all hooks can survive the rerender - this is like strict mode from react.
|
|
42
|
+
renderedHook.rerender();
|
|
43
|
+
await index.flushPromises();
|
|
41
44
|
});
|
|
45
|
+
await index.flushPromisesInAct();
|
|
42
46
|
return renderedHook;
|
|
43
47
|
};
|
|
44
48
|
|
package/HookRenderer.esm.js
CHANGED
|
@@ -1,17 +1,17 @@
|
|
|
1
|
-
import { act } from '@testing-library/react';
|
|
2
|
-
import {
|
|
3
|
-
import { flushPromises } from './index.esm.js';
|
|
1
|
+
import { act, renderHook } from '@testing-library/react';
|
|
2
|
+
import { flushPromises, flushPromisesInAct } from './index.esm.js';
|
|
4
3
|
import 'react/jsx-runtime';
|
|
5
4
|
import '@trackunit/react-core-hooks';
|
|
6
5
|
import 'lodash/omit';
|
|
7
6
|
import '@apollo/client';
|
|
7
|
+
import '@apollo/client/dev';
|
|
8
8
|
import '@apollo/client/link/error';
|
|
9
9
|
import '@apollo/client/testing';
|
|
10
10
|
import '@trackunit/react-core-contexts-api';
|
|
11
11
|
import '@trackunit/shared-utils';
|
|
12
|
+
import 'react';
|
|
12
13
|
import '@apollo/client/react';
|
|
13
14
|
import '@tanstack/react-router';
|
|
14
|
-
import 'react';
|
|
15
15
|
import 'graphql';
|
|
16
16
|
|
|
17
17
|
/**
|
|
@@ -36,7 +36,11 @@ const reactHooksRenderHook = async (callback, getMockedCompositionRoot) => {
|
|
|
36
36
|
});
|
|
37
37
|
await act(async () => {
|
|
38
38
|
await flushPromises();
|
|
39
|
+
// rerender to make sure that all hooks can survive the rerender - this is like strict mode from react.
|
|
40
|
+
renderedHook.rerender();
|
|
41
|
+
await flushPromises();
|
|
39
42
|
});
|
|
43
|
+
await flushPromisesInAct();
|
|
40
44
|
return renderedHook;
|
|
41
45
|
};
|
|
42
46
|
|
package/index.cjs.js
CHANGED
|
@@ -5,13 +5,14 @@ var react$1 = require('@testing-library/react');
|
|
|
5
5
|
var reactCoreHooks = require('@trackunit/react-core-hooks');
|
|
6
6
|
var omit = require('lodash/omit');
|
|
7
7
|
var client = require('@apollo/client');
|
|
8
|
+
var dev = require('@apollo/client/dev');
|
|
8
9
|
var error = require('@apollo/client/link/error');
|
|
9
10
|
var testing = require('@apollo/client/testing');
|
|
10
11
|
var reactCoreContextsApi = require('@trackunit/react-core-contexts-api');
|
|
11
12
|
var sharedUtils = require('@trackunit/shared-utils');
|
|
13
|
+
var React = require('react');
|
|
12
14
|
var react = require('@apollo/client/react');
|
|
13
15
|
var reactRouter = require('@tanstack/react-router');
|
|
14
|
-
var React = require('react');
|
|
15
16
|
var graphql = require('graphql');
|
|
16
17
|
|
|
17
18
|
const defaultOptions = {
|
|
@@ -27,19 +28,21 @@ const defaultOptions = {
|
|
|
27
28
|
*/
|
|
28
29
|
function ApolloMockedProviderWithError(props) {
|
|
29
30
|
const isDebugging = !!process.env.VSCODE_INSPECTOR_OPTIONS || !!process.env.DEBUG;
|
|
31
|
+
dev.loadDevMessages();
|
|
32
|
+
dev.loadErrorMessages();
|
|
30
33
|
const { mocks, ...otherProps } = props;
|
|
31
|
-
const mockLink = new testing.MockLink(mocks, false, { showWarnings:
|
|
34
|
+
const mockLink = new testing.MockLink(mocks, false, { showWarnings: isDebugging });
|
|
32
35
|
const errorLoggingLink = error.onError(({ graphQLErrors, networkError }) => {
|
|
33
36
|
if (graphQLErrors) {
|
|
34
37
|
// eslint-disable-next-line array-callback-return
|
|
35
38
|
graphQLErrors.map(({ message, locations, path }) => {
|
|
36
|
-
if (
|
|
39
|
+
if (isDebugging) {
|
|
37
40
|
// eslint-disable-next-line no-console
|
|
38
41
|
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
|
|
39
42
|
}
|
|
40
43
|
});
|
|
41
44
|
}
|
|
42
|
-
if (networkError &&
|
|
45
|
+
if (networkError && isDebugging) {
|
|
43
46
|
// eslint-disable-next-line no-console
|
|
44
47
|
console.log(`[Network error]: ${networkError}`);
|
|
45
48
|
}
|
|
@@ -219,6 +222,40 @@ const mockUserSubscriptionContext = {
|
|
|
219
222
|
packageType: "EXPAND_FLEET_OWNER",
|
|
220
223
|
};
|
|
221
224
|
|
|
225
|
+
const RerenderContext = React.createContext({ rerenderCounter: 0, setRerenderCounter: (value) => { } });
|
|
226
|
+
/**
|
|
227
|
+
* This provider is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
228
|
+
*/
|
|
229
|
+
const RerenderProvider = ({ children, counter }) => {
|
|
230
|
+
const [rerenderCounter, setRerenderCounter] = React.useState(0);
|
|
231
|
+
React.useEffect(() => {
|
|
232
|
+
if (counter && rerenderCounter !== counter && rerenderCounter !== 0) {
|
|
233
|
+
setRerenderCounter(counter);
|
|
234
|
+
}
|
|
235
|
+
}, [rerenderCounter, counter]);
|
|
236
|
+
return (jsxRuntime.jsx(RerenderContext.Provider, { value: { rerenderCounter, setRerenderCounter }, children: children }));
|
|
237
|
+
};
|
|
238
|
+
/**
|
|
239
|
+
* This hook is used to get the rerender counter.
|
|
240
|
+
*/
|
|
241
|
+
const useRerenderCounter = () => {
|
|
242
|
+
return React.useContext(RerenderContext);
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* This component is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
247
|
+
*/
|
|
248
|
+
const RerenderComponent = ({ children }) => {
|
|
249
|
+
const { rerenderCounter } = useRerenderCounter();
|
|
250
|
+
return (jsxRuntime.jsx("div", { "data-rerender-counter": `rerender-${rerenderCounter}`, children: React.Children.map(children, child => React.isValidElement(child)
|
|
251
|
+
? React.createElement(child.type, {
|
|
252
|
+
...child.props,
|
|
253
|
+
key: child.key,
|
|
254
|
+
"data-rerender-counter": rerenderCounter,
|
|
255
|
+
})
|
|
256
|
+
: child) }));
|
|
257
|
+
};
|
|
258
|
+
|
|
222
259
|
const buildFlatRouteMap = (routes) => {
|
|
223
260
|
const routeMap = {};
|
|
224
261
|
routes.forEach(route => {
|
|
@@ -829,14 +866,13 @@ class TrackunitProvidersMockBuilder {
|
|
|
829
866
|
* Make sure this represent the same structure as the main index.tsx does.
|
|
830
867
|
*
|
|
831
868
|
* @param testChildren - the child element being tested.
|
|
832
|
-
* @param addTestRootContainer - if you want to add a root container to the test.
|
|
833
869
|
*/
|
|
834
|
-
getMockedCompositionRoot(testChildren
|
|
835
|
-
return (jsxRuntime.jsx(reactCoreHooks.ErrorHandlingContextProvider, { value: this.selectedErrorHandler, children: jsxRuntime.jsx(reactCoreHooks.CurrentUserProvider, { value: this.selectedCurrentUserContext, children: jsxRuntime.jsx(reactCoreHooks.AnalyticsContext.Provider, { value: this.selectedAnalyticsContext, children: jsxRuntime.jsx(reactCoreHooks.UserSubscriptionProvider, { value: this.selectedUserSubscriptionContext, children: jsxRuntime.jsx(reactCoreHooks.OemBrandingContextProvider, { value: this.selectedOemBrandingContext, children: jsxRuntime.jsx(reactCoreHooks.TokenProvider, { value: this.selectedTokenContext, children: jsxRuntime.jsx(reactCoreHooks.ToastProvider, { value: this.selectedToastContext, children: jsxRuntime.jsx(reactCoreHooks.ConfirmationDialogProvider, { value: this.selectedConfirmationDialogContext, children: jsxRuntime.jsx(reactCoreHooks.FilterBarProvider, { value: { filterBarValues: this.selectedFilterBarValues }, children: jsxRuntime.jsx(reactCoreHooks.AssetSortingProvider, { value: this.selectedAssetSortingContext, children: jsxRuntime.jsx(ApolloMockedProviderWithError, { addTypename: false,
|
|
870
|
+
getMockedCompositionRoot(testChildren) {
|
|
871
|
+
return (jsxRuntime.jsx(reactCoreHooks.ErrorHandlingContextProvider, { value: this.selectedErrorHandler, children: jsxRuntime.jsx(reactCoreHooks.CurrentUserProvider, { value: this.selectedCurrentUserContext, children: jsxRuntime.jsx(reactCoreHooks.AnalyticsContext.Provider, { value: this.selectedAnalyticsContext, children: jsxRuntime.jsx(reactCoreHooks.UserSubscriptionProvider, { value: this.selectedUserSubscriptionContext, children: jsxRuntime.jsx(reactCoreHooks.OemBrandingContextProvider, { value: this.selectedOemBrandingContext, children: jsxRuntime.jsx(reactCoreHooks.TokenProvider, { value: this.selectedTokenContext, children: jsxRuntime.jsx(reactCoreHooks.ToastProvider, { value: this.selectedToastContext, children: jsxRuntime.jsx(reactCoreHooks.ConfirmationDialogProvider, { value: this.selectedConfirmationDialogContext, children: jsxRuntime.jsx(reactCoreHooks.FilterBarProvider, { value: { filterBarValues: this.selectedFilterBarValues }, children: jsxRuntime.jsx(reactCoreHooks.AssetSortingProvider, { value: this.selectedAssetSortingContext, children: jsxRuntime.jsx(ApolloMockedProviderWithError, { addTypename: false, mocks: this.selectedApolloMocks, children: jsxRuntime.jsx(reactCoreHooks.NavigationContextProvider, { value: this.selectedNavigationContext, children: jsxRuntime.jsx(reactCoreHooks.CurrentUserPreferenceProvider, { value: this.selectedCurrentUserPreferenceContext, children: jsxRuntime.jsx(reactCoreHooks.EnvironmentContextProvider, { value: this.selectedEnvironmentContext, children: jsxRuntime.jsx(reactCoreHooks.ModalDialogContextProvider, { value: this.selectedModalDialogContext, children: testChildren }) }) }) }) }) }) }) }) }) }) }) }) }) }) }));
|
|
836
872
|
}
|
|
837
|
-
getMockedCompositionRootWithRouter(testChildren
|
|
838
|
-
const childrenWithRouter = (jsxRuntime.jsx(RouterContainer, { addTestRootContainer:
|
|
839
|
-
return this.getMockedCompositionRoot(childrenWithRouter
|
|
873
|
+
getMockedCompositionRootWithRouter(testChildren) {
|
|
874
|
+
const childrenWithRouter = (jsxRuntime.jsx(RouterContainer, { addTestRootContainer: true, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: testChildren }));
|
|
875
|
+
return this.getMockedCompositionRoot(childrenWithRouter);
|
|
840
876
|
}
|
|
841
877
|
/**
|
|
842
878
|
* This will return the mocked composition root.
|
|
@@ -845,7 +881,16 @@ class TrackunitProvidersMockBuilder {
|
|
|
845
881
|
this.validateSuppliedMocks();
|
|
846
882
|
// This ensures correct act loading when using hooks and not loaded if this build is used for storybook
|
|
847
883
|
const hookRenderer = await Promise.resolve().then(function () { return require('./HookRenderer.cjs.js'); });
|
|
848
|
-
|
|
884
|
+
let rerenderCounter = 0;
|
|
885
|
+
const renderedHook = await hookRenderer.reactHooksRenderHook(callback, children => this.getMockedCompositionRoot(jsxRuntime.jsx(RerenderProvider, { counter: rerenderCounter, children: jsxRuntime.jsx(RouterContainer, { addTestRootContainer: true, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: jsxRuntime.jsx(RerenderComponent, { children: parentElement ? parentElement(children) : children }) }) })));
|
|
886
|
+
return {
|
|
887
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
888
|
+
...renderedHook,
|
|
889
|
+
rerender: async (props) => {
|
|
890
|
+
rerenderCounter += 1;
|
|
891
|
+
renderedHook.rerender(props);
|
|
892
|
+
},
|
|
893
|
+
};
|
|
849
894
|
}
|
|
850
895
|
/**
|
|
851
896
|
* This will use react-testing-library.render the child in the correct mocked hierarchy of context providers.
|
|
@@ -868,13 +913,14 @@ class TrackunitProvidersMockBuilder {
|
|
|
868
913
|
await react$1.act(async () => {
|
|
869
914
|
await flushPromises();
|
|
870
915
|
});
|
|
916
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
871
917
|
return mountedcomponent;
|
|
872
918
|
}
|
|
873
919
|
/**
|
|
874
920
|
* This will return the children in the correct mocked hierarchy of context providers.
|
|
875
921
|
*/
|
|
876
922
|
storybook(child) {
|
|
877
|
-
return this.getMockedCompositionRoot(child
|
|
923
|
+
return this.getMockedCompositionRoot(child);
|
|
878
924
|
}
|
|
879
925
|
}
|
|
880
926
|
/**
|
package/index.esm.js
CHANGED
|
@@ -3,13 +3,14 @@ import { act, render } from '@testing-library/react';
|
|
|
3
3
|
import { ErrorHandlingContextProvider, CurrentUserProvider, AnalyticsContext, UserSubscriptionProvider, OemBrandingContextProvider, TokenProvider, ToastProvider, ConfirmationDialogProvider, FilterBarProvider, AssetSortingProvider, NavigationContextProvider, CurrentUserPreferenceProvider, EnvironmentContextProvider, ModalDialogContextProvider } from '@trackunit/react-core-hooks';
|
|
4
4
|
import omit from 'lodash/omit';
|
|
5
5
|
import { ApolloLink } from '@apollo/client';
|
|
6
|
+
import { loadDevMessages, loadErrorMessages } from '@apollo/client/dev';
|
|
6
7
|
import { onError } from '@apollo/client/link/error';
|
|
7
8
|
import { MockLink, MockedProvider } from '@apollo/client/testing';
|
|
8
9
|
import { AssetSortByProperty, SortOrder } from '@trackunit/react-core-contexts-api';
|
|
9
10
|
import { doNothing as doNothing$1, objectValues, objectKeys } from '@trackunit/shared-utils';
|
|
11
|
+
import React, { createContext, useState, useEffect, useContext, useRef, useCallback, useMemo } from 'react';
|
|
10
12
|
import { useApolloClient } from '@apollo/client/react';
|
|
11
13
|
import { createMemoryHistory, createRootRoute, createRoute, createRouter, RouterProvider, Outlet } from '@tanstack/react-router';
|
|
12
|
-
import React, { useRef, useCallback, useMemo, useEffect } from 'react';
|
|
13
14
|
import { GraphQLError } from 'graphql';
|
|
14
15
|
|
|
15
16
|
const defaultOptions = {
|
|
@@ -25,19 +26,21 @@ const defaultOptions = {
|
|
|
25
26
|
*/
|
|
26
27
|
function ApolloMockedProviderWithError(props) {
|
|
27
28
|
const isDebugging = !!process.env.VSCODE_INSPECTOR_OPTIONS || !!process.env.DEBUG;
|
|
29
|
+
loadDevMessages();
|
|
30
|
+
loadErrorMessages();
|
|
28
31
|
const { mocks, ...otherProps } = props;
|
|
29
|
-
const mockLink = new MockLink(mocks, false, { showWarnings:
|
|
32
|
+
const mockLink = new MockLink(mocks, false, { showWarnings: isDebugging });
|
|
30
33
|
const errorLoggingLink = onError(({ graphQLErrors, networkError }) => {
|
|
31
34
|
if (graphQLErrors) {
|
|
32
35
|
// eslint-disable-next-line array-callback-return
|
|
33
36
|
graphQLErrors.map(({ message, locations, path }) => {
|
|
34
|
-
if (
|
|
37
|
+
if (isDebugging) {
|
|
35
38
|
// eslint-disable-next-line no-console
|
|
36
39
|
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
|
|
37
40
|
}
|
|
38
41
|
});
|
|
39
42
|
}
|
|
40
|
-
if (networkError &&
|
|
43
|
+
if (networkError && isDebugging) {
|
|
41
44
|
// eslint-disable-next-line no-console
|
|
42
45
|
console.log(`[Network error]: ${networkError}`);
|
|
43
46
|
}
|
|
@@ -217,6 +220,40 @@ const mockUserSubscriptionContext = {
|
|
|
217
220
|
packageType: "EXPAND_FLEET_OWNER",
|
|
218
221
|
};
|
|
219
222
|
|
|
223
|
+
const RerenderContext = createContext({ rerenderCounter: 0, setRerenderCounter: (value) => { } });
|
|
224
|
+
/**
|
|
225
|
+
* This provider is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
226
|
+
*/
|
|
227
|
+
const RerenderProvider = ({ children, counter }) => {
|
|
228
|
+
const [rerenderCounter, setRerenderCounter] = useState(0);
|
|
229
|
+
useEffect(() => {
|
|
230
|
+
if (counter && rerenderCounter !== counter && rerenderCounter !== 0) {
|
|
231
|
+
setRerenderCounter(counter);
|
|
232
|
+
}
|
|
233
|
+
}, [rerenderCounter, counter]);
|
|
234
|
+
return (jsx(RerenderContext.Provider, { value: { rerenderCounter, setRerenderCounter }, children: children }));
|
|
235
|
+
};
|
|
236
|
+
/**
|
|
237
|
+
* This hook is used to get the rerender counter.
|
|
238
|
+
*/
|
|
239
|
+
const useRerenderCounter = () => {
|
|
240
|
+
return useContext(RerenderContext);
|
|
241
|
+
};
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* This component is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
245
|
+
*/
|
|
246
|
+
const RerenderComponent = ({ children }) => {
|
|
247
|
+
const { rerenderCounter } = useRerenderCounter();
|
|
248
|
+
return (jsx("div", { "data-rerender-counter": `rerender-${rerenderCounter}`, children: React.Children.map(children, child => React.isValidElement(child)
|
|
249
|
+
? React.createElement(child.type, {
|
|
250
|
+
...child.props,
|
|
251
|
+
key: child.key,
|
|
252
|
+
"data-rerender-counter": rerenderCounter,
|
|
253
|
+
})
|
|
254
|
+
: child) }));
|
|
255
|
+
};
|
|
256
|
+
|
|
220
257
|
const buildFlatRouteMap = (routes) => {
|
|
221
258
|
const routeMap = {};
|
|
222
259
|
routes.forEach(route => {
|
|
@@ -827,14 +864,13 @@ class TrackunitProvidersMockBuilder {
|
|
|
827
864
|
* Make sure this represent the same structure as the main index.tsx does.
|
|
828
865
|
*
|
|
829
866
|
* @param testChildren - the child element being tested.
|
|
830
|
-
* @param addTestRootContainer - if you want to add a root container to the test.
|
|
831
867
|
*/
|
|
832
|
-
getMockedCompositionRoot(testChildren
|
|
833
|
-
return (jsx(ErrorHandlingContextProvider, { value: this.selectedErrorHandler, children: jsx(CurrentUserProvider, { value: this.selectedCurrentUserContext, children: jsx(AnalyticsContext.Provider, { value: this.selectedAnalyticsContext, children: jsx(UserSubscriptionProvider, { value: this.selectedUserSubscriptionContext, children: jsx(OemBrandingContextProvider, { value: this.selectedOemBrandingContext, children: jsx(TokenProvider, { value: this.selectedTokenContext, children: jsx(ToastProvider, { value: this.selectedToastContext, children: jsx(ConfirmationDialogProvider, { value: this.selectedConfirmationDialogContext, children: jsx(FilterBarProvider, { value: { filterBarValues: this.selectedFilterBarValues }, children: jsx(AssetSortingProvider, { value: this.selectedAssetSortingContext, children: jsx(ApolloMockedProviderWithError, { addTypename: false,
|
|
868
|
+
getMockedCompositionRoot(testChildren) {
|
|
869
|
+
return (jsx(ErrorHandlingContextProvider, { value: this.selectedErrorHandler, children: jsx(CurrentUserProvider, { value: this.selectedCurrentUserContext, children: jsx(AnalyticsContext.Provider, { value: this.selectedAnalyticsContext, children: jsx(UserSubscriptionProvider, { value: this.selectedUserSubscriptionContext, children: jsx(OemBrandingContextProvider, { value: this.selectedOemBrandingContext, children: jsx(TokenProvider, { value: this.selectedTokenContext, children: jsx(ToastProvider, { value: this.selectedToastContext, children: jsx(ConfirmationDialogProvider, { value: this.selectedConfirmationDialogContext, children: jsx(FilterBarProvider, { value: { filterBarValues: this.selectedFilterBarValues }, children: jsx(AssetSortingProvider, { value: this.selectedAssetSortingContext, children: jsx(ApolloMockedProviderWithError, { addTypename: false, mocks: this.selectedApolloMocks, children: jsx(NavigationContextProvider, { value: this.selectedNavigationContext, children: jsx(CurrentUserPreferenceProvider, { value: this.selectedCurrentUserPreferenceContext, children: jsx(EnvironmentContextProvider, { value: this.selectedEnvironmentContext, children: jsx(ModalDialogContextProvider, { value: this.selectedModalDialogContext, children: testChildren }) }) }) }) }) }) }) }) }) }) }) }) }) }) }));
|
|
834
870
|
}
|
|
835
|
-
getMockedCompositionRootWithRouter(testChildren
|
|
836
|
-
const childrenWithRouter = (jsx(RouterContainer, { addTestRootContainer:
|
|
837
|
-
return this.getMockedCompositionRoot(childrenWithRouter
|
|
871
|
+
getMockedCompositionRootWithRouter(testChildren) {
|
|
872
|
+
const childrenWithRouter = (jsx(RouterContainer, { addTestRootContainer: true, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: testChildren }));
|
|
873
|
+
return this.getMockedCompositionRoot(childrenWithRouter);
|
|
838
874
|
}
|
|
839
875
|
/**
|
|
840
876
|
* This will return the mocked composition root.
|
|
@@ -843,7 +879,16 @@ class TrackunitProvidersMockBuilder {
|
|
|
843
879
|
this.validateSuppliedMocks();
|
|
844
880
|
// This ensures correct act loading when using hooks and not loaded if this build is used for storybook
|
|
845
881
|
const hookRenderer = await import('./HookRenderer.esm.js');
|
|
846
|
-
|
|
882
|
+
let rerenderCounter = 0;
|
|
883
|
+
const renderedHook = await hookRenderer.reactHooksRenderHook(callback, children => this.getMockedCompositionRoot(jsx(RerenderProvider, { counter: rerenderCounter, children: jsx(RouterContainer, { addTestRootContainer: true, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: jsx(RerenderComponent, { children: parentElement ? parentElement(children) : children }) }) })));
|
|
884
|
+
return {
|
|
885
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
886
|
+
...renderedHook,
|
|
887
|
+
rerender: async (props) => {
|
|
888
|
+
rerenderCounter += 1;
|
|
889
|
+
renderedHook.rerender(props);
|
|
890
|
+
},
|
|
891
|
+
};
|
|
847
892
|
}
|
|
848
893
|
/**
|
|
849
894
|
* This will use react-testing-library.render the child in the correct mocked hierarchy of context providers.
|
|
@@ -866,13 +911,14 @@ class TrackunitProvidersMockBuilder {
|
|
|
866
911
|
await act(async () => {
|
|
867
912
|
await flushPromises();
|
|
868
913
|
});
|
|
914
|
+
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
869
915
|
return mountedcomponent;
|
|
870
916
|
}
|
|
871
917
|
/**
|
|
872
918
|
* This will return the children in the correct mocked hierarchy of context providers.
|
|
873
919
|
*/
|
|
874
920
|
storybook(child) {
|
|
875
|
-
return this.getMockedCompositionRoot(child
|
|
921
|
+
return this.getMockedCompositionRoot(child);
|
|
876
922
|
}
|
|
877
923
|
}
|
|
878
924
|
/**
|
package/package.json
CHANGED
package/src/HookRenderer.d.ts
CHANGED
|
@@ -356,14 +356,19 @@ export declare class TrackunitProvidersMockBuilder<T extends AnyRoute> {
|
|
|
356
356
|
* Make sure this represent the same structure as the main index.tsx does.
|
|
357
357
|
*
|
|
358
358
|
* @param testChildren - the child element being tested.
|
|
359
|
-
* @param addTestRootContainer - if you want to add a root container to the test.
|
|
360
359
|
*/
|
|
361
|
-
protected getMockedCompositionRoot(testChildren: React.ReactNode
|
|
362
|
-
protected getMockedCompositionRootWithRouter(testChildren: React.ReactNode
|
|
360
|
+
protected getMockedCompositionRoot(testChildren: React.ReactNode): import("react/jsx-runtime").JSX.Element;
|
|
361
|
+
protected getMockedCompositionRootWithRouter(testChildren: React.ReactNode): import("react/jsx-runtime").JSX.Element;
|
|
363
362
|
/**
|
|
364
363
|
* This will return the mocked composition root.
|
|
365
364
|
*/
|
|
366
|
-
renderHook<TProps, TResult>(callback: (props: TProps) => TResult, parentElement?: (children: React.ReactNode) => React.ReactElement): Promise<
|
|
365
|
+
renderHook<TProps, TResult>(callback: (props: TProps) => TResult, parentElement?: (children: React.ReactNode) => React.ReactElement): Promise<{
|
|
366
|
+
rerender: (props?: TProps) => Promise<void>;
|
|
367
|
+
result: {
|
|
368
|
+
current: TResult;
|
|
369
|
+
};
|
|
370
|
+
unmount: () => void;
|
|
371
|
+
}>;
|
|
367
372
|
/**
|
|
368
373
|
* This will use react-testing-library.render the child in the correct mocked hierarchy of context providers.
|
|
369
374
|
*
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
/**
|
|
3
|
+
* This component is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
4
|
+
*/
|
|
5
|
+
export declare const RerenderComponent: ({ children }: {
|
|
6
|
+
children: React.ReactNode;
|
|
7
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const RerenderContext: import("react").Context<{
|
|
2
|
+
rerenderCounter: number;
|
|
3
|
+
setRerenderCounter: (value: number) => void;
|
|
4
|
+
}>;
|
|
5
|
+
/**
|
|
6
|
+
* This provider is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
7
|
+
*/
|
|
8
|
+
export declare const RerenderProvider: ({ children, counter }: {
|
|
9
|
+
children: React.ReactNode;
|
|
10
|
+
counter: number;
|
|
11
|
+
}) => import("react/jsx-runtime").JSX.Element;
|
|
12
|
+
/**
|
|
13
|
+
* This hook is used to get the rerender counter.
|
|
14
|
+
*/
|
|
15
|
+
export declare const useRerenderCounter: () => {
|
|
16
|
+
rerenderCounter: number;
|
|
17
|
+
setRerenderCounter: (value: number) => void;
|
|
18
|
+
};
|