@@ -12,9 +12,12 @@ class MockProvider implements Provider {
1212 return Promise . resolve ( ) ;
1313 }
1414
15- resolveBooleanEvaluation ( ) : ResolutionDetails < boolean > {
16- throw new Error ( 'Not implemented' ) ;
17- }
15+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
16+ resolveBooleanEvaluation = jest . fn ( ( flagKey : string , defaultValue : boolean , context : EvaluationContext ) => {
17+ return {
18+ value : true
19+ } ;
20+ } ) ;
1821
1922 resolveNumberEvaluation ( ) : ResolutionDetails < number > {
2023 throw new Error ( 'Not implemented' ) ;
@@ -76,6 +79,57 @@ describe('Evaluation Context', () => {
7679 expect ( OpenFeature . getContext ( clientName ) ) . toEqual ( globalContext ) ;
7780 } ) ;
7881
82+ it ( 'should call onContextChange for appropriate provider with appropriate context' , async ( ) => {
83+ const globalContext : EvaluationContext = { scope : 'global' } ;
84+ const testContext : EvaluationContext = { scope : 'test' } ;
85+ const clientName = 'appropriateProviderTest' ;
86+ const defaultProvider = new MockProvider ( ) ;
87+ const provider1 = new MockProvider ( ) ;
88+
89+ await OpenFeature . setProviderAndWait ( defaultProvider ) ;
90+ await OpenFeature . setProviderAndWait ( clientName , provider1 ) ;
91+
92+ // Spy on context changed handlers of both providers
93+ const defaultProviderSpy = jest . spyOn ( defaultProvider , 'onContextChange' ) ;
94+ const provider1Spy = jest . spyOn ( provider1 , 'onContextChange' ) ;
95+
96+ await OpenFeature . setContext ( globalContext ) ;
97+ await OpenFeature . setContext ( clientName , testContext ) ;
98+
99+ // provider one should get global and specific context calls
100+ expect ( defaultProviderSpy ) . toHaveBeenCalledWith ( { } , globalContext ) ;
101+ expect ( provider1Spy ) . toHaveBeenCalledWith ( globalContext , testContext ) ;
102+ } ) ;
103+
104+ it ( 'should pass correct context to resolver' , async ( ) => {
105+ const globalContext : EvaluationContext = { scope : 'global' } ;
106+ const testContext : EvaluationContext = { scope : 'test' } ;
107+ const clientName = 'correctContextTest' ;
108+ const defaultProvider = new MockProvider ( ) ;
109+ const provider1 = new MockProvider ( ) ;
110+
111+ await OpenFeature . setProviderAndWait ( defaultProvider ) ;
112+ await OpenFeature . setProviderAndWait ( clientName , provider1 ) ;
113+
114+ // Spy on boolean resolvers of both providers
115+ const defaultProviderSpy = jest . spyOn ( defaultProvider , 'resolveBooleanEvaluation' ) ;
116+ const provider1Spy = jest . spyOn ( provider1 , 'resolveBooleanEvaluation' ) ;
117+
118+ await OpenFeature . setContext ( globalContext ) ;
119+ await OpenFeature . setContext ( clientName , testContext ) ;
120+
121+ const defaultClient = OpenFeature . getClient ( ) ;
122+ const provider1Client = OpenFeature . getClient ( clientName ) ;
123+
124+ const flagName = 'some-flag' ;
125+ defaultClient . getBooleanValue ( flagName , false ) ;
126+ provider1Client . getBooleanValue ( flagName , false ) ;
127+
128+ // provider one should get global and specific context
129+ expect ( defaultProviderSpy ) . toHaveBeenCalledWith ( flagName , false , globalContext , expect . anything ( ) ) ;
130+ expect ( provider1Spy ) . toHaveBeenCalledWith ( flagName , false , testContext , expect . anything ( ) ) ;
131+ } ) ;
132+
79133 it ( 'should only call a providers onContextChange once when clearing context' , async ( ) => {
80134 const globalContext : EvaluationContext = { scope : 'global' } ;
81135 const testContext : EvaluationContext = { scope : 'test' } ;
0 commit comments