@@ -75,6 +75,57 @@ describe("useRoutes", () => {
75
75
` ) ;
76
76
} ) ;
77
77
78
+ it ( "returns null when no route matches" , ( ) => {
79
+ let routes = [ { path : "one" , element : < h1 > one</ h1 > } ] ;
80
+
81
+ const NullRenderer = ( props : { routes : RouteObject [ ] } ) => {
82
+ const element = useRoutes ( props . routes ) ;
83
+ return element === null ? < div > is null</ div > : < div > is not null</ div > ;
84
+ } ;
85
+
86
+ let renderer : TestRenderer . ReactTestRenderer ;
87
+ TestRenderer . act ( ( ) => {
88
+ renderer = TestRenderer . create (
89
+ < MemoryRouter initialEntries = { [ "/two" ] } >
90
+ < NullRenderer routes = { routes } />
91
+ </ MemoryRouter >
92
+ ) ;
93
+ } ) ;
94
+
95
+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `
96
+ <div>
97
+ is null
98
+ </div>
99
+ ` ) ;
100
+ } ) ;
101
+
102
+ it ( "returns null when no route matches and a `location` prop is passed" , ( ) => {
103
+ let routes = [ { path : "one" , element : < h1 > one</ h1 > } ] ;
104
+
105
+ const NullRenderer = ( props : {
106
+ routes : RouteObject [ ] ;
107
+ location ?: Partial < Location > & { pathname : string } ;
108
+ } ) => {
109
+ const element = useRoutes ( props . routes , props . location ) ;
110
+ return element === null ? < div > is null</ div > : < div > is not null</ div > ;
111
+ } ;
112
+
113
+ let renderer : TestRenderer . ReactTestRenderer ;
114
+ TestRenderer . act ( ( ) => {
115
+ renderer = TestRenderer . create (
116
+ < MemoryRouter initialEntries = { [ "/two" ] } >
117
+ < NullRenderer routes = { routes } location = { { pathname : "/three" } } />
118
+ </ MemoryRouter >
119
+ ) ;
120
+ } ) ;
121
+
122
+ expect ( renderer . toJSON ( ) ) . toMatchInlineSnapshot ( `
123
+ <div>
124
+ is null
125
+ </div>
126
+ ` ) ;
127
+ } ) ;
128
+
78
129
describe ( "warns" , ( ) => {
79
130
let consoleWarn : jest . SpyInstance ;
80
131
beforeEach ( ( ) => {
0 commit comments