@trackunit/react-core-contexts-test 0.1.187 → 0.1.189

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.
@@ -6,11 +6,12 @@ var react = require('@testing-library/react');
6
6
  var pure = require('@testing-library/react/pure');
7
7
  require('@trackunit/react-core-hooks');
8
8
  require('lodash/omit');
9
- require('react-router-dom');
10
9
  require('@apollo/client');
11
10
  require('@apollo/client/link/error');
12
11
  require('@apollo/client/testing');
13
12
  require('@trackunit/react-core-contexts-api');
13
+ require('@apollo/client/react');
14
+ require('@tanstack/react-router');
14
15
  require('react');
15
16
  require('graphql');
16
17
 
@@ -4,11 +4,12 @@ import { act } from '@testing-library/react';
4
4
  import { renderHook } from '@testing-library/react/pure';
5
5
  import '@trackunit/react-core-hooks';
6
6
  import 'lodash/omit';
7
- import 'react-router-dom';
8
7
  import '@apollo/client';
9
8
  import '@apollo/client/link/error';
10
9
  import '@apollo/client/testing';
11
10
  import '@trackunit/react-core-contexts-api';
11
+ import '@apollo/client/react';
12
+ import '@tanstack/react-router';
12
13
  import 'react';
13
14
  import 'graphql';
14
15
 
package/index.cjs.js CHANGED
@@ -7,11 +7,12 @@ require('react/jsx-runtime');
7
7
  require('@testing-library/react');
8
8
  require('@trackunit/react-core-hooks');
9
9
  require('lodash/omit');
10
- require('react-router-dom');
11
10
  require('@apollo/client');
12
11
  require('@apollo/client/link/error');
13
12
  require('@apollo/client/testing');
14
13
  require('@trackunit/react-core-contexts-api');
14
+ require('@apollo/client/react');
15
+ require('@tanstack/react-router');
15
16
  require('react');
16
17
  require('graphql');
17
18
 
package/index.cjs2.js CHANGED
@@ -1,14 +1,15 @@
1
1
  'use strict';
2
2
 
3
3
  var jsxRuntime = require('react/jsx-runtime');
