File tree 4 files changed +56
-3
lines changed
4 files changed +56
-3
lines changed Original file line number Diff line number Diff line change @@ -28,5 +28,6 @@ export {
28
28
RoutingContext ,
29
29
TestRoutingContext ,
30
30
useInRoutingContext ,
31
+ useInTestRoutingContext ,
31
32
useActiveRouteEvents ,
32
33
} from "./providers" ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { RoutingEvent } from "./routingEvent";
5
5
6
6
type Context = {
7
7
activeRouteEvents ?: MutableRefObject < RoutingEvent < any > [ ] > ;
8
+ isTestRoutingContext ?: boolean ;
8
9
} ;
9
10
10
11
export const RoutingContext = createContext < Context | undefined > ( undefined ) ;
@@ -30,6 +31,15 @@ export function useInRoutingContext(): boolean {
30
31
return context !== undefined ;
31
32
}
32
33
34
+ /**
35
+ * @private
36
+ */
37
+ export function useInTestRoutingContext ( ) : boolean {
38
+ const context = useContext ( RoutingContext ) ;
39
+
40
+ return context ?. isTestRoutingContext ?? false ;
41
+ }
42
+
33
43
/**
34
44
* @public
35
45
*
@@ -62,7 +72,10 @@ export function TestRoutingContext({
62
72
} ) {
63
73
return (
64
74
< RoutingContext . Provider
65
- value = { { activeRouteEvents : { current : activeRouteEvents } } }
75
+ value = { {
76
+ activeRouteEvents : { current : activeRouteEvents } ,
77
+ isTestRoutingContext : true ,
78
+ } }
66
79
>
67
80
{ children }
68
81
</ RoutingContext . Provider >
Original file line number Diff line number Diff line change @@ -11,6 +11,7 @@ import {
11
11
buildActions ,
12
12
createXStateTreeMachine ,
13
13
} from "./builders" ;
14
+ import { TestRoutingContext } from "./routing" ;
14
15
import { singleSlot } from "./slots" ;
15
16
import { delay } from "./utils" ;
16
17
import {
@@ -337,6 +338,38 @@ describe("xstate-tree", () => {
337
338
throw new Error ( "Should have thrown" ) ;
338
339
} ) ;
339
340
341
+ it ( "does not throw an error if it's inside a test routing context" , async ( ) => {
342
+ const machine = createMachine ( {
343
+ id : "test" ,
344
+ initial : "idle" ,
345
+ states : {
346
+ idle : { } ,
347
+ } ,
348
+ } ) ;
349
+
350
+ const RootMachine = createXStateTreeMachine ( machine , {
351
+ View ( ) {
352
+ return < p > I am root</ p > ;
353
+ } ,
354
+ } ) ;
355
+ const Root = buildRootComponent ( RootMachine , {
356
+ basePath : "/" ,
357
+ history : createMemoryHistory ( ) ,
358
+ routes : [ ] ,
359
+ } ) ;
360
+
361
+ const { rerender } = render (
362
+ < TestRoutingContext activeRouteEvents = { [ ] } >
363
+ < Root />
364
+ </ TestRoutingContext >
365
+ ) ;
366
+ rerender (
367
+ < TestRoutingContext activeRouteEvents = { [ ] } >
368
+ < Root />
369
+ </ TestRoutingContext >
370
+ ) ;
371
+ } ) ;
372
+
340
373
it ( "does not throw an error if either or one are a routing root" , async ( ) => {
341
374
const machine = createMachine ( {
342
375
id : "test" ,
Original file line number Diff line number Diff line change @@ -24,8 +24,9 @@ import {
24
24
RoutingEvent ,
25
25
SharedMeta ,
26
26
useInRoutingContext ,
27
+ useInTestRoutingContext ,
28
+ useActiveRouteEvents ,
27
29
} from "./routing" ;
28
- import { useActiveRouteEvents } from "./routing/providers" ;
29
30
import { GetSlotNames , Slot } from "./slots" ;
30
31
import { GlobalEvents , AnyXstateTreeMachine , XstateTreeHistory } from "./types" ;
31
32
import { useConstant } from "./useConstant" ;
@@ -357,7 +358,12 @@ export function buildRootComponent(
357
358
activeRouteEventsRef . current = events ;
358
359
} ;
359
360
const insideRoutingContext = useInRoutingContext ( ) ;
360
- if ( insideRoutingContext && typeof routing !== "undefined" ) {
361
+ const inTestRoutingContext = useInTestRoutingContext ( ) ;
362
+ if (
363
+ ! inTestRoutingContext &&
364
+ insideRoutingContext &&
365
+ typeof routing !== "undefined"
366
+ ) {
361
367
const m =
362
368
"Routing root rendered inside routing context, this implies a bug" ;
363
369
if ( process . env . NODE_ENV !== "production" ) {
You can’t perform that action at this time.
0 commit comments