@@ -150,6 +150,58 @@ it('allFlagsState() forwards options to base client', async () => {
150150 expect ( client . allFlagsState ) . toHaveBeenCalledWith ( context , options ) ;
151151} ) ;
152152
153+ describe ( 'given a client with forContext' , ( ) => {
154+ function makeMockScopedBaseClient ( ) {
155+ const base = makeMockBaseClient ( ) ;
156+ const forContext = jest . fn ( ( ctx : LDContext ) => ( {
157+ currentContext : ( ) => ctx ,
158+ boolVariation : ( key : string , def : boolean ) => base . boolVariation ( key , ctx , def ) ,
159+ numberVariation : ( key : string , def : number ) => base . numberVariation ( key , ctx , def ) ,
160+ stringVariation : ( key : string , def : string ) => base . stringVariation ( key , ctx , def ) ,
161+ jsonVariation : ( key : string , def : unknown ) => base . jsonVariation ( key , ctx , def ) ,
162+ boolVariationDetail : ( key : string , def : boolean ) => base . boolVariationDetail ( key , ctx , def ) ,
163+ numberVariationDetail : ( key : string , def : number ) =>
164+ base . numberVariationDetail ( key , ctx , def ) ,
165+ stringVariationDetail : ( key : string , def : string ) =>
166+ base . stringVariationDetail ( key , ctx , def ) ,
167+ jsonVariationDetail : ( key : string , def : unknown ) => base . jsonVariationDetail ( key , ctx , def ) ,
168+ allFlagsState : ( options ?: LDFlagsStateOptions ) => base . allFlagsState ( ctx , options ) ,
169+ } ) ) ;
170+ return { ...base , forContext } as any ;
171+ }
172+
173+ it ( 'uses forContext when available' , ( ) => {
174+ const client = makeMockScopedBaseClient ( ) ;
175+ createLDServerSession ( client , context ) ;
176+ expect ( client . forContext ) . toHaveBeenCalledWith ( context , {
177+ wrapperName : 'react-client-sdk' ,
178+ wrapperVersion : '0.0.0' ,
179+ } ) ;
180+ } ) ;
181+
182+ it ( 'delegates boolVariation through scoped client' , async ( ) => {
183+ const client = makeMockScopedBaseClient ( ) ;
184+ client . boolVariation . mockResolvedValue ( true ) ;
185+ const session = createLDServerSession ( client , context ) ;
186+ const result = await session . boolVariation ( 'my-flag' , false ) ;
187+ expect ( result ) . toBe ( true ) ;
188+ expect ( client . boolVariation ) . toHaveBeenCalledWith ( 'my-flag' , context , false ) ;
189+ } ) ;
190+
191+ it ( 'getContext() returns the scoped client currentContext' , ( ) => {
192+ const client = makeMockScopedBaseClient ( ) ;
193+ const session = createLDServerSession ( client , context ) ;
194+ expect ( session . getContext ( ) ) . toEqual ( context ) ;
195+ } ) ;
196+
197+ it ( 'initialized() delegates to the base client' , ( ) => {
198+ const client = makeMockScopedBaseClient ( ) ;
199+ client . initialized . mockReturnValue ( false ) ;
200+ const session = createLDServerSession ( client , context ) ;
201+ expect ( session . initialized ( ) ) . toBe ( false ) ;
202+ } ) ;
203+ } ) ;
204+
153205describe ( 'given a browser environment (window defined)' , ( ) => {
154206 let originalWindow : typeof globalThis . window ;
155207
0 commit comments