4
- var react = require('@testing-library/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
- var reactRouterDom = require('react-router-dom');
8
7
  var client = require('@apollo/client');
9
8
  var error = require('@apollo/client/link/error');
10
9
  var testing = require('@apollo/client/testing');
11
10
  var reactCoreContextsApi = require('@trackunit/react-core-contexts-api');
11
+ var react = require('@apollo/client/react');
12
+ var reactRouter = require('@tanstack/react-router');
12
13
  var React = require('react');
13
14
  var graphql = require('graphql');
14
15
 
@@ -227,9 +228,129 @@ const mockUserSubscriptionContext = {
227
228
  numberOfDaysWithAccessToHistoricalData: 30,
228
229
  numberOfDaysWithAccessToHistoricalInsights: 30,
229
230
  features: [],
231
+ loading: false,
230
232
  packageType: "EXPAND_FLEET_OWNER",
231
233
  };
232
234
 
235
+ const buildFlatRouteMap = (routes) => {
236
+ const routeMap = {};
237
+ routes.forEach(route => {
238
+ routeMap[route.id] = route;
239
+ if (Array.isArray(route.children)) {
240
+ Object.assign(routeMap, buildFlatRouteMap(route.children));
241
+ }
242
+ });
243
+ return routeMap;
244
+ };
245
+ /**
246
+ * This component is used to wrap the children of the RouterContainer to add a test root container
247
+ */
248
+ const RootRouteDebugger = () => {
249
+ // const matches = useMatches();
250
+ // console.log("matches", matches);
251
+ return jsxRuntime.jsx(reactRouter.Outlet, {});
252
+ };
253
+ /**
254
+ * This component is used to wrap the children of the RouterContainer to add a test root container.
255
+ *
256
+ * @param addTestRootContainer boolean to add test root container
257
+ * @param children children to be wrapped
258
+ * @returns React.ReactElement
259
+ */
260
+ const TestRenderChildren = ({ addTestRootContainer, children }) => {
261
+ return addTestRootContainer ? (jsxRuntime.jsx("div", { className: "inline-block h-[1000px] w-[1024px]", "data-testid": "testRoot", style: {
262
+ "--tw-scale-x": "0.99",
263
+ "--tw-scale-y": "0.99",
264
+ }, children: children })) : (jsxRuntime.jsx("div", { children: children }));
265
+ };
266
+ /**
267
+ * This component is used to wrap the children of the RouterContainer to add a test root container.
268
+ *
269
+ * @param addTestRootContainer boolean to add test root container
270
+ * @param selectedRouterProps selected router props
271
+ * @param rootRoute root route
272
+ * @param children children to be wrapped
273
+ * @returns React.ReactElement
274
+ */
275
+ const RouterContainer = ({ addTestRootContainer, selectedRouterProps, rootRoute, children, }) => {
276
+ const client = react.useApolloClient();
277
+ const memoryHistory = React.useMemo(() => {
278
+ var _a, _b;
279
+ return reactRouter.createMemoryHistory({
280
+ initialEntries: (_b = (_a = selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.initialEntries) === null || _a === void 0 ? void 0 : _a.map(entry => entry.path)) !== null && _b !== void 0 ? _b : ["/"],
281
+ initialIndex: 0,
282
+ });
283
+ }, [selectedRouterProps]);
284
+ const getChildren = React.useCallback(() => children, [children]);
285
+ const router = React.useMemo(() => {
286
+ var _a, _b;
287
+ if (!rootRoute) {
288
+ rootRoute = reactRouter.createRootRoute({ component: RootRouteDebugger });
289
+ const childRoute = reactRouter.createRoute({
290
+ path: "/",
291
+ getParentRoute: () => rootRoute,
292
+ component: () => (jsxRuntime.jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() })),
293
+ });
294
+ rootRoute.addChildren([childRoute]);
295
+ }
296
+ else {
297
+ const pathsToRoute = buildFlatRouteMap([rootRoute]);
298
+ if (pathsToRoute.__root__) {
299
+ pathsToRoute.__root__.options.component = () => jsxRuntime.jsx(RootRouteDebugger, {});
300
+ }
301
+ if (pathsToRoute["/"]) {
302
+ // This ensures / is not redirecting to default home route
303
+ pathsToRoute["/"].options.beforeLoad = () => { };
304
+ }
305
+ if (((_a = selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.initialEntries) === null || _a === void 0 ? void 0 : _a.length) || 0 > 0) {
306
+ (_b = selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.initialEntries) === null || _b === void 0 ? void 0 : _b.forEach(entry => {
307
+ const route = pathsToRoute[entry.route];
308
+ if (route) {
309
+ if (entry.component) {
310
+ route.options.component = entry.component;
311
+ }
312
+ else {
313
+ route.options.component = () => (jsxRuntime.jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() }));
314
+ }
315
+ }
316
+ });
317
+ }
318
+ else {
319
+ const slashRoute = pathsToRoute["/"];
320
+ if (slashRoute) {
321
+ slashRoute.options.component = () => (jsxRuntime.jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() }));
322
+ }
323
+ else {
324
+ const childRoute = reactRouter.createRoute({
325
+ path: "/",
326
+ getParentRoute: () => rootRoute,
327
+ component: () => (jsxRuntime.jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() })),
328
+ }).lazy(() => __awaiter(void 0, void 0, void 0, function* () {
329
+ return reactRouter.createLazyRoute("/")({
330
+ component: () => (jsxRuntime.jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() })),
331
+ });
332
+ }));
333
+ rootRoute.addChildren([childRoute]);
334
+ }
335
+ }
336
+ }
337
+ return reactRouter.createRouter({
338
+ routeTree: rootRoute,
339
+ history: memoryHistory,
340
+ context: {
341
+ hasAccessTo: () => __awaiter(void 0, void 0, void 0, function* () {
342
+ return true;
343
+ }),
344
+ client: null,
345
+ defaultUserRoute: "/",
346
+ isAuthenticated: true,
347
+ },
348
+ });
349
+ }, [rootRoute]);
350
+ const context = React.useMemo(() => (Object.assign({ hasAccessTo: () => __awaiter(void 0, void 0, void 0, function* () { return true; }), isAuthenticated: true, client, defaultUserRoute: "/" }, ((selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.context) || {}))), [client]);
351
+ return jsxRuntime.jsx(reactRouter.RouterProvider, { context: context, router: router });
352
+ };
353
+
233
354
  /**
234
355
  * Flushes all promises in the queue.
235
356
  * This is useful when testing async code.
@@ -257,7 +378,7 @@ const flushPromises = (waitTimeInMS = 0) => {
257
378
  * @returns {Promise<void>} - Returns a promise that resolves after the wait time.
258
379
  */
