1
1
import { jest , describe , it , beforeEach , expect } from '@jest/globals' ;
2
2
import { h , render } from 'preact' ;
3
3
import { html } from 'htm/preact' ;
4
- import { LocationProvider , Router , useLocation } from '../router.js' ;
4
+ import { LocationProvider , Router , useLocation , Route , useRoute } from '../router.js' ;
5
5
import lazy , { ErrorBoundary } from '../lazy.js' ;
6
6
7
7
Object . defineProperty ( window , 'scrollTo' , { value ( ) { } } ) ;
@@ -47,7 +47,7 @@ describe('Router', () => {
47
47
) ;
48
48
49
49
expect ( scratch ) . toHaveProperty ( 'textContent' , 'Home' ) ;
50
- expect ( Home ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } } , expect . anything ( ) ) ;
50
+ expect ( Home ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
51
51
expect ( Profiles ) . not . toHaveBeenCalled ( ) ;
52
52
expect ( Profile ) . not . toHaveBeenCalled ( ) ;
53
53
expect ( Fallback ) . not . toHaveBeenCalled ( ) ;
@@ -64,7 +64,7 @@ describe('Router', () => {
64
64
65
65
expect ( scratch ) . toHaveProperty ( 'textContent' , 'Profiles' ) ;
66
66
expect ( Home ) . not . toHaveBeenCalled ( ) ;
67
- expect ( Profiles ) . toHaveBeenCalledWith ( { path : '/profiles' , query : { } , params : { } } , expect . anything ( ) ) ;
67
+ expect ( Profiles ) . toHaveBeenCalledWith ( { path : '/profiles' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
68
68
expect ( Profile ) . not . toHaveBeenCalled ( ) ;
69
69
expect ( Fallback ) . not . toHaveBeenCalled ( ) ;
70
70
@@ -82,7 +82,7 @@ describe('Router', () => {
82
82
expect ( Home ) . not . toHaveBeenCalled ( ) ;
83
83
expect ( Profiles ) . not . toHaveBeenCalled ( ) ;
84
84
expect ( Profile ) . toHaveBeenCalledWith (
85
- { path : '/profiles/bob' , query : { } , params : { id : 'bob' } , id : 'bob' } ,
85
+ { path : '/profiles/bob' , query : { } , params : { id : 'bob' } , id : 'bob' , rest : '' } ,
86
86
expect . anything ( )
87
87
) ;
88
88
expect ( Fallback ) . not . toHaveBeenCalled ( ) ;
@@ -102,7 +102,7 @@ describe('Router', () => {
102
102
expect ( Profiles ) . not . toHaveBeenCalled ( ) ;
103
103
expect ( Profile ) . not . toHaveBeenCalled ( ) ;
104
104
expect ( Fallback ) . toHaveBeenCalledWith (
105
- { default : true , path : '/other' , query : { a : 'b' , c : 'd' } , params : { } } ,
105
+ { default : true , path : '/other' , query : { a : 'b' , c : 'd' } , params : { } , rest : '' } ,
106
106
expect . anything ( )
107
107
) ;
108
108
@@ -141,13 +141,13 @@ describe('Router', () => {
141
141
) ;
142
142
143
143
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '' ) ;
144
- expect ( A ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } } , expect . anything ( ) ) ;
144
+ expect ( A ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
145
145
146
146
A . mockClear ( ) ;
147
147
await sleep ( 10 ) ;
148
148
149
149
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>A</h1><p>hello</p>' ) ;
150
- expect ( A ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } } , expect . anything ( ) ) ;
150
+ expect ( A ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
151
151
152
152
A . mockClear ( ) ;
153
153
loc . route ( '/b' ) ;
@@ -160,14 +160,14 @@ describe('Router', () => {
160
160
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>A</h1><p>hello</p>' ) ;
161
161
// We should never re-invoke <A /> while loading <B /> (that would be a remount of the old route):
162
162
expect ( A ) . not . toHaveBeenCalled ( ) ;
163
- expect ( B ) . toHaveBeenCalledWith ( { path : '/b' , query : { } , params : { } } , expect . anything ( ) ) ;
163
+ expect ( B ) . toHaveBeenCalledWith ( { path : '/b' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
164
164
165
165
B . mockClear ( ) ;
166
166
await sleep ( 10 ) ;
167
167
168
168
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>B</h1><p>hello</p>' ) ;
169
169
expect ( A ) . not . toHaveBeenCalled ( ) ;
170
- expect ( B ) . toHaveBeenCalledWith ( { path : '/b' , query : { } , params : { } } , expect . anything ( ) ) ;
170
+ expect ( B ) . toHaveBeenCalledWith ( { path : '/b' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
171
171
172
172
B . mockClear ( ) ;
173
173
loc . route ( '/c' ) ;
@@ -186,14 +186,14 @@ describe('Router', () => {
186
186
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>B</h1><p>hello</p>' ) ;
187
187
// We should never re-invoke <A /> while loading <B /> (that would be a remount of the old route):
188
188
expect ( B ) . not . toHaveBeenCalled ( ) ;
189
- expect ( C ) . toHaveBeenCalledWith ( { path : '/c' , query : { } , params : { } } , expect . anything ( ) ) ;
189
+ expect ( C ) . toHaveBeenCalledWith ( { path : '/c' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
190
190
191
191
C . mockClear ( ) ;
192
192
await sleep ( 10 ) ;
193
193
194
194
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>C</h1>' ) ;
195
195
expect ( B ) . not . toHaveBeenCalled ( ) ;
196
- expect ( C ) . toHaveBeenCalledWith ( { path : '/c' , query : { } , params : { } } , expect . anything ( ) ) ;
196
+ expect ( C ) . toHaveBeenCalledWith ( { path : '/c' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
197
197
198
198
// "instant" routing to already-loaded routes
199
199
@@ -205,7 +205,7 @@ describe('Router', () => {
205
205
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>B</h1><p>hello</p>' ) ;
206
206
expect ( C ) . not . toHaveBeenCalled ( ) ;
207
207
// expect(B).toHaveBeenCalledTimes(1);
208
- expect ( B ) . toHaveBeenCalledWith ( { path : '/b' , query : { } , params : { } } , expect . anything ( ) ) ;
208
+ expect ( B ) . toHaveBeenCalledWith ( { path : '/b' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
209
209
210
210
B . mockClear ( ) ;
211
211
loc . route ( '/' ) ;
@@ -214,7 +214,7 @@ describe('Router', () => {
214
214
expect ( scratch ) . toHaveProperty ( 'innerHTML' , '<h1>A</h1><p>hello</p>' ) ;
215
215
expect ( B ) . not . toHaveBeenCalled ( ) ;
216
216
// expect(A).toHaveBeenCalledTimes(1);
217
- expect ( A ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } } , expect . anything ( ) ) ;
217
+ expect ( A ) . toHaveBeenCalledWith ( { path : '/' , query : { } , params : { } , rest : '' } , expect . anything ( ) ) ;
218
218
} ) ;
219
219
220
220
describe ( 'intercepted VS external links' , ( ) => {
@@ -436,4 +436,66 @@ describe('Router', () => {
436
436
437
437
pushState . mockRestore ( ) ;
438
438
} ) ;
439
+
440
+ it ( 'should match nested routes' , async ( ) => {
441
+ let route ;
442
+ const Inner = ( ) => html `
443
+ < ${ Router } >
444
+ < ${ Route }
445
+ path="/bob"
446
+ component=${ ( ) => {
447
+ route = useRoute ( ) ;
448
+ return null ;
449
+ } }
450
+ />
451
+ </ />
452
+ ` ;
453
+
454
+ render (
455
+ html `
456
+ < ${ LocationProvider } >
457
+ < ${ Router } >
458
+ < ${ Route } path="/foo/:id/*" component=${ Inner } />
459
+ </ />
460
+ < a href="/foo/bar/bob"> </ a>
461
+ </ />
462
+ ` ,
463
+ scratch
464
+ ) ;
465
+
466
+ scratch . querySelector ( 'a[href="/foo/bar/bob"]' ) . click ( ) ;
467
+ await sleep ( 20 ) ;
468
+ expect ( route ) . toMatchObject ( { path : '/bob' , params : { id : 'bar' } } ) ;
469
+ } ) ;
470
+
471
+ it ( 'should append params in nested routes' , async ( ) => {
472
+ let params ;
473
+ const Inner = ( ) => html `
474
+ < ${ Router } >
475
+ < ${ Route }
476
+ path="/bob"
477
+ component=${ ( ) => {
478
+ params = useRoute ( ) . params ;
479
+ return null ;
480
+ } }
481
+ />
482
+ </ />
483
+ ` ;
484
+
485
+ render (
486
+ html `
487
+ < ${ LocationProvider } >
488
+ < ${ Router } >
489
+ < ${ Route } path="/foo/:id/*" component=${ Inner } />
490
+ </ />
491
+ < a href="/foo/bar/bob"> </ a>
492
+ </ />
493
+ ` ,
494
+ scratch
495
+ ) ;
496
+
497
+ scratch . querySelector ( 'a[href="/foo/bar/bob"]' ) . click ( ) ;
498
+ await sleep ( 20 ) ;
499
+ expect ( params ) . toMatchObject ( { id : 'bar' } ) ;
500
+ } ) ;
439
501
} ) ;
0 commit comments