@trackunit/react-core-contexts-test 1.3.68 → 1.3.72
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/index.cjs.js +34 -29
- package/index.esm.js +9 -4
- package/package.json +4 -4
- package/src/TrackunitProvidersMockBuilder.d.ts +3 -3
package/index.cjs.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
var jsxRuntime = require('react/jsx-runtime');
|
|
4
|
-
var react$
|
|
4
|
+
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');
|
|
@@ -10,8 +10,8 @@ var error = require('@apollo/client/link/error');
|
|
|
10
10
|
var testing = require('@apollo/client/testing');
|
|
11
11
|
var reactCoreContextsApi = require('@trackunit/react-core-contexts-api');
|
|
12
12
|
var sharedUtils = require('@trackunit/shared-utils');
|
|
13
|
-
var
|
|
14
|
-
var react
|
|
13
|
+
var React = require('react');
|
|
14
|
+
var react = require('@apollo/client/react');
|
|
15
15
|
var reactRouter = require('@tanstack/react-router');
|
|
16
16
|
var graphql = require('graphql');
|
|
17
17
|
|
|
@@ -222,13 +222,13 @@ const mockUserSubscriptionContext = {
|
|
|
222
222
|
packageType: "EXPAND_FLEET_OWNER",
|
|
223
223
|
};
|
|
224
224
|
|
|
225
|
-
const RerenderContext =
|
|
225
|
+
const RerenderContext = React.createContext({ rerenderCounter: 0, setRerenderCounter: (value) => { } });
|
|
226
226
|
/**
|
|
227
227
|
* This provider is used to force re-renders in the test environment, since tanstack router does not support re-render.
|
|
228
228
|
*/
|
|
229
229
|
const RerenderProvider = ({ children, counter }) => {
|
|
230
|
-
const [rerenderCounter, setRerenderCounter] =
|
|
231
|
-
|
|
230
|
+
const [rerenderCounter, setRerenderCounter] = React.useState(0);
|
|
231
|
+
React.useEffect(() => {
|
|
232
232
|
if (counter && rerenderCounter !== counter && rerenderCounter !== 0) {
|
|
233
233
|
setRerenderCounter(counter);
|
|
234
234
|
}
|
|
@@ -239,7 +239,7 @@ const RerenderProvider = ({ children, counter }) => {
|
|
|
239
239
|
* This hook is used to get the rerender counter.
|
|
240
240
|
*/
|
|
241
241
|
const useRerenderCounter = () => {
|
|
242
|
-
return
|
|
242
|
+
return React.useContext(RerenderContext);
|
|
243
243
|
};
|
|
244
244
|
|
|
245
245
|
/**
|
|
@@ -247,8 +247,8 @@ const useRerenderCounter = () => {
|
|
|
247
247
|
*/
|
|
248
248
|
const RerenderComponent = ({ children }) => {
|
|
249
249
|
const { rerenderCounter } = useRerenderCounter();
|
|
250
|
-
return (jsxRuntime.jsx("div", { "data-rerender-counter": `rerender-${rerenderCounter}`, children:
|
|
251
|
-
?
|
|
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
252
|
...child.props,
|
|
253
253
|
key: child.key,
|
|
254
254
|
"data-rerender-counter": rerenderCounter,
|
|
@@ -300,14 +300,14 @@ const TestRenderChildren = ({ addTestRootContainer, children }) => {
|
|
|
300
300
|
* @returns {ReactElement} children component wrapped in a test root container
|
|
301
301
|
*/
|
|
302
302
|
const RouterContainer = ({ addTestRootContainer, selectedRouterProps, rootRoute, children, }) => {
|
|
303
|
-
const client = react
|
|
303
|
+
const client = react.useApolloClient();
|
|
304
304
|
// The current version of createMemoryHistory seem to have issues when NOT ending on / so adding a # will not effect what url is rendered but it seems to work
|
|
305
|
-
const memoryHistory =
|
|
305
|
+
const memoryHistory = React.useRef(reactRouter.createMemoryHistory({
|
|
306
306
|
initialEntries: selectedRouterProps?.initialEntries?.map(entry => entry.path + "#") ?? ["/#"],
|
|
307
307
|
initialIndex: 0,
|
|
308
308
|
}));
|
|
309
|
-
const getChildren =
|
|
310
|
-
const router =
|
|
309
|
+
const getChildren = React.useCallback(() => children, [children]);
|
|
310
|
+
const router = React.useMemo(() => {
|
|
311
311
|
let localRootRoute = rootRoute;
|
|
312
312
|
if (!localRootRoute) {
|
|
313
313
|
const route = reactRouter.createRootRoute({ component: RootRouteDebugger });
|
|
@@ -390,7 +390,7 @@ const RouterContainer = ({ addTestRootContainer, selectedRouterProps, rootRoute,
|
|
|
390
390
|
// This causes unexpected state updates in Tanstack router which make the test fail
|
|
391
391
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
392
392
|
}, [rootRoute]);
|
|
393
|
-
const context =
|
|
393
|
+
const context = React.useMemo(() => ({
|
|
394
394
|
hasAccessTo: async () => true,
|
|
395
395
|
isAuthenticated: true,
|
|
396
396
|
client,
|
|
@@ -430,7 +430,7 @@ const flushPromises = (waitTimeInMS = 0) => {
|
|
|
430
430
|
* @returns {Promise<void>} - Returns a promise that resolves after the wait time.
|
|
431
431
|
*/
|
|
432
432
|
const flushPromisesInAct = (waitTimeInMS = 0) => {
|
|
433
|
-
return react$
|
|
433
|
+
return react$1.act(() => {
|
|
434
434
|
return new Promise(resolve => {
|
|
435
435
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
436
436
|
if (global.ORG_setTimeout) {
|
|
@@ -470,7 +470,12 @@ class TrackunitProvidersMockBuilder {
|
|
|
470
470
|
this.selectedAnalyticsContext = mockAnalyticsContext;
|
|
471
471
|
this.selectedOemBrandingContext = mockOemBrandingContext;
|
|
472
472
|
this.selectedUserSubscriptionContext = mockUserSubscriptionContext;
|
|
473
|
-
this.selectedFilterBarValues = {
|
|
473
|
+
this.selectedFilterBarValues = {
|
|
474
|
+
filterBarValues: {},
|
|
475
|
+
assetsFilterBarValues: {},
|
|
476
|
+
customersFilterBarValues: {},
|
|
477
|
+
sitesFilterBarValues: {},
|
|
478
|
+
};
|
|
474
479
|
}
|
|
475
480
|
/**
|
|
476
481
|
* Use this Analytics Context.
|
|
@@ -624,7 +629,7 @@ class TrackunitProvidersMockBuilder {
|
|
|
624
629
|
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
625
630
|
*/
|
|
626
631
|
filterBarValues(filterBarValues) {
|
|
627
|
-
this.selectedFilterBarValues = filterBarValues;
|
|
632
|
+
this.selectedFilterBarValues = { ...this.selectedFilterBarValues, ...filterBarValues };
|
|
628
633
|
return this;
|
|
629
634
|
}
|
|
630
635
|
/**
|
|
@@ -865,7 +870,7 @@ class TrackunitProvidersMockBuilder {
|
|
|
865
870
|
* @param testChildren - the child element being tested.
|
|
866
871
|
*/
|
|
867
872
|
getMockedCompositionRoot(testChildren) {
|
|
868
|
-
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:
|
|
873
|
+
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: 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 }) }) }) }) }) }) }) }) }) }) }) }) }) }) }));
|
|
869
874
|
}
|
|
870
875
|
getMockedCompositionRootWithRouter(testChildren) {
|
|
871
876
|
const childrenWithRouter = (jsxRuntime.jsx(RouterContainer, { addTestRootContainer: true, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: testChildren }));
|
|
@@ -898,16 +903,16 @@ class TrackunitProvidersMockBuilder {
|
|
|
898
903
|
async render(child) {
|
|
899
904
|
this.validateSuppliedMocks();
|
|
900
905
|
let mountedcomponent;
|
|
901
|
-
await react$
|
|
902
|
-
mountedcomponent = react$
|
|
906
|
+
await react$1.act(async () => {
|
|
907
|
+
mountedcomponent = react$1.render(child, {
|
|
903
908
|
wrapper: ({ children }) => this.getMockedCompositionRootWithRouter(children),
|
|
904
909
|
});
|
|
905
910
|
await flushPromises();
|
|
906
911
|
});
|
|
907
|
-
await react$
|
|
912
|
+
await react$1.act(async () => {
|
|
908
913
|
await flushPromises();
|
|
909
914
|
});
|
|
910
|
-
await react$
|
|
915
|
+
await react$1.act(async () => {
|
|
911
916
|
await flushPromises();
|
|
912
917
|
});
|
|
913
918
|
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
@@ -936,8 +941,8 @@ const trackunitProviders = () => new TrackunitProvidersMockBuilder();
|
|
|
936
941
|
* @returns {boolean} Returns true if it is the first render, false otherwise.
|
|
937
942
|
*/
|
|
938
943
|
const useIsFirstRender = () => {
|
|
939
|
-
const [isFirstRender, setIsFirstRender] =
|
|
940
|
-
|
|
944
|
+
const [isFirstRender, setIsFirstRender] = React.useState(true);
|
|
945
|
+
React.useEffect(() => {
|
|
941
946
|
setIsFirstRender(false);
|
|
942
947
|
}, []);
|
|
943
948
|
return isFirstRender;
|
|
@@ -955,8 +960,8 @@ const useIsFirstRender = () => {
|
|
|
955
960
|
* useDebugger(propsToWatch);
|
|
956
961
|
*/
|
|
957
962
|
const useDebugger = (propsToWatch, id) => {
|
|
958
|
-
const prevPropsRef =
|
|
959
|
-
const uniqueId =
|
|
963
|
+
const prevPropsRef = React.useRef(propsToWatch);
|
|
964
|
+
const uniqueId = React.useMemo(() => {
|
|
960
965
|
// eslint-disable-next-line local-rules/no-typescript-assertion
|
|
961
966
|
let stackId = id || (propsToWatch && propsToWatch.id);
|
|
962
967
|
const stack = new Error().stack;
|
|
@@ -975,7 +980,7 @@ const useDebugger = (propsToWatch, id) => {
|
|
|
975
980
|
const isFirstRender = useIsFirstRender();
|
|
976
981
|
// eslint-disable-next-line no-console
|
|
977
982
|
console.log(isFirstRender ? "First-render" : "Re-render", uniqueId, window.location.pathname);
|
|
978
|
-
|
|
983
|
+
React.useEffect(() => {
|
|
979
984
|
const changedProps = Object.entries(propsToWatch || {}).reduce((result, [key, value]) => {
|
|
980
985
|
if (prevPropsRef.current && prevPropsRef.current[key] !== value) {
|
|
981
986
|
result[key + ""] = [prevPropsRef.current[key], value];
|
|
@@ -1027,8 +1032,8 @@ const Debugger = ({ id, logPropsChanges, stop, children, }) => {
|
|
|
1027
1032
|
new Error().stack?.split("\n")[2]?.trim() ||
|
|
1028
1033
|
"unknown-id";
|
|
1029
1034
|
useDebugger(logPropsChanges || {}, id);
|
|
1030
|
-
const uniqueIdRef =
|
|
1031
|
-
|
|
1035
|
+
const uniqueIdRef = React.useRef(uniqueId);
|
|
1036
|
+
React.useEffect(() => {
|
|
1032
1037
|
const currentUniqueId = uniqueIdRef.current;
|
|
1033
1038
|
// eslint-disable-next-line no-console
|
|
1034
1039
|
console.log(`${currentUniqueId} Debugger is mounting`);
|
package/index.esm.js
CHANGED
|
@@ -8,7 +8,7 @@ import { onError } from '@apollo/client/link/error';
|
|
|
8
8
|
import { MockLink, MockedProvider } from '@apollo/client/testing';
|
|
9
9
|
import { AssetSortByProperty, SortOrder } from '@trackunit/react-core-contexts-api';
|
|
10
10
|
import { doNothing as doNothing$1, objectValues, objectKeys } from '@trackunit/shared-utils';
|
|
11
|
-
import { createContext, useState, useEffect, useContext, Children, isValidElement, createElement, useRef, useCallback, useMemo } from 'react';
|
|
11
|
+
import React, { createContext, useState, useEffect, useContext, Children, isValidElement, createElement, useRef, useCallback, useMemo } from 'react';
|
|
12
12
|
import { useApolloClient } from '@apollo/client/react';
|
|
13
13
|
import { createMemoryHistory, createRootRoute, createRoute, createRouter, RouterProvider, Outlet } from '@tanstack/react-router';
|
|
14
14
|
import { GraphQLError } from 'graphql';
|
|
@@ -468,7 +468,12 @@ class TrackunitProvidersMockBuilder {
|
|
|
468
468
|
this.selectedAnalyticsContext = mockAnalyticsContext;
|
|
469
469
|
this.selectedOemBrandingContext = mockOemBrandingContext;
|
|
470
470
|
this.selectedUserSubscriptionContext = mockUserSubscriptionContext;
|
|
471
|
-
this.selectedFilterBarValues = {
|
|
471
|
+
this.selectedFilterBarValues = {
|
|
472
|
+
filterBarValues: {},
|
|
473
|
+
assetsFilterBarValues: {},
|
|
474
|
+
customersFilterBarValues: {},
|
|
475
|
+
sitesFilterBarValues: {},
|
|
476
|
+
};
|
|
472
477
|
}
|
|
473
478
|
/**
|
|
474
479
|
* Use this Analytics Context.
|
|
@@ -622,7 +627,7 @@ class TrackunitProvidersMockBuilder {
|
|
|
622
627
|
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
623
628
|
*/
|
|
624
629
|
filterBarValues(filterBarValues) {
|
|
625
|
-
this.selectedFilterBarValues = filterBarValues;
|
|
630
|
+
this.selectedFilterBarValues = { ...this.selectedFilterBarValues, ...filterBarValues };
|
|
626
631
|
return this;
|
|
627
632
|
}
|
|
628
633
|
/**
|
|
@@ -863,7 +868,7 @@ class TrackunitProvidersMockBuilder {
|
|
|
863
868
|
* @param testChildren - the child element being tested.
|
|
864
869
|
*/
|
|
865
870
|
getMockedCompositionRoot(testChildren) {
|
|
866
|
-
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:
|
|
871
|
+
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: 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 }) }) }) }) }) }) }) }) }) }) }) }) }) }) }));
|
|
867
872
|
}
|
|
868
873
|
getMockedCompositionRootWithRouter(testChildren) {
|
|
869
874
|
const childrenWithRouter = (jsx(RouterContainer, { addTestRootContainer: true, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: testChildren }));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@trackunit/react-core-contexts-test",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.72",
|
|
4
4
|
"repository": "https://github.com/Trackunit/manager",
|
|
5
5
|
"license": "SEE LICENSE IN LICENSE.txt",
|
|
6
6
|
"engines": {
|
|
@@ -13,9 +13,9 @@
|
|
|
13
13
|
"lodash": "4.17.21",
|
|
14
14
|
"graphql": "^16.10.0",
|
|
15
15
|
"@tanstack/react-router": "1.47.1",
|
|
16
|
-
"@trackunit/react-core-contexts-api": "1.4.
|
|
17
|
-
"@trackunit/react-core-hooks": "1.3.
|
|
18
|
-
"@trackunit/shared-utils": "1.5.
|
|
16
|
+
"@trackunit/react-core-contexts-api": "1.4.69",
|
|
17
|
+
"@trackunit/react-core-hooks": "1.3.71",
|
|
18
|
+
"@trackunit/shared-utils": "1.5.68"
|
|
19
19
|
},
|
|
20
20
|
"module": "./index.esm.js",
|
|
21
21
|
"main": "./index.cjs.js",
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { MockedResponse } from "@apollo/client/testing";
|
|
2
2
|
import { AnyRoute } from "@tanstack/react-router";
|
|
3
3
|
import { RenderResult } from "@testing-library/react";
|
|
4
|
-
import { AssetSortingContextValue, ConfirmationDialogContextValue, CurrentUserContextInterface, ErrorHandlingContextValue,
|
|
4
|
+
import { AssetSortingContextValue, ConfirmationDialogContextValue, CurrentUserContextInterface, ErrorHandlingContextValue, FilterBarContext, IAnalyticsContext, IEnvironmentContext, INavigationContext, IToastContext, ITokenContext, IUserPreferencesContext, IUserSubscriptionContext, ModalDialogContextValue, OemBrandingContext } from "@trackunit/react-core-contexts-api";
|
|
5
5
|
import { ReactElement, ReactNode } from "react";
|
|
6
6
|
import { MemoryRouterProps } from "./utils/routingUtils";
|
|
7
7
|
/**
|
|
@@ -30,7 +30,7 @@ export declare class TrackunitProvidersMockBuilder<T extends AnyRoute> {
|
|
|
30
30
|
protected selectedAnalyticsContext: IAnalyticsContext<Record<string, never>>;
|
|
31
31
|
protected selectedOemBrandingContext: OemBrandingContext;
|
|
32
32
|
protected selectedUserSubscriptionContext: IUserSubscriptionContext;
|
|
33
|
-
protected selectedFilterBarValues:
|
|
33
|
+
protected selectedFilterBarValues: FilterBarContext;
|
|
34
34
|
/**
|
|
35
35
|
* Use this Analytics Context.
|
|
36
36
|
* Defaults to mockAnalyticsContext.
|
|
@@ -163,7 +163,7 @@ export declare class TrackunitProvidersMockBuilder<T extends AnyRoute> {
|
|
|
163
163
|
* ...
|
|
164
164
|
* @returns { TrackunitProvidersMockBuilder } - The builder.
|
|
165
165
|
*/
|
|
166
|
-
filterBarValues(filterBarValues:
|
|
166
|
+
filterBarValues(filterBarValues: Partial<FilterBarContext>): this;
|
|
167
167
|
/**
|
|
168
168
|
* Use this to pass in a differerent current user subscription.
|
|
169
169
|
* Defaults to mockUserSubscriptionContext.
|