@@ -456,10 +456,18 @@ describe('LDProvider', () => {
456456 const mockSetState = jest . spyOn ( instance , 'setState' ) ;
457457
458458 await instance . componentDidMount ( ) ;
459- const setStateFunction = mockSetState . mock ?. lastCall ?. [ 0 ] as ( p : ProviderState ) => ProviderState ;
459+
460+ // Each set of the state depends on the previous state, so to re-create the final state, we need to call the
461+ // setState function for each call.
462+ let finalState = previousState ;
463+
464+ for ( const call of mockSetState . mock . calls ) {
465+ const setStateFunction = call [ 0 ] as ( p : ProviderState ) => ProviderState ;
466+ finalState = setStateFunction ( finalState ) ;
467+ }
460468
461469 expect ( mockLDClient . on ) . toHaveBeenCalledWith ( 'change' , expect . any ( Function ) ) ;
462- expect ( setStateFunction ( previousState ) ) . toEqual ( {
470+ expect ( finalState ) . toMatchObject ( {
463471 flags : { anotherTestFlag : true , testFlag : false } ,
464472 unproxiedFlags : { 'another-test-flag' : true , 'test-flag' : false } ,
465473 flagKeyMap : { anotherTestFlag : 'another-test-flag' , testFlag : 'test-flag' } ,
@@ -480,16 +488,56 @@ describe('LDProvider', () => {
480488 const mockSetState = jest . spyOn ( instance , 'setState' ) ;
481489
482490 await instance . componentDidMount ( ) ;
483- const setStateFunction = mockSetState . mock ?. lastCall ?. [ 0 ] as ( p : ProviderState ) => ProviderState ;
491+
492+ // Each set of the state depends on the previous state, so to re-create the final state, we need to call the
493+ // setState function for each call.
494+ let finalState = previousState ;
495+
496+ for ( const call of mockSetState . mock . calls ) {
497+ const setStateFunction = call [ 0 ] as ( p : ProviderState ) => ProviderState ;
498+ finalState = setStateFunction ( finalState ) ;
499+ }
484500
485501 expect ( mockLDClient . on ) . toHaveBeenCalledWith ( 'change' , expect . any ( Function ) ) ;
486- expect ( setStateFunction ( previousState ) ) . toEqual ( {
502+ expect ( finalState ) . toMatchObject ( {
487503 flagKeyMap : { } ,
488504 unproxiedFlags : { 'another-test-flag' : false , 'test-flag' : false } ,
489505 flags : { 'another-test-flag' : false , 'test-flag' : false } ,
490506 } ) ;
491507 } ) ;
492508
509+ test ( 'handles deletion of flags' , async ( ) => {
510+ mockLDClient . on . mockImplementation ( ( _e : string , cb : ( c : LDFlagChangeset ) => void ) => {
511+ cb ( { 'another-test-flag' : { current : undefined , previous : true } , 'test-flag' : { current : false , previous : true } } ) ;
512+ } ) ;
513+ const props : ProviderConfig = { clientSideID, reactOptions : { useCamelCaseFlagKeys : false } } ;
514+ const LaunchDarklyApp = (
515+ < LDProvider { ...props } >
516+ < App />
517+ </ LDProvider >
518+ ) ;
519+ const instance = create ( LaunchDarklyApp ) . root . findByType ( LDProvider ) . instance as EnhancedComponent ;
520+ const mockSetState = jest . spyOn ( instance , 'setState' ) ;
521+
522+ await instance . componentDidMount ( ) ;
523+
524+ // Each set of the state depends on the previous state, so to re-create the final state, we need to call the
525+ // setState function for each call.
526+ let finalState = previousState ;
527+
528+ for ( const call of mockSetState . mock . calls ) {
529+ const setStateFunction = call [ 0 ] as ( p : ProviderState ) => ProviderState ;
530+ finalState = setStateFunction ( finalState ) ;
531+ }
532+
533+ expect ( mockLDClient . on ) . toHaveBeenCalledWith ( 'change' , expect . any ( Function ) ) ;
534+ expect ( finalState ) . toMatchObject ( {
535+ flagKeyMap : { } ,
536+ unproxiedFlags : { 'test-flag' : false } ,
537+ flags : { 'test-flag' : false } ,
538+ } ) ;
539+ } ) ;
540+
493541 test ( `if props.deferInitialization is true, ld client will only initialize once props.user is defined` , async ( ) => {
494542 options = { ...options , bootstrap : { } } ;
495543 const props : ProviderConfig = { clientSideID, deferInitialization : true , options } ;
0 commit comments