@@ -3,7 +3,8 @@ import React, {
3
3
render ,
4
4
createPortal ,
5
5
useState ,
6
- Component
6
+ Component ,
7
+ useEffect
7
8
} from 'preact/compat' ;
8
9
import { setupScratch , teardown } from '../../../test/_util/helpers' ;
9
10
import { setupRerender , act } from 'preact/test-utils' ;
@@ -624,4 +625,101 @@ describe('Portal', () => {
624
625
'<div><div>Closed</div><button>Show</button>Closed</div>'
625
626
) ;
626
627
} ) ;
628
+
629
+ it ( 'should order effects well' , ( ) => {
630
+ const calls = [ ] ;
631
+ const Modal = ( { children } ) => {
632
+ useEffect ( ( ) => {
633
+ calls . push ( 'Modal' ) ;
634
+ } , [ ] ) ;
635
+ return createPortal ( < div className = "modal" > { children } </ div > , scratch ) ;
636
+ } ;
637
+
638
+ const ModalButton = ( { i } ) => {
639
+ useEffect ( ( ) => {
640
+ calls . push ( `Button ${ i } ` ) ;
641
+ } , [ ] ) ;
642
+
643
+ return < button > Action</ button > ;
644
+ } ;
645
+
646
+ const App = ( ) => {
647
+ useEffect ( ( ) => {
648
+ calls . push ( 'App' ) ;
649
+ } , [ ] ) ;
650
+
651
+ return (
652
+ < Modal >
653
+ < ModalButton i = "1" />
654
+ < ModalButton i = "2" />
655
+ </ Modal >
656
+ ) ;
657
+ } ;
658
+
659
+ act ( ( ) => {
660
+ render ( < App /> , scratch ) ;
661
+ } ) ;
662
+
663
+ expect ( calls ) . to . deep . equal ( [ 'Button 1' , 'Button 2' , 'Modal' , 'App' ] ) ;
664
+ } ) ;
665
+
666
+ it ( 'should order complex effects well' , ( ) => {
667
+ const calls = [ ] ;
668
+ const Parent = ( { children, isPortal } ) => {
669
+ useEffect ( ( ) => {
670
+ calls . push ( `${ isPortal ? 'Portal ' : '' } Parent` ) ;
671
+ } , [ isPortal ] ) ;
672
+
673
+ return < div > { children } </ div > ;
674
+ } ;
675
+
676
+ const Child = ( { index, isPortal } ) => {
677
+ useEffect ( ( ) => {
678
+ calls . push ( `${ isPortal ? 'Portal ' : '' } Child ${ index } ` ) ;
679
+ } , [ index , isPortal ] ) ;
680
+
681
+ return < div > { index } </ div > ;
682
+ } ;
683
+
684
+ const Portal = ( ) => {
685
+ const content = [ 1 , 2 , 3 ] . map ( index => (
686
+ < Child key = { index } index = { index } isPortal />
687
+ ) ) ;
688
+
689
+ useEffect ( ( ) => {
690
+ calls . push ( 'Portal' ) ;
691
+ } , [ ] ) ;
692
+
693
+ return createPortal ( < Parent isPortal > { content } </ Parent > , document . body ) ;
694
+ } ;
695
+
696
+ const App = ( ) => {
697
+ const content = [ 1 , 2 , 3 ] . map ( index => (
698
+ < Child key = { index } index = { index } />
699
+ ) ) ;
700
+
701
+ return (
702
+ < React . Fragment >
703
+ < Parent > { content } </ Parent >
704
+ < Portal />
705
+ </ React . Fragment >
706
+ ) ;
707
+ } ;
708
+
709
+ act ( ( ) => {
710
+ render ( < App /> , scratch ) ;
711
+ } ) ;
712
+
713
+ expect ( calls ) . to . deep . equal ( [
714
+ 'Child 1' ,
715
+ 'Child 2' ,
716
+ 'Child 3' ,
717
+ 'Parent' ,
718
+ 'Portal Child 1' ,
719
+ 'Portal Child 2' ,
720
+ 'Portal Child 3' ,
721
+ 'Portal Parent' ,
722
+ 'Portal'
723
+ ] ) ;
724
+ } ) ;
627
725
} ) ;
0 commit comments