1- import { it , expect } from "vitest" ;
2- import { render , act } from "@testing-library/react" ;
3- import * as TestRenderer from "react-test-renderer" ;
1+ import { it , expect , afterEach } from "vitest" ;
2+ import { render , act , cleanup } from "@testing-library/react" ;
43
54import { Router , Route } from "wouter" ;
65import { memoryLocation } from "wouter/memory-location" ;
76import { ReactElement } from "react" ;
87
8+ // Clean up after each test to avoid DOM pollution
9+ afterEach ( cleanup ) ;
10+
911const testRouteRender = ( initialPath : string , jsx : ReactElement ) => {
10- const instance = TestRenderer . create (
12+ return render (
1113 < Router hook = { memoryLocation ( { path : initialPath } ) . hook } > { jsx } </ Router >
12- ) . root ;
13-
14- return instance ;
14+ ) ;
1515} ;
1616
1717it ( "always renders its content when `path` is empty" , ( ) => {
18- const result = testRouteRender (
18+ const { container } = testRouteRender (
1919 "/nothing" ,
2020 < Route >
2121 < h1 > Hello!</ h1 >
2222 </ Route >
2323 ) ;
2424
25- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "Hello!" ) ;
25+ const heading = container . querySelector ( "h1" ) ;
26+ expect ( heading ) . toBeInTheDocument ( ) ;
27+ expect ( heading ) . toHaveTextContent ( "Hello!" ) ;
2628} ) ;
2729
2830it ( "accepts plain children" , ( ) => {
29- const result = testRouteRender (
31+ const { container } = testRouteRender (
3032 "/foo" ,
3133 < Route path = "/foo" >
3234 < h1 > Hello!</ h1 >
3335 </ Route >
3436 ) ;
3537
36- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "Hello!" ) ;
38+ const heading = container . querySelector ( "h1" ) ;
39+ expect ( heading ) . toBeInTheDocument ( ) ;
40+ expect ( heading ) . toHaveTextContent ( "Hello!" ) ;
3741} ) ;
3842
3943it ( "works with render props" , ( ) => {
40- const result = testRouteRender (
44+ const { container } = testRouteRender (
4145 "/foo" ,
4246 < Route path = "/foo" > { ( ) => < h1 > Hello!</ h1 > } </ Route >
4347 ) ;
4448
45- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "Hello!" ) ;
49+ const heading = container . querySelector ( "h1" ) ;
50+ expect ( heading ) . toBeInTheDocument ( ) ;
51+ expect ( heading ) . toHaveTextContent ( "Hello!" ) ;
4652} ) ;
4753
4854it ( "passes a match param object to the render function" , ( ) => {
49- const result = testRouteRender (
55+ const { container } = testRouteRender (
5056 "/users/alex" ,
5157 < Route path = "/users/:name" > { ( params ) => < h1 > { params . name } </ h1 > } </ Route >
5258 ) ;
5359
54- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "alex" ) ;
60+ const heading = container . querySelector ( "h1" ) ;
61+ expect ( heading ) . toBeInTheDocument ( ) ;
62+ expect ( heading ) . toHaveTextContent ( "alex" ) ;
5563} ) ;
5664
5765it ( "renders nothing when there is not match" , ( ) => {
58- const result = testRouteRender (
66+ const { container } = testRouteRender (
5967 "/bar" ,
6068 < Route path = "/foo" >
6169 < div > Hi!</ div >
6270 </ Route >
6371 ) ;
6472
65- expect ( ( ) => result . findByType ( "div" ) ) . toThrow ( ) ;
73+ expect ( container . querySelector ( "div" ) ) . not . toBeInTheDocument ( ) ;
6674} ) ;
6775
6876it ( "supports `component` prop similar to React-Router" , ( ) => {
6977 const Users = ( ) => < h2 > All users</ h2 > ;
7078
71- const result = testRouteRender (
79+ const { container } = testRouteRender (
7280 "/foo" ,
7381 < Route path = "/foo" component = { Users } />
7482 ) ;
7583
76- expect ( result . findByType ( "h2" ) . props . children ) . toBe ( "All users" ) ;
84+ const heading = container . querySelector ( "h2" ) ;
85+ expect ( heading ) . toBeInTheDocument ( ) ;
86+ expect ( heading ) . toHaveTextContent ( "All users" ) ;
7787} ) ;
7888
7989it ( "supports `base` routers with relative path" , ( ) => {
@@ -90,52 +100,58 @@ it("supports `base` routers with relative path", () => {
90100
91101 act ( ( ) => history . replaceState ( null , "" , "/app/nested" ) ) ;
92102
93- expect ( container . childNodes . length ) . toBe ( 1 ) ;
94- expect ( ( container . firstChild as HTMLElement ) . tagName ) . toBe ( "H1" ) ;
103+ expect ( container . children ) . toHaveLength ( 1 ) ;
104+ expect ( container . firstChild ) . toHaveProperty ( "tagName" , "H1" ) ;
95105
96106 unmount ( ) ;
97107} ) ;
98108
99109it ( "supports `path` prop with regex" , ( ) => {
100- const result = testRouteRender (
110+ const { container } = testRouteRender (
101111 "/foo" ,
102112 < Route path = { / [ / ] f o o / } >
103113 < h1 > Hello!</ h1 >
104114 </ Route >
105115 ) ;
106116
107- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "Hello!" ) ;
117+ const heading = container . querySelector ( "h1" ) ;
118+ expect ( heading ) . toBeInTheDocument ( ) ;
119+ expect ( heading ) . toHaveTextContent ( "Hello!" ) ;
108120} ) ;
109121
110122it ( "supports regex path named params" , ( ) => {
111- const result = testRouteRender (
123+ const { container } = testRouteRender (
112124 "/users/alex" ,
113125 < Route path = { / [ / ] u s e r s [ / ] (?< name > [ a - z ] + ) / } >
114126 { ( params ) => < h1 > { params . name } </ h1 > }
115127 </ Route >
116128 ) ;
117129
118- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "alex" ) ;
130+ const heading = container . querySelector ( "h1" ) ;
131+ expect ( heading ) . toBeInTheDocument ( ) ;
132+ expect ( heading ) . toHaveTextContent ( "alex" ) ;
119133} ) ;
120134
121135it ( "supports regex path anonymous params" , ( ) => {
122- const result = testRouteRender (
136+ const { container } = testRouteRender (
123137 "/users/alex" ,
124138 < Route path = { / [ / ] u s e r s [ / ] ( [ a - z ] + ) / } >
125139 { ( params ) => < h1 > { params [ 0 ] } </ h1 > }
126140 </ Route >
127141 ) ;
128142
129- expect ( result . findByType ( "h1" ) . props . children ) . toBe ( "alex" ) ;
143+ const heading = container . querySelector ( "h1" ) ;
144+ expect ( heading ) . toBeInTheDocument ( ) ;
145+ expect ( heading ) . toHaveTextContent ( "alex" ) ;
130146} ) ;
131147
132148it ( "rejects when a path does not match the regex" , ( ) => {
133- const result = testRouteRender (
149+ const { container } = testRouteRender (
134150 "/users/1234" ,
135151 < Route path = { / [ / ] u s e r s [ / ] (?< name > [ a - z ] + ) / } >
136152 { ( params ) => < h1 > { params . name } </ h1 > }
137153 </ Route >
138154 ) ;
139155
140- expect ( ( ) => result . findByType ( "h1" ) ) . toThrow ( ) ;
156+ expect ( container . querySelector ( "h1" ) ) . not . toBeInTheDocument ( ) ;
141157} ) ;
0 commit comments