259
380
  const flushPromisesInAct = (waitTimeInMS = 0) => {
260
- return react.act(() => {
381
+ return react$1.act(() => {
261
382
  return new Promise(resolve => {
262
383
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
263
384
  if (global.ORG_setTimeout) {
@@ -286,7 +407,7 @@ class TrackunitProvidersMockBuilder {
286
407
  this.selectedNavigationContext = mockNavigationContext;
287
408
  this.selectedTokenContext = { token: "fakeToken" };
288
409
  this.selectedApolloMocks = [];
289
- this.selectedRouterProps = {};
410
+ this.selectedRouterProps = null;
290
411
  this.selectedToastContext = mockToastContext;
291
412
  this.selectedConfirmationDialogContext = mockConfirmationDialogContext;
292
413
  this.selectedAssetSortingContext = mockAssetSortingContext;
@@ -602,7 +723,7 @@ class TrackunitProvidersMockBuilder {
602
723
  /**
603
724
  * Use this Router Props with the given mocks.
604
725
  *
605
- * This is used to provide a MemoryRouter from lib "react-router-dom"
726
+ * This is used to provide a MemoryRouter from lib "@tanstack/react-router"
606
727
  *
607
728
  * @example
608
729
  * ...
@@ -623,7 +744,7 @@ class TrackunitProvidersMockBuilder {
623
744
  */
624
745
  routerProps(routerProps) {
625
746
  this.selectedRouterProps = routerProps;
626
- return this;
747
+ return this.rootRoute(routerProps.routeTree ? routerProps.routeTree : this.selectedRootRoute);
627
748
  }
628
749
  /**
629
750
  * Use this Manager Apollo Context Provider with the given mocks.
@@ -660,6 +781,10 @@ class TrackunitProvidersMockBuilder {
660
781
  validateSuppliedMocks() {
661
782
  return true;
662
783
  }
784
+ rootRoute(rootRoute) {
785
+ this.selectedRootRoute = rootRoute;
786
+ return this;
787
+ }
663
788
  /**
664
789
  * Make sure this represent the same structure as the main index.tsx does.
665
790
  *
@@ -667,10 +792,7 @@ class TrackunitProvidersMockBuilder {
667
792
  * @param addTestRootContainer - if you want to add a root container to the test.
668
793
  */
669
794
  getMockedCompositionRoot(testChildren, addTestRootContainer = true, forceDebugging = false) {
670
- return (jsxRuntime.jsx(reactCoreHooks.EnvironmentContextProvider, { value: this.selectedEnvironmentContext, children: jsxRuntime.jsx(reactRouterDom.MemoryRouter, Object.assign({}, this.selectedRouterProps, { 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, forceDebugging: forceDebugging, mocks: this.selectedApolloMocks, children: jsxRuntime.jsx(reactCoreHooks.NavigationContextProvider, { value: this.selectedNavigationContext, children: jsxRuntime.jsx(reactCoreHooks.CurrentUserPreferenceProvider, { value: this.selectedCurrentUserPreferenceContext, children: addTestRootContainer ? (jsxRuntime.jsx("div", { className: "inline-block h-[1000px] w-[1024px]", "data-testid": "testRoot", style: {
671
- "--tw-scale-x": "0.99",
672
- "--tw-scale-y": "0.99",
673
- }, children: testChildren })) : (jsxRuntime.jsx("div", { children: testChildren })) }) }) }) }) }) }) }) }) }) }) }) }) })) }));
795
+ return (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, forceDebugging: forceDebugging, 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(RouterContainer, { addTestRootContainer: addTestRootContainer, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: testChildren }) }) }) }) }) }) }) }) }) }) }) }) }) }));
674
796
  }
