@@ -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,42 @@ 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+ const setter = jest . fn ( ( prevContext : EvaluationContext ) => {
312+ return { ...
prevContext , user :
'[email protected] ' } ; 313+ } ) ;
314+
315+ OpenFeature . setProvider ( DOMAIN , suspendingProvider ( ) , { done : false } ) ;
316+
317+ render (
318+ < OpenFeatureProvider domain = { DOMAIN } >
319+ < React . Suspense fallback = { < div > { FALLBACK } </ div > } >
320+ < TestComponent name = "Will" setter = { setter } />
321+ </ React . Suspense >
322+ </ OpenFeatureProvider > ,
323+ ) ;
324+
325+ await waitFor ( ( ) => {
326+ expect ( screen . getByText ( 'Will says hi' ) ) . toBeInTheDocument ( ) ;
327+ } ) ;
328+
329+ act ( ( ) => {
330+ const button = screen . getByText ( 'Update Context' ) ;
331+ fireEvent . click ( button ) ;
332+ } ) ;
333+
334+ await waitFor (
335+ ( ) => {
336+ expect ( screen . getByText ( 'Will says aloha' ) ) . toBeInTheDocument ( ) ;
337+ } ,
338+ { timeout : DELAY * 4 } ,
339+ ) ;
340+
341+ expect ( setter ) . toHaveBeenCalledTimes ( 1 ) ;
342+ expect ( setter ) . toHaveBeenCalledWith ( { done : false } ) ;
343+ expect ( OpenFeature . getContext ( DOMAIN ) ) . toEqual ( { done :
false , user :
'[email protected] ' } ) ; 344+ } ) ;
307345 } ) ;
308346} ) ;
0 commit comments