@@ -12,6 +12,11 @@ import './setup.js';
12
12
const expect = chai . expect ;
13
13
chai . use ( sinonChai ) ;
14
14
15
+ /**
16
+ * Usage:
17
+ * - `await sleep(1)` for nav + loc/pushState/sync component check
18
+ * - `await sleep(10)` for nav + async component check
19
+ */
15
20
const sleep = ms => new Promise ( r => setTimeout ( r , ms ) ) ;
16
21
17
22
// delayed lazy()
@@ -335,8 +340,6 @@ describe('Router', () => {
335
340
scratch
336
341
) ;
337
342
338
- await sleep ( 10 ) ;
339
-
340
343
expect ( scratch ) . to . have . property ( 'innerHTML' , '<h1>a</h1>' ) ;
341
344
expect ( renderRefCount ) . to . equal ( 2 ) ;
342
345
@@ -401,7 +404,7 @@ describe('Router', () => {
401
404
loadStart . resetHistory ( ) ;
402
405
loadEnd . resetHistory ( ) ;
403
406
routeChange . resetHistory ( ) ;
404
- await sleep ( 10 ) ;
407
+ await sleep ( 1 ) ;
405
408
406
409
expect ( scratch ) . to . have . property ( 'innerHTML' , '<h1>A</h1><p>hello</p>' ) ;
407
410
expect ( A ) . to . have . been . calledWith ( { path : '/' , query : { } , params : { } , rest : '' } ) ;
@@ -417,7 +420,7 @@ describe('Router', () => {
417
420
< p > hello</ p >
418
421
</ >
419
422
) ;
420
- const A = sinon . fake ( groggy ( ( ) => route ( 'A' ) , 1 ) ) ;
423
+ const A = sinon . fake ( ( ) => route ( 'A' ) ) ;
421
424
const B = sinon . fake ( groggy ( ( ) => route ( 'B' ) , 1 ) ) ;
422
425
const loadStart = sinon . fake ( ) ;
423
426
const loadEnd = sinon . fake ( ) ;
@@ -440,15 +443,12 @@ describe('Router', () => {
440
443
scratch
441
444
) ;
442
445
443
- await sleep ( 10 ) ;
444
-
445
446
A . resetHistory ( ) ;
446
447
loadStart . resetHistory ( ) ;
447
448
loadEnd . resetHistory ( ) ;
448
449
routeChange . resetHistory ( ) ;
449
450
450
451
loc . route ( '/b' ) ;
451
-
452
452
await sleep ( 1 ) ;
453
453
454
454
expect ( loadStart ) . to . have . been . calledWith ( '/b' ) ;
@@ -459,7 +459,6 @@ describe('Router', () => {
459
459
loadStart . resetHistory ( ) ;
460
460
loadEnd . resetHistory ( ) ;
461
461
routeChange . resetHistory ( ) ;
462
-
463
462
await sleep ( 10 ) ;
464
463
465
464
expect ( scratch ) . to . have . property ( 'innerHTML' , '<h1>B</h1><p>hello</p>' ) ;
@@ -497,6 +496,7 @@ describe('Router', () => {
497
496
498
497
await sleep ( 10 ) ;
499
498
499
+ expect ( loadEnd ) . to . have . been . calledWith ( '/' ) ;
500
500
loadEnd . resetHistory ( ) ;
501
501
502
502
set ( '2' ) ;
@@ -543,7 +543,6 @@ describe('Router', () => {
543
543
</ LocationProvider > ,
544
544
scratch
545
545
) ;
546
- await sleep ( 10 ) ;
547
546
Route . resetHistory ( ) ;
548
547
clickHandler . resetHistory ( ) ;
549
548
pushState . resetHistory ( ) ;
@@ -554,8 +553,6 @@ describe('Router', () => {
554
553
// these should all be intercepted by the router.
555
554
for ( const target of shouldIntercept ) {
556
555
it ( `should intercept clicks on links with ${ getName ( target ) } ` , async ( ) => {
557
- await sleep ( 10 ) ;
558
-
559
556
const sel = target == null ? `a:not([target])` : `a[target="${ target } "]` ;
560
557
const el = scratch . querySelector ( sel ) ;
561
558
if ( ! el ) throw Error ( `Unable to find link: ${ sel } ` ) ;
@@ -572,8 +569,6 @@ describe('Router', () => {
572
569
// these should all navigate.
573
570
for ( const target of shouldNavigate ) {
574
571
it ( `should allow default browser navigation for links with ${ getName ( target ) } ` , async ( ) => {
575
- await sleep ( 10 ) ;
576
-
577
572
const sel = target == null ? `a:not([target])` : `a[target="${ target } "]` ;
578
573
const el = scratch . querySelector ( sel ) ;
579
574
if ( ! el ) throw Error ( `Unable to find link: ${ sel } ` ) ;
@@ -626,11 +621,9 @@ describe('Router', () => {
626
621
</ LocationProvider > ,
627
622
scratch
628
623
) ;
629
- await sleep ( 10 ) ;
630
624
631
625
for ( const url of shouldIntercept ) {
632
- const el = scratch . querySelector ( `a[href="${ url } "]` ) ;
633
- el . click ( ) ;
626
+ scratch . querySelector ( `a[href="${ url } "]` ) . click ( ) ;
634
627
await sleep ( 1 ) ;
635
628
expect ( loc ) . to . deep . include ( { url } ) ;
636
629
expect ( pushState ) . to . have . been . calledWith ( null , '' , url ) ;
@@ -641,19 +634,17 @@ describe('Router', () => {
641
634
}
642
635
} ) ;
643
636
644
- it ( 'should allow default browser navigation for links not matching the `limit ` props (string)' , async ( ) => {
637
+ it ( 'should allow default browser navigation for links not matching the `scope ` props (string)' , async ( ) => {
645
638
render (
646
639
< LocationProvider scope = "app" >
647
640
< Links />
648
641
< ShallowLocation />
649
642
</ LocationProvider > ,
650
643
scratch
651
644
) ;
652
- await sleep ( 10 ) ;
653
645
654
646
for ( const url of shouldNavigate ) {
655
- const el = scratch . querySelector ( `a[href="${ url } "]` ) ;
656
- el . click ( ) ;
647
+ scratch . querySelector ( `a[href="${ url } "]` ) . click ( ) ;
657
648
await sleep ( 1 ) ;
658
649
expect ( pushState ) . not . to . have . been . called ;
659
650
expect ( clickHandler ) . to . have . been . called ;
@@ -663,19 +654,17 @@ describe('Router', () => {
663
654
}
664
655
} ) ;
665
656
666
- it ( 'should intercept clicks on links matching the `limit ` props (regex)' , async ( ) => {
657
+ it ( 'should intercept clicks on links matching the `scope ` props (regex)' , async ( ) => {
667
658
render (
668
659
< LocationProvider scope = { / ^ \/ a p p / } >
669
660
< Links />
670
661
< ShallowLocation />
671
662
</ LocationProvider > ,
672
663
scratch
673
664
) ;
674
- await sleep ( 10 ) ;
675
665
676
666
for ( const url of shouldIntercept ) {
677
- const el = scratch . querySelector ( `a[href="${ url } "]` ) ;
678
- el . click ( ) ;
667
+ scratch . querySelector ( `a[href="${ url } "]` ) . click ( ) ;
679
668
await sleep ( 1 ) ;
680
669
expect ( loc ) . to . deep . include ( { url } ) ;
681
670
expect ( pushState ) . to . have . been . calledWith ( null , '' , url ) ;
@@ -686,19 +675,17 @@ describe('Router', () => {
686
675
}
687
676
} ) ;
688
677
689
- it ( 'should allow default browser navigation for links not matching the `limit ` props (regex)' , async ( ) => {
678
+ it ( 'should allow default browser navigation for links not matching the `scope ` props (regex)' , async ( ) => {
690
679
render (
691
680
< LocationProvider scope = { / ^ \/ a p p / } >
692
681
< Links />
693
682
< ShallowLocation />
694
683
</ LocationProvider > ,
695
684
scratch
696
685
) ;
697
- await sleep ( 10 ) ;
698
686
699
687
for ( const url of shouldNavigate ) {
700
- const el = scratch . querySelector ( `a[href="${ url } "]` ) ;
701
- el . click ( ) ;
688
+ scratch . querySelector ( `a[href="${ url } "]` ) . click ( ) ;
702
689
await sleep ( 1 ) ;
703
690
expect ( pushState ) . not . to . have . been . called ;
704
691
expect ( clickHandler ) . to . have . been . called ;
@@ -723,14 +710,13 @@ describe('Router', () => {
723
710
scratch
724
711
) ;
725
712
726
- await sleep ( 20 ) ;
727
-
728
713
expect ( scrollTo ) . not . to . have . been . called ;
729
714
expect ( Route ) . to . have . been . calledOnce ;
730
715
Route . resetHistory ( ) ;
731
716
732
717
loc . route ( '/programmatic' ) ;
733
- await sleep ( 10 ) ;
718
+ await sleep ( 1 ) ;
719
+
734
720
expect ( loc ) . to . deep . include ( { url : '/programmatic' } ) ;
735
721
expect ( scrollTo ) . to . have . been . calledWith ( 0 , 0 ) ;
736
722
expect ( scrollTo ) . to . have . been . calledOnce ;
@@ -739,14 +725,14 @@ describe('Router', () => {
739
725
scrollTo . resetHistory ( ) ;
740
726
741
727
scratch . querySelector ( 'a' ) . click ( ) ;
742
- await sleep ( 10 ) ;
728
+ await sleep ( 1 ) ;
729
+
743
730
expect ( loc ) . to . deep . include ( { url : '/link' } ) ;
744
731
expect ( scrollTo ) . to . have . been . calledWith ( 0 , 0 ) ;
745
732
expect ( scrollTo ) . to . have . been . calledOnce ;
746
733
expect ( Route ) . to . have . been . calledOnce ;
747
734
Route . resetHistory ( ) ;
748
735
749
- await sleep ( 10 ) ;
750
736
scrollTo . restore ( ) ;
751
737
} ) ;
752
738
@@ -773,20 +759,19 @@ describe('Router', () => {
773
759
774
760
expect ( Route ) . to . have . been . calledOnce ;
775
761
Route . resetHistory ( ) ;
776
- await sleep ( 20 ) ;
777
762
778
763
scratch . querySelector ( 'a[href="#foo"]' ) . click ( ) ;
779
- await sleep ( 20 ) ;
764
+ await sleep ( 1 ) ;
765
+
780
766
// NOTE: we don't (currently) propagate in-page anchor navigations into context, to avoid useless renders.
781
767
expect ( loc ) . to . deep . include ( { url : '/' } ) ;
782
768
expect ( Route ) . not . to . have . been . called ;
783
769
expect ( pushState ) . not . to . have . been . called ;
784
770
expect ( location . hash ) . to . equal ( '#foo' ) ;
785
771
786
- await sleep ( 10 ) ;
787
-
788
772
scratch . querySelector ( 'a[href="/other#bar"]' ) . click ( ) ;
789
- await sleep ( 10 ) ;
773
+ await sleep ( 1 ) ;
774
+
790
775
expect ( Route ) . to . have . been . calledOnce ;
791
776
expect ( loc ) . to . deep . include ( { url : '/other#bar' , path : '/other' } ) ;
792
777
expect ( pushState ) . to . have . been . called ;
@@ -813,10 +798,10 @@ describe('Router', () => {
813
798
814
799
expect ( Route ) . to . have . been . calledOnce ;
815
800
Route . resetHistory ( ) ;
816
- await sleep ( 20 ) ;
817
801
818
802
scratch . querySelector ( 'a[href="/foo#foo"]' ) . click ( ) ;
819
- await sleep ( 20 ) ;
803
+ await sleep ( 10 ) ;
804
+
820
805
expect ( Route ) . to . have . been . calledOnce ;
821
806
expect ( loc ) . to . deep . include ( { url : '/foo#foo' , path : '/foo' } ) ;
822
807
expect ( pushState ) . to . have . been . called ;
@@ -849,7 +834,7 @@ describe('Router', () => {
849
834
) ;
850
835
851
836
scratch . querySelector ( 'a[href="/foo/bar/bob"]' ) . click ( ) ;
852
- await sleep ( 20 ) ;
837
+ await sleep ( 1 ) ;
853
838
expect ( route ) . to . deep . include ( { path : '/bob' , params : { id : 'bar' } } ) ;
854
839
} ) ;
855
840
@@ -878,7 +863,7 @@ describe('Router', () => {
878
863
) ;
879
864
880
865
scratch . querySelector ( 'a[href="/foo/bar/bob"]' ) . click ( ) ;
881
- await sleep ( 20 ) ;
866
+ await sleep ( 1 ) ;
882
867
expect ( params ) . to . deep . include ( { id : 'bar' } ) ;
883
868
} ) ;
884
869
@@ -896,7 +881,6 @@ describe('Router', () => {
896
881
scratch
897
882
) ;
898
883
899
- await sleep ( 20 ) ;
900
884
loc . route ( "/foo" , true ) ;
901
885
expect ( pushState ) . not . to . have . been . called ;
902
886
expect ( replaceState ) . to . have . been . calledWith ( null , "" , "/foo" ) ;
0 commit comments