675
797
  /**
676
798
  * This will return the mocked composition root.
@@ -693,16 +815,16 @@ class TrackunitProvidersMockBuilder {
693
815
  return __awaiter(this, void 0, void 0, function* () {
694
816
  this.validateSuppliedMocks();
695
817
  let mountedcomponent;
696
- yield react.act(() => __awaiter(this, void 0, void 0, function* () {
697
- mountedcomponent = react.render(child, {
818
+ yield react$1.act(() => __awaiter(this, void 0, void 0, function* () {
819
+ mountedcomponent = react$1.render(child, {
698
820
  wrapper: ({ children }) => this.getMockedCompositionRoot(children),
699
821
  });
700
822
  yield flushPromises();
701
823
  }));
702
- yield react.act(() => __awaiter(this, void 0, void 0, function* () {
824
+ yield react$1.act(() => __awaiter(this, void 0, void 0, function* () {
703
825
  yield flushPromises();
704
826
  }));
705
- yield react.act(() => __awaiter(this, void 0, void 0, function* () {
827
+ yield react$1.act(() => __awaiter(this, void 0, void 0, function* () {
706
828
  yield flushPromises();
707
829
  }));
708
830
  return mountedcomponent;
@@ -751,7 +873,7 @@ const useDebugger = (propsToWatch, id) => {
751
873
  return stackId || "unknown-id";
752
874
  }, [id]);
753
875
  // eslint-disable-next-line no-console
754
- console.log("Rerender", uniqueId);
876
+ console.log("Rerender", uniqueId, window.location.pathname);
755
877
  React.useEffect(() => {
756
878
  const changedProps = Object.entries(propsToWatch || {}).reduce((result, [key, value]) => {
757
879
  if (prevPropsRef.current && prevPropsRef.current[key] !== value) {
package/index.esm.js CHANGED
@@ -3,10 +3,11 @@ import 'react/jsx-runtime';
3
3
  import '@testing-library/react';
4
4
  import '@trackunit/react-core-hooks';
5
5
  import 'lodash/omit';
6
- import 'react-router-dom';
7
6
  import '@apollo/client';
8
7
  import '@apollo/client/link/error';
9
8
  import '@apollo/client/testing';
10
9
  import '@trackunit/react-core-contexts-api';
10
+ import '@apollo/client/react';
11
+ import '@tanstack/react-router';
11
12
  import 'react';
12
13
  import 'graphql';
package/index.esm2.js CHANGED
@@ -1,13 +1,14 @@
1
1
  import { jsx } from 'react/jsx-runtime';
2
2
  import { act, render } from '@testing-library/react';
3
- import { EnvironmentContextProvider, CurrentUserProvider, AnalyticsContext, UserSubscriptionProvider, OemBrandingContextProvider, TokenProvider, ToastProvider, ConfirmationDialogProvider, FilterBarProvider, AssetSortingProvider, NavigationContextProvider, CurrentUserPreferenceProvider } from '@trackunit/react-core-hooks';
3
+ import { CurrentUserProvider, AnalyticsContext, UserSubscriptionProvider, OemBrandingContextProvider, TokenProvider, ToastProvider, ConfirmationDialogProvider, FilterBarProvider, AssetSortingProvider, NavigationContextProvider, CurrentUserPreferenceProvider, EnvironmentContextProvider } from '@trackunit/react-core-hooks';
4
4
  import omit from 'lodash/omit';
5
- import { MemoryRouter } from 'react-router-dom';
6
5
  import { ApolloLink } from '@apollo/client';
7
6
  import { onError } from '@apollo/client/link/error';
8
7
  import { MockLink, MockedProvider } from '@apollo/client/testing';
9
8
  import { AssetSortByProperty, SortOrder } from '@trackunit/react-core-contexts-api';
10
- import React, { useRef, useMemo, useEffect } from 'react';
9
+ import { useApolloClient } from '@apollo/client/react';
10
+ import { createMemoryHistory, createRootRoute, createRoute, createLazyRoute, createRouter, RouterProvider, Outlet } from '@tanstack/react-router';
11
+ import React, { useMemo, useCallback, useRef, useEffect } from 'react';
11
12
  import { GraphQLError } from 'graphql';
12
13
 
13
14
  /******************************************************************************
@@ -220,9 +221,129 @@ const mockUserSubscriptionContext = {
220
221
  numberOfDaysWithAccessToHistoricalData: 30,
221
222
  numberOfDaysWithAccessToHistoricalInsights: 30,
222
223
  features: [],
224
+ loading: false,
223
225
  packageType: "EXPAND_FLEET_OWNER",
224
226
  };
225
227
 
228
+ const buildFlatRouteMap = (routes) => {
229
+ const routeMap = {};
230
+ routes.forEach(route => {
231
+ routeMap[route.id] = route;
232
+ if (Array.isArray(route.children)) {
233
+ Object.assign(routeMap, buildFlatRouteMap(route.children));
234
+ }
235
+ });
236
+ return routeMap;
237
+ };
238
+ /**
239
+ * This component is used to wrap the children of the RouterContainer to add a test root container
240
+ */
241
+ const RootRouteDebugger = () => {
242
+ // const matches = useMatches();
243
+ // console.log("matches", matches);
244
+ return jsx(Outlet, {});
245
+ };
246
+ /**
247
+ * This component is used to wrap the children of the RouterContainer to add a test root container.
248
+ *
249
+ * @param addTestRootContainer boolean to add test root container
250
+ * @param children children to be wrapped
251
+ * @returns React.ReactElement
252
+ */
253
+ const TestRenderChildren = ({ addTestRootContainer, children }) => {
254
+ return addTestRootContainer ? (jsx("div", { className: "inline-block h-[1000px] w-[1024px]", "data-testid": "testRoot", style: {
255
+ "--tw-scale-x": "0.99",
256
+ "--tw-scale-y": "0.99",
257
+ }, children: children })) : (jsx("div", { children: children }));
258
+ };
259
+ /**
260
+ * This component is used to wrap the children of the RouterContainer to add a test root container.
261
+ *
262
+ * @param addTestRootContainer boolean to add test root container
263
+ * @param selectedRouterProps selected router props
264
+ * @param rootRoute root route
265
+ * @param children children to be wrapped
266
+ * @returns React.ReactElement
267
+ */
268
+ const RouterContainer = ({ addTestRootContainer, selectedRouterProps, rootRoute, children, }) => {
269
+ const client = useApolloClient();
270
+ const memoryHistory = useMemo(() => {
271
+ var _a, _b;
272
+ return createMemoryHistory({
273
+ initialEntries: (_b = (_a = selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.initialEntries) === null || _a === void 0 ? void 0 : _a.map(entry => entry.path)) !== null && _b !== void 0 ? _b : ["/"],
274
+ initialIndex: 0,
275
+ });
276
+ }, [selectedRouterProps]);
277
+ const getChildren = useCallback(() => children, [children]);
278
+ const router = useMemo(() => {
279
+ var _a, _b;
280
+ if (!rootRoute) {
281
+ rootRoute = createRootRoute({ component: RootRouteDebugger });
282
+ const childRoute = createRoute({
283
+ path: "/",
284
+ getParentRoute: () => rootRoute,
285
+ component: () => (jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() })),
286
+ });
287
+ rootRoute.addChildren([childRoute]);
288
+ }
289
+ else {
290
+ const pathsToRoute = buildFlatRouteMap([rootRoute]);
291
+ if (pathsToRoute.__root__) {
292
+ pathsToRoute.__root__.options.component = () => jsx(RootRouteDebugger, {});
293
+ }
294
+ if (pathsToRoute["/"]) {
295
+ // This ensures / is not redirecting to default home route
296
+ pathsToRoute["/"].options.beforeLoad = () => { };
297
+ }
298
+ if (((_a = selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.initialEntries) === null || _a === void 0 ? void 0 : _a.length) || 0 > 0) {
299
+ (_b = selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.initialEntries) === null || _b === void 0 ? void 0 : _b.forEach(entry => {
300
+ const route = pathsToRoute[entry.route];
301
+ if (route) {
302
+ if (entry.component) {
303
+ route.options.component = entry.component;
304
+ }
305
+ else {
306
+ route.options.component = () => (jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() }));
307
+ }
308
+ }
309
+ });
310
+ }
311
+ else {
312
+ const slashRoute = pathsToRoute["/"];
313
+ if (slashRoute) {
314
+ slashRoute.options.component = () => (jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() }));
315
+ }
316
+ else {
317
+ const childRoute = createRoute({
318
+ path: "/",
319
+ getParentRoute: () => rootRoute,
320
+ component: () => (jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() })),
321
+ }).lazy(() => __awaiter(void 0, void 0, void 0, function* () {
322
+ return createLazyRoute("/")({
323
+ component: () => (jsx(TestRenderChildren, { addTestRootContainer: addTestRootContainer, children: getChildren() })),
324
+ });
325
+ }));
326
+ rootRoute.addChildren([childRoute]);
327
+ }
328
+ }
329
+ }
330
+ return createRouter({
331
+ routeTree: rootRoute,
332
+ history: memoryHistory,
333
+ context: {
334
+ hasAccessTo: () => __awaiter(void 0, void 0, void 0, function* () {
335
+ return true;
336
+ }),
337
+ client: null,
338
+ defaultUserRoute: "/",
339
+ isAuthenticated: true,
340
+ },
341
+ });
342
+ }, [rootRoute]);
343
+ const context = useMemo(() => (Object.assign({ hasAccessTo: () => __awaiter(void 0, void 0, void 0, function* () { return true; }), isAuthenticated: true, client, defaultUserRoute: "/" }, ((selectedRouterProps === null || selectedRouterProps === void 0 ? void 0 : selectedRouterProps.context) || {}))), [client]);
344
+ return jsx(RouterProvider, { context: context, router: router });
345
+ };
346
+
226
347
  /**
227
348
  * Flushes all promises in the queue.
228
349
  * This is useful when testing async code.
@@ -279,7 +400,7 @@ class TrackunitProvidersMockBuilder {
279
400
  this.selectedNavigationContext = mockNavigationContext;
280
401
  this.selectedTokenContext = { token: "fakeToken" };
281
402
  this.selectedApolloMocks = [];
282
- this.selectedRouterProps = {};
403
+ this.selectedRouterProps = null;
283
404
  this.selectedToastContext = mockToastContext;
284
405
  this.selectedConfirmationDialogContext = mockConfirmationDialogContext;
285
406
  this.selectedAssetSortingContext = mockAssetSortingContext;
@@ -595,7 +716,7 @@ class TrackunitProvidersMockBuilder {
595
716
  /**
596
717
  * Use this Router Props with the given mocks.
597
718
  *
598
- * This is used to provide a MemoryRouter from lib "react-router-dom"
719
+ * This is used to provide a MemoryRouter from lib "@tanstack/react-router"
599
720
  *
600
721
  * @example
601
722
  * ...
@@ -616,7 +737,7 @@ class TrackunitProvidersMockBuilder {
616
737
  */
617
738
  routerProps(routerProps) {
618
739
  this.selectedRouterProps = routerProps;
619
- return this;
740
+ return this.rootRoute(routerProps.routeTree ? routerProps.routeTree : this.selectedRootRoute);
620
741
  }
621
742
  /**
622
743
  * Use this Manager Apollo Context Provider with the given mocks.
@@ -653,6 +774,10 @@ class TrackunitProvidersMockBuilder {
653
774
  validateSuppliedMocks() {
654
775
  return true;
655
776
  }
777
+ rootRoute(rootRoute) {
778
+ this.selectedRootRoute = rootRoute;
779
+ return this;
780
+ }
656
781
  /**
657
782
  * Make sure this represent the same structure as the main index.tsx does.
658
783
  *
@@ -660,10 +785,7 @@ class TrackunitProvidersMockBuilder {
660
785
  * @param addTestRootContainer - if you want to add a root container to the test.
661
786
  */
662
787
  getMockedCompositionRoot(testChildren, addTestRootContainer = true, forceDebugging = false) {
663
- return (jsx(EnvironmentContextProvider, { value: this.selectedEnvironmentContext, children: jsx(MemoryRouter, Object.assign({}, this.selectedRouterProps, { 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, forceDebugging: forceDebugging, mocks: this.selectedApolloMocks, children: jsx(NavigationContextProvider, { value: this.selectedNavigationContext, children: jsx(CurrentUserPreferenceProvider, { value: this.selectedCurrentUserPreferenceContext, children: addTestRootContainer ? (jsx("div", { className: "inline-block h-[1000px] w-[1024px]", "data-testid": "testRoot", style: {
664
- "--tw-scale-x": "0.99",
665
- "--tw-scale-y": "0.99",
666
- }, children: testChildren })) : (jsx("div", { children: testChildren })) }) }) }) }) }) }) }) }) }) }) }) }) })) }));
788
+ return (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, forceDebugging: forceDebugging, mocks: this.selectedApolloMocks, children: jsx(NavigationContextProvider, { value: this.selectedNavigationContext, children: jsx(CurrentUserPreferenceProvider, { value: this.selectedCurrentUserPreferenceContext, children: jsx(EnvironmentContextProvider, { value: this.selectedEnvironmentContext, children: jsx(RouterContainer, { addTestRootContainer: addTestRootContainer, rootRoute: this.selectedRootRoute, selectedRouterProps: this.selectedRouterProps, children: testChildren }) }) }) }) }) }) }) }) }) }) }) }) }) }));
667
789
  }
668
790
  /**
669
791
  * This will return the mocked composition root.
@@ -744,7 +866,7 @@ const useDebugger = (propsToWatch, id) => {
744
866
  return stackId || "unknown-id";
745
867
  }, [id]);
746
868
  // eslint-disable-next-line no-console
747
- console.log("Rerender", uniqueId);
869
+ console.log("Rerender", uniqueId, window.location.pathname);
748
870
  useEffect(() => {
749
871
  const changedProps = Object.entries(propsToWatch || {}).reduce((result, [key, value]) => {
750
872
  if (prevPropsRef.current && prevPropsRef.current[key] !== value) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts-test",
3
- "version": "0.1.187",
3
+ "version": "0.1.189",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
@@ -13,8 +13,8 @@
13
13
  "@trackunit/react-core-contexts-api": "*",
14
14
  "@trackunit/react-core-hooks": "*",
15
15
  "lodash": "4.17.21",
16
- "react-router-dom": "6.18.0",
17
- "graphql": "^15.8.0"
16
+ "graphql": "^15.8.0",
17
+ "@tanstack/react-router": "1.19.1"
18
18
  },
19
19
  "module": "./index.esm.js",
20
20
  "main": "./index.cjs.js"
@@ -1,8 +1,17 @@
1
1
  import { MockedResponse } from "@apollo/client/testing";
2
+ import { AnyRoute, RegisteredRouter, RouteIds } from "@tanstack/react-router";
2
3
  import { RenderResult } from "@testing-library/react";
3
4
  import { ConfirmationDialogContextValue, FilterBarValues, IAnalyticsContext, IAssetSortingContext, ICurrentUserContext, IEnvironmentContext, INavigationContext, IOemBrandingContext, IToastContext, ITokenContext, IUserPreferencesContext, IUserSubscriptionContext } from "@trackunit/react-core-contexts-api";
4
5
  import * as React from "react";
5
- import { MemoryRouterProps } from "react-router-dom";
6
+ export type MemoryRouterProps<T extends AnyRoute> = {
7
+ routeTree?: T;
8
+ initialEntries?: {
9
+ route: RouteIds<RegisteredRouter["routeTree"]>;
10
+ path: string;
11
+ component?: () => JSX.Element | null;
12
+ }[];
13
+ context?: Record<string, unknown>;
14
+ };
6
15
  /**
7
16
  * This builder allows you to enable trackunit providers using the builder pattern, and then call 1 of either:
8
17
  * For React Components:
@@ -12,12 +21,13 @@ import { MemoryRouterProps } from "react-router-dom";
12
21
  * For Storybook:
13
22
  * - storybook
14
23
  */
15
- export declare class TrackunitProvidersMockBuilder {
24
+ export declare class TrackunitProvidersMockBuilder<T extends AnyRoute> {
16
25
  protected selectedEnvironmentContext: IEnvironmentContext;
17
26
  protected selectedNavigationContext: INavigationContext;
18
27
  protected selectedTokenContext: ITokenContext;
19
28
  protected selectedApolloMocks: MockedResponse[];
20
- protected selectedRouterProps: MemoryRouterProps;
29
+ protected selectedRouterProps: MemoryRouterProps<T> | null;
30
+ protected selectedRootRoute: AnyRoute | null;
21
31
  protected selectedToastContext: IToastContext;
22
32
  protected selectedConfirmationDialogContext: ConfirmationDialogContextValue;
23
33
  protected selectedAssetSortingContext: IAssetSortingContext;
@@ -289,7 +299,7 @@ export declare class TrackunitProvidersMockBuilder {
289
299
  /**
290
300
  * Use this Router Props with the given mocks.
291
301
  *
292
- * This is used to provide a MemoryRouter from lib "react-router-dom"
302
+ * This is used to provide a MemoryRouter from lib "@tanstack/react-router"
293
303
  *
294
304
  * @example
295
305
  * ...
@@ -308,7 +318,7 @@ export declare class TrackunitProvidersMockBuilder {
308
318
  * @param routerProps - The router props to use.
309
319
  * @returns { TrackunitProvidersMockBuilder } - The builder.
310
320
  */
311
- routerProps(routerProps: MemoryRouterProps): this;
321
+ routerProps(routerProps: MemoryRouterProps<T>): this;
312
322
  /**
313
323
  * Use this Manager Apollo Context Provider with the given mocks.
314
324
  *
@@ -339,6 +349,7 @@ export declare class TrackunitProvidersMockBuilder {
339
349
  * @returns {boolean} true or throws error if any invalid mocks
340
350
  */
341
351
  protected validateSuppliedMocks(): true | never;
352
+ protected rootRoute(rootRoute: AnyRoute | null): this;
342
353
  /**
343
354
  * Make sure this represent the same structure as the main index.tsx does.
344
355
  *
@@ -365,4 +376,4 @@ export declare class TrackunitProvidersMockBuilder {
365
376
  /**
366
377
  * This is the default mock builder for the TrackunitProviders.
367
378
  */
368
- export declare const trackunitProviders: () => TrackunitProvidersMockBuilder;
379
+ export declare const trackunitProviders: () => TrackunitProvidersMockBuilder<AnyRoute>;
@@ -0,0 +1,38 @@
1
+ import { AnyRoute } from "@tanstack/react-router";
2
+ import React from "react";
3
+ import { MemoryRouterProps } from "../TrackunitProvidersMockBuilder";
4
+ /**
5
+ * This component is used to wrap the children of the RouterContainer to add a test root container
6
+ */
7
+ export declare const RootRouteDebugger: () => JSX.Element;
8
+ /**
9
+ * This component is used to wrap the children of the RouterContainer to add a test root container
10
+ */
11
+ export interface TestRenderChildrenProps {
12
+ addTestRootContainer: boolean;
13
+ children: React.ReactNode;
14
+ }
15
+ /**
16
+ * This component is used to wrap the children of the RouterContainer to add a test root container.
17
+ *
18
+ * @param addTestRootContainer boolean to add test root container
19
+ * @param children children to be wrapped
20
+ * @returns React.ReactElement
21
+ */
22
+ export declare const TestRenderChildren: ({ addTestRootContainer, children }: TestRenderChildrenProps) => JSX.Element;
23
+ export interface RouterContainerProps<T extends AnyRoute = AnyRoute> {
24
+ addTestRootContainer: boolean;
25
+ rootRoute?: T | null;
26
+ selectedRouterProps: MemoryRouterProps<T> | null;
27
+ children: React.ReactNode;
28
+ }
29
+ /**
30
+ * This component is used to wrap the children of the RouterContainer to add a test root container.
31
+ *
32
+ * @param addTestRootContainer boolean to add test root container
33
+ * @param selectedRouterProps selected router props
34
+ * @param rootRoute root route
35
+ * @param children children to be wrapped
36
+ * @returns React.ReactElement
37
+ */
38
+ export declare const RouterContainer: ({ addTestRootContainer, selectedRouterProps, rootRoute, children, }: RouterContainerProps) => React.ReactElement;