11import { CallbackClient , Code , ConnectError , PromiseClient } from '@bufbuild/connect-web' ;
22import { Struct } from '@bufbuild/protobuf' ;
3- import { Client , ErrorCode , OpenFeature , ProviderEvents , StandardResolutionReasons } from '@openfeature/web-sdk' ;
3+ import { Client , ErrorCode , JsonValue , OpenFeature , ProviderEvents , StandardResolutionReasons } from '@openfeature/web-sdk' ;
44import fetchMock from 'jest-fetch-mock' ;
55import { Service } from '../proto/ts/schema/v1/schema_connectweb' ;
66import { AnyFlag , EventStreamResponse , ResolveAllResponse } from '../proto/ts/schema/v1/schema_pb' ;
@@ -105,6 +105,10 @@ class MockPromiseClient implements Partial<PromiseClient<typeof Service>> {
105105}
106106
107107describe ( FlagdWebProvider . name , ( ) => {
108+ afterEach ( ( ) => {
109+ jest . clearAllMocks ( ) ;
110+ } ) ;
111+
108112 describe ( 'resolution functionality' , ( ) => {
109113 let client : Client ;
110114 beforeAll ( ( done ) => {
@@ -126,10 +130,6 @@ describe(FlagdWebProvider.name, () => {
126130 } ) ;
127131 } ) ;
128132
129- afterEach ( ( ) => {
130- fetchMock . resetMocks ( ) ;
131- } ) ;
132-
133133 it ( FlagdWebProvider . prototype . resolveBooleanEvaluation . name , async ( ) => {
134134 const details = client . getBooleanDetails ( BOOL_FLAG_KEY , false ) ;
135135 expect ( details . value ) . toEqual ( BOOL_FLAG_VALUE ) ;
@@ -159,13 +159,14 @@ describe(FlagdWebProvider.name, () => {
159159 } ) ;
160160 } ) ;
161161
162- describe ( 'events-enabled ' , ( ) => {
162+ describe ( 'events' , ( ) => {
163163 let client : Client ;
164164 let mockCallbackClient : MockCallbackClient ;
165+ const mockPromiseClient = new MockPromiseClient ( ) as unknown as PromiseClient < typeof Service > ;
166+ const context = { some : 'value' } ;
165167
166- beforeEach ( ( ) => {
168+ beforeEach ( async ( ) => {
167169 mockCallbackClient = new MockCallbackClient ( ) ;
168- const mockPromiseClient = new MockPromiseClient ( ) as unknown as PromiseClient < typeof Service > ;
169170 OpenFeature . setProvider (
170171 new FlagdWebProvider (
171172 { host : 'fake.com' , maxRetries : - 1 } ,
@@ -207,6 +208,23 @@ describe(FlagdWebProvider.name, () => {
207208 type : EVENT_CONFIGURATION_CHANGE ,
208209 } ) ;
209210 } ) ;
211+
212+ it ( 'should trigger call to resolveAll with current context' , ( done ) => {
213+
214+ client . addHandler ( ProviderEvents . ConfigurationChanged , ( ) => {
215+ try {
216+ expect ( mockPromiseClient . resolveAll ) . toHaveBeenLastCalledWith ( { context : Struct . fromJson ( context as JsonValue ) } ) ;
217+ done ( ) ;
218+ } catch ( err ) {
219+ done ( err ) ;
220+ }
221+ } ) ;
222+ OpenFeature . setContext ( context ) . then ( ( ) => {
223+ mockCallbackClient . mockMessage ( {
224+ type : EVENT_CONFIGURATION_CHANGE ,
225+ } ) ;
226+ } ) ;
227+ } ) ;
210228 } ) ;
211229
212230 describe ( ProviderEvents . Error , ( ) => {
0 commit comments