@@ -162,17 +162,18 @@ describe('OpenFeatureProvider', () => {
162162 } ) ;
163163 } ) ;
164164 describe ( 'useMutateContext' , ( ) => {
165- const MutateButton = ( ) => {
165+ const MutateButton = ( { setter } : { setter ?: ( prevContext : EvaluationContext ) => EvaluationContext } ) => {
166166 const { setContext } = useContextMutator ( ) ;
167167
168- return < button onClick = { ( ) => setContext ( { user :
'[email protected] ' } ) } > Update Context
</ button > ; 168+ return < button onClick = { ( ) => setContext ( setter ?? { user :
'[email protected] ' } ) } > Update Context
</ button > ; 169169 } ;
170- const TestComponent = ( { name } : { name : string } ) => {
170+
171+ const TestComponent = ( { name, setter } : { name : string ; setter ?: ( prevContext : EvaluationContext ) => EvaluationContext } ) => {
171172 const flagValue = useStringFlagValue < 'hi' | 'bye' | 'aloha' > ( SUSPENSE_FLAG_KEY , 'hi' ) ;
172173
173174 return (
174175 < div >
175- < MutateButton />
176+ < MutateButton setter = { setter } />
176177 < div > { `${ name } says ${ flagValue } ` } </ div >
177178 </ div >
178179 ) ;
@@ -304,5 +305,37 @@ describe('OpenFeatureProvider', () => {
304305
305306 expect ( screen . getByText ( 'Will says aloha' ) ) . toBeInTheDocument ( ) ;
306307 } ) ;
308+
309+ it ( 'should accept a method taking the previous context' , async ( ) => {
310+ const DOMAIN = 'mutate-context-with-function' ;
311+ OpenFeature . setProvider ( DOMAIN , suspendingProvider ( ) , { done : false } ) ;
312+
313+ const setter = jest . fn ( ( prevContext :
EvaluationContext ) => ( { ...
prevContext , user :
'[email protected] ' } ) ) ; 314+ render (
315+ < OpenFeatureProvider domain = { DOMAIN } >
316+ < React . Suspense fallback = { < div > { FALLBACK } </ div > } >
317+ < TestComponent name = "Will" setter = { setter } />
318+ </ React . Suspense >
319+ </ OpenFeatureProvider > ,
320+ ) ;
321+
322+ await waitFor ( ( ) => {
323+ expect ( screen . getByText ( 'Will says hi' ) ) . toBeInTheDocument ( ) ;
324+ } ) ;
325+
326+ act ( ( ) => {
327+ fireEvent . click ( screen . getByText ( 'Update Context' ) ) ;
328+ } ) ;
329+ await waitFor (
330+ ( ) => {
331+ expect ( screen . getByText ( 'Will says aloha' ) ) . toBeInTheDocument ( ) ;
332+ } ,
333+ { timeout : DELAY * 4 } ,
334+ ) ;
335+
336+ expect ( setter ) . toHaveBeenCalledTimes ( 1 ) ;
337+ expect ( setter ) . toHaveBeenCalledWith ( { done : false } ) ;
338+ expect ( OpenFeature . getContext ( DOMAIN ) ) . toEqual ( { done :
false , user :
'[email protected] ' } ) ; 339+ } ) ;
307340 } ) ;
308341} ) ;
0 commit comments