1
1
import { jest , describe , it , beforeEach , expect } from '@jest/globals' ;
2
2
import { h , render } from 'preact' ;
3
+ import { useState } from 'preact/hooks' ;
3
4
import { html } from 'htm/preact' ;
4
5
import { LocationProvider , Router , useLocation , Route , useRoute } from '../src/router.js' ;
5
6
import lazy , { ErrorBoundary } from '../src/lazy.js' ;
@@ -55,25 +56,31 @@ describe('Router', () => {
55
56
} ) ;
56
57
} ) ;
57
58
58
- it . only ( 'should allow updating props in a route' , async ( ) => {
59
+ it ( 'should allow updating props in a route' , async ( ) => {
59
60
const Home = jest . fn ( ( ) => html `< h1 > Home</ h1 > ` ) ;
60
61
const stack = [ ] ;
61
- let loc ;
62
- render (
63
- html `
62
+ let loc , set ;
63
+
64
+ const App = ( ) => {
65
+ const [ test , setTest ] = useState ( '2' ) ;
66
+ set = setTest ;
67
+ return html `
64
68
< ${ LocationProvider } >
65
- < ${ Router }
66
- onRouteChange =${ url => {
67
- stack . push ( url ) ;
68
- } }
69
- >
70
- < ${ Home } path ="/" test="2" />
71
- </ />
72
- < ${ ( ) => {
73
- loc = useLocation ( ) ;
74
- } } />
69
+ < ${ Router }
70
+ onRouteChange =${ url => {
71
+ stack . push ( url ) ;
72
+ } }
73
+ >
74
+ < ${ Home } path ="/" test=${ test } />
75
75
</ />
76
- ` ,
76
+ < ${ ( ) => {
77
+ loc = useLocation ( ) ;
78
+ } } />
79
+ </ />
80
+ ` ;
81
+ }
82
+ render (
83
+ html `< ${ App } /> ` ,
77
84
scratch
78
85
) ;
79
86
@@ -87,34 +94,17 @@ describe('Router', () => {
87
94
} ) ;
88
95
89
96
Home . mockReset ( ) ;
97
+ set ( '3' )
98
+ await sleep ( 1 ) ;
90
99
91
- render (
92
- html `
93
- < ${ LocationProvider } >
94
- < ${ Router }
95
- onRouteChange=${ url => {
96
- stack . push ( url ) ;
97
- } }
98
- >
99
- < ${ Home } path="/" test="3" />
100
- </ />
101
- < ${ ( ) => {
102
- loc = useLocation ( ) ;
103
- } } />
104
- </ />
105
- ` ,
106
- scratch
107
- ) ;
108
-
109
- console . log ( scratch . outerHTML )
110
- expect ( scratch ) . toHaveProperty ( 'textContent' , 'Home' ) ;
111
100
expect ( Home ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } , rest : '' , test : '3' } , expect . anything ( ) ) ;
112
101
expect ( loc ) . toMatchObject ( {
113
102
url : '/' ,
114
103
path : '/' ,
115
104
query : { } ,
116
105
route : expect . any ( Function )
117
106
} ) ;
107
+ expect ( scratch ) . toHaveProperty ( 'textContent' , 'Home' ) ;
118
108
} ) ;
119
109
120
110
it ( 'should switch between synchronous routes' , async ( ) => {
0 commit comments