@@ -79,3 +79,55 @@ test('should remove listeners when component unmounts', () => {
7979 expect ( onChangeFirst ) . toHaveBeenCalledTimes ( 2 ) ;
8080 expect ( onChangeSecond ) . toHaveBeenCalledTimes ( 1 ) ;
8181} ) ;
82+
83+ test ( 'should notify listeners in registration order' , ( ) => {
84+ const { Demo, state } = setup ( ) ;
85+ const callOrder : string [ ] = [ ] ;
86+ const onChangeFirst = jest . fn ( ( ) => callOrder . push ( 'first' ) ) ;
87+ const onChangeSecond = jest . fn ( ( ) => callOrder . push ( 'second' ) ) ;
88+ const onChangeThird = jest . fn ( ( ) => callOrder . push ( 'third' ) ) ;
89+ render (
90+ < >
91+ < Demo onChange = { onChangeFirst } />
92+ < Demo onChange = { onChangeSecond } />
93+ < Demo onChange = { onChangeThird } />
94+ </ >
95+ ) ;
96+ state . handler ( 42 ) ;
97+ expect ( callOrder ) . toEqual ( [ 'first' , 'second' , 'third' ] ) ;
98+ } ) ;
99+
100+ test ( 'should handle rapid mount/unmount cycles correctly' , ( ) => {
101+ const { Demo, state } = setup ( ) ;
102+ const onChange1 = jest . fn ( ) ;
103+ const onChange2 = jest . fn ( ) ;
104+ const onChange3 = jest . fn ( ) ;
105+
106+ const { rerender, unmount } = render ( < Demo onChange = { onChange1 } /> ) ;
107+ expect ( state . subscriptions ) . toEqual ( 1 ) ;
108+
109+ // Rapid additions
110+ rerender (
111+ < >
112+ < Demo onChange = { onChange1 } />
113+ < Demo onChange = { onChange2 } />
114+ </ >
115+ ) ;
116+ rerender (
117+ < >
118+ < Demo onChange = { onChange1 } />
119+ < Demo onChange = { onChange2 } />
120+ < Demo onChange = { onChange3 } />
121+ </ >
122+ ) ;
123+ expect ( state . subscriptions ) . toEqual ( 1 ) ;
124+
125+ state . handler ( 100 ) ;
126+ expect ( onChange1 ) . toHaveBeenCalledWith ( 100 ) ;
127+ expect ( onChange2 ) . toHaveBeenCalledWith ( 100 ) ;
128+ expect ( onChange3 ) . toHaveBeenCalledWith ( 100 ) ;
129+
130+ // Complete cleanup
131+ unmount ( ) ;
132+ expect ( state . subscriptions ) . toEqual ( 0 ) ;
133+ } ) ;
0 commit comments