@trackunit/react-core-contexts-test 1.19.9 → 1.20.0

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/index2.cjs.js CHANGED
@@ -13716,6 +13716,8 @@ const TestErrorComponent = ({ error }) => {
13716
13716
  /**
13717
13717
  * This component is used to wrap the children of the RouterContainer to add a test root container.
13718
13718
  *
13719
+ * History defaults to `createMemoryHistory()` (no blocking). See {@link MemoryRouterProps.history}.
13720
+ *
13719
13721
  * @param props - The component props.
13720
13722
  * @param props.addTestRootContainer - Boolean to add test root container.
13721
13723
  * @param props.selectedRouterProps - Selected router props.
@@ -13725,11 +13727,30 @@ const TestErrorComponent = ({ error }) => {
13725
13727
  */
13726
13728
  const RouterContainer = ({ addTestRootContainer, selectedRouterProps, rootRoute, children, }) => {
13727
13729
  const client = react.useApolloClient();
13728
- const memoryHistory = React.useRef(reactRouter.createMemoryHistory({
13729
- initialEntries: selectedRouterProps?.initialEntries
13730
- ? [...selectedRouterProps.initialEntries].reverse().map(entry => entry.path)
13731
- : ["/"],
13732
- }));
13730
+ const historyOverride = selectedRouterProps?.history;
13731
+ // Seed the override's start path, like `initialEntries` does for memory history. Runs once
13732
+ // per mount, during render, so the router memo below snapshots it — an effect runs too late.
13733
+ const hasSeededStartPath = React.useRef(false);
13734
+ if (historyOverride && typeof window !== "undefined" && !hasSeededStartPath.current) {
13735
+ const startPath = selectedRouterProps.initialEntries?.[0]?.path ?? "/";
13736
+ window.history.replaceState({ __TSR_index: 0, key: "test-start" }, "", startPath);
13737
+ hasSeededStartPath.current = true;
13738
+ }
13739
+ // Destroy the override once on unmount so its pushState/replaceState patches do not leak.
13740
+ // A ref keeps this effect from re-running when the prop identity changes.
13741
+ const historyOverrideRef = React.useRef(historyOverride);
13742
+ React.useEffect(() => {
13743
+ const override = historyOverrideRef.current;
13744
+ return () => {
13745
+ override?.destroy();
13746
+ };
13747
+ }, []);
13748
+ const memoryHistory = React.useRef(historyOverride ??
13749
+ reactRouter.createMemoryHistory({
13750
+ initialEntries: selectedRouterProps?.initialEntries
13751
+ ? [...selectedRouterProps.initialEntries].reverse().map(entry => entry.path)
13752
+ : ["/"],
13753
+ }));
13733
13754
  const addTestRootContainerRef = React.useRef(addTestRootContainer);
13734
13755
  React.useEffect(() => {
13735
13756
  addTestRootContainerRef.current = addTestRootContainer;
package/index2.esm.js CHANGED
@@ -13694,6 +13694,8 @@ const TestErrorComponent = ({ error }) => {
13694
13694
  /**
13695
13695
  * This component is used to wrap the children of the RouterContainer to add a test root container.
13696
13696
  *
13697
+ * History defaults to `createMemoryHistory()` (no blocking). See {@link MemoryRouterProps.history}.
13698
+ *
13697
13699
  * @param props - The component props.
13698
13700
  * @param props.addTestRootContainer - Boolean to add test root container.
13699
13701
  * @param props.selectedRouterProps - Selected router props.
@@ -13703,11 +13705,30 @@ const TestErrorComponent = ({ error }) => {
13703
13705
  */
13704
13706
  const RouterContainer = ({ addTestRootContainer, selectedRouterProps, rootRoute, children, }) => {
13705
13707
  const client = useApolloClient();
13706
- const memoryHistory = useRef(createMemoryHistory({
13707
- initialEntries: selectedRouterProps?.initialEntries
13708
- ? [...selectedRouterProps.initialEntries].reverse().map(entry => entry.path)
13709
- : ["/"],
13710
- }));
13708
+ const historyOverride = selectedRouterProps?.history;
13709
+ // Seed the override's start path, like `initialEntries` does for memory history. Runs once
13710
+ // per mount, during render, so the router memo below snapshots it — an effect runs too late.
13711
+ const hasSeededStartPath = useRef(false);
13712
+ if (historyOverride && typeof window !== "undefined" && !hasSeededStartPath.current) {
13713
+ const startPath = selectedRouterProps.initialEntries?.[0]?.path ?? "/";
13714
+ window.history.replaceState({ __TSR_index: 0, key: "test-start" }, "", startPath);
13715
+ hasSeededStartPath.current = true;
13716
+ }
13717
+ // Destroy the override once on unmount so its pushState/replaceState patches do not leak.
13718
+ // A ref keeps this effect from re-running when the prop identity changes.
13719
+ const historyOverrideRef = useRef(historyOverride);
13720
+ useEffect(() => {
13721
+ const override = historyOverrideRef.current;
13722
+ return () => {
13723
+ override?.destroy();
13724
+ };
13725
+ }, []);
13726
+ const memoryHistory = useRef(historyOverride ??
13727
+ createMemoryHistory({
13728
+ initialEntries: selectedRouterProps?.initialEntries
13729
+ ? [...selectedRouterProps.initialEntries].reverse().map(entry => entry.path)
13730
+ : ["/"],
13731
+ }));
13711
13732
  const addTestRootContainerRef = useRef(addTestRootContainer);
13712
13733
  useEffect(() => {
13713
13734
  addTestRootContainerRef.current = addTestRootContainer;
package/package.json CHANGED
@@ -1,16 +1,16 @@
1
1
  {
2
2
  "name": "@trackunit/react-core-contexts-test",
3
- "version": "1.19.9",
3
+ "version": "1.20.0",
4
4
  "repository": "https://github.com/Trackunit/manager",
5
5
  "license": "SEE LICENSE IN LICENSE.txt",
6
6
  "engines": {
7
7
  "node": ">=24.x"
8
8
  },
9
9
  "dependencies": {
10
- "@trackunit/shared-utils": "1.16.57",
10
+ "@trackunit/shared-utils": "1.17.0",
11
11
  "@tanstack/router-core": "1.114.29",
12
- "@trackunit/iris-app-runtime-core-api": "1.18.9",
13
- "@trackunit/react-core-contexts-api": "1.19.9",
12
+ "@trackunit/iris-app-runtime-core-api": "1.19.0",
13
+ "@trackunit/react-core-contexts-api": "1.20.0",
14
14
  "es-toolkit": "^1.39.10"
15
15
  },
16
16
  "peerDependencies": {
@@ -1,4 +1,4 @@
1
- import { AnyRoute, RegisteredRouter, RouteIds } from "@tanstack/react-router";
1
+ import { AnyRoute, RegisteredRouter, RouteIds, RouterHistory } from "@tanstack/react-router";
2
2
  import { ReactElement, ReactNode } from "react";
3
3
  export type MemoryRouterProps<TRouterTree extends AnyRoute = RegisteredRouter["routeTree"]> = {
4
4
  routeTree?: TRouterTree;
@@ -9,6 +9,24 @@ export type MemoryRouterProps<TRouterTree extends AnyRoute = RegisteredRouter["r
9
9
  beforeLoad?: () => void;
10
10
  }>;
11
11
  context?: Record<string, unknown>;
12
+ /**
13
+ * Override the router history. Defaults to `createMemoryHistory()`, which never wires
14
+ * blockers, so `useBlocker` / `onBeforeClose` guards are no-ops. Pass
15
+ * `createBrowserHistory()` to assert a blocked navigation stays blocked.
16
+ *
17
+ * When set, the harness starts it at the first `initialEntries` path (or `/`) and calls
18
+ * `history.destroy()` on unmount. No extra per-test cleanup is needed.
19
+ *
20
+ * @example
21
+ * ```ts
22
+ * import { createBrowserHistory } from "@tanstack/react-router";
23
+ *
24
+ * await trackunitProviders()
25
+ * .routerProps({ history: createBrowserHistory() })
26
+ * .render(<ComponentThatBlocksNavigation />);
27
+ * ```
28
+ */
29
+ history?: RouterHistory;
12
30
  };
13
31
  /**
14
32
  * This component is used to wrap the children of the RouterContainer to add a test root container
@@ -26,6 +44,8 @@ export interface RouterContainerProps<TRoute extends AnyRoute = AnyRoute> {
26
44
  /**
27
45
  * This component is used to wrap the children of the RouterContainer to add a test root container.
28
46
  *
47
+ * History defaults to `createMemoryHistory()` (no blocking). See {@link MemoryRouterProps.history}.
48
+ *
29
49
  * @param props - The component props.
30
50
  * @param props.addTestRootContainer - Boolean to add test root container.
31
51
  * @param props.selectedRouterProps - Selected router props.
@@ -1 +0,0 @@
1
- {"version":3,"file":"entry.js","sourceRoot":"","sources":["../../../../../libs/react/core-contexts-test/migrations/entry.ts"],"names":[],"mappings":"","sourcesContent":["export {};\n"]}