@@ -150,6 +150,55 @@ 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 , { wrapperName : 'react-server-sdk' } ) ;
177+ } ) ;
178+
179+ it ( 'delegates boolVariation through scoped client' , async ( ) => {
180+ const client = makeMockScopedBaseClient ( ) ;
181+ client . boolVariation . mockResolvedValue ( true ) ;
182+ const session = createLDServerSession ( client , context ) ;
183+ const result = await session . boolVariation ( 'my-flag' , false ) ;
184+ expect ( result ) . toBe ( true ) ;
185+ expect ( client . boolVariation ) . toHaveBeenCalledWith ( 'my-flag' , context , false ) ;
186+ } ) ;
187+
188+ it ( 'getContext() returns the scoped client currentContext' , ( ) => {
189+ const client = makeMockScopedBaseClient ( ) ;
190+ const session = createLDServerSession ( client , context ) ;
191+ expect ( session . getContext ( ) ) . toEqual ( context ) ;
192+ } ) ;
193+
194+ it ( 'initialized() delegates to the base client' , ( ) => {
195+ const client = makeMockScopedBaseClient ( ) ;
196+ client . initialized . mockReturnValue ( false ) ;
197+ const session = createLDServerSession ( client , context ) ;
198+ expect ( session . initialized ( ) ) . toBe ( false ) ;
199+ } ) ;
200+ } ) ;
201+
153202describe ( 'given a browser environment (window defined)' , ( ) => {
154203 let originalWindow : typeof globalThis . window ;
155204
0 commit comments