@@ -130,10 +130,17 @@ describe('withLDProvider', () => {
130130 const mockSetState = jest . spyOn ( instance , 'setState' ) ;
131131
132132 await instance . componentDidMount ( ) ;
133- const setStateFunction = mockSetState . mock ?. lastCall ?. [ 0 ] as ( p : ProviderState ) => ProviderState ;
133+ // Each set of the state depends on the previous state, so to re-create the final state, we need to call the
134+ // setState function for each call.
135+ let finalState = previousState ;
136+
137+ for ( const call of mockSetState . mock . calls ) {
138+ const setStateFunction = call [ 0 ] as ( p : ProviderState ) => ProviderState ;
139+ finalState = setStateFunction ( finalState ) ;
140+ }
134141
135142 expect ( mockLDClient . on ) . toHaveBeenCalledWith ( 'change' , expect . any ( Function ) ) ;
136- expect ( setStateFunction ( previousState ) ) . toEqual ( {
143+ expect ( finalState ) . toMatchObject ( {
137144 flags : { anotherTestFlag : true , testFlag : false } ,
138145 unproxiedFlags : { 'test-flag' : false , 'another-test-flag' : true } ,
139146 flagKeyMap : { testFlag : 'test-flag' , anotherTestFlag : 'another-test-flag' } ,
@@ -149,10 +156,17 @@ describe('withLDProvider', () => {
149156 const mockSetState = jest . spyOn ( instance , 'setState' ) ;
150157
151158 await instance . componentDidMount ( ) ;
152- const setStateFunction = mockSetState . mock ?. lastCall ?. [ 0 ] as ( p : ProviderState ) => ProviderState ;
159+ // Each set of the state depends on the previous state, so to re-create the final state, we need to call the
160+ // setState function for each call.
161+ let finalState = previousState ;
162+
163+ for ( const call of mockSetState . mock . calls ) {
164+ const setStateFunction = call [ 0 ] as ( p : ProviderState ) => ProviderState ;
165+ finalState = setStateFunction ( finalState ) ;
166+ }
153167
154168 expect ( mockLDClient . on ) . toHaveBeenCalledWith ( 'change' , expect . any ( Function ) ) ;
155- expect ( setStateFunction ( previousState ) ) . toEqual ( {
169+ expect ( finalState ) . toMatchObject ( {
156170 flags : { 'test-flag' : false , 'another-test-flag' : false } ,
157171 unproxiedFlags : { 'test-flag' : false , 'another-test-flag' : false } ,
158172 flagKeyMap : { } ,
0 commit comments