Skip to content

Commit

Permalink
fix: wrong context passed to named provider
Browse files Browse the repository at this point in the history
Signed-off-by: Todd Baert <[email protected]>
  • Loading branch information
toddbaert committed Jan 10, 2024
1 parent 072d2c7 commit 4fbc710
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/client/src/client/open-feature-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export class OpenFeatureClient implements Client {
const allHooksReversed = [...allHooks].reverse();

const context = {
...OpenFeature.getContext(),
...OpenFeature.getContext(this?.options?.name),
};

// this reference cannot change during the course of evaluation
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/open-feature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export class OpenFeatureAPI extends OpenFeatureCommonAPI<Provider, Hook> impleme
* @param {string} clientName The name to identify the client
* @returns {EvaluationContext} Evaluation context
*/
getContext(clientName: string): EvaluationContext;
getContext(clientName?: string): EvaluationContext;
getContext(nameOrUndefined?: string): EvaluationContext {
const clientName = stringOrUndefined(nameOrUndefined);
if (clientName) {
Expand Down
22 changes: 22 additions & 0 deletions packages/client/test/evaluation-context.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,28 @@ describe('Evaluation Context', () => {
expect(OpenFeature.getContext(clientName)).toEqual(globalContext);
});

it('should call onContextChange for appropriate provider with appropriate context', async () => {
const globalContext: EvaluationContext = { scope: 'global' };
const testContext: EvaluationContext = { scope: 'test' };
const clientName = 'test';
const defaultProvider = new MockProvider();
const provider1 = new MockProvider();

await OpenFeature.setProviderAndWait(defaultProvider);
await OpenFeature.setProviderAndWait(clientName, provider1);

// Spy on context changed handlers of both providers
const defaultProviderSpy = jest.spyOn(defaultProvider, 'onContextChange');
const provider1Spy = jest.spyOn(provider1, 'onContextChange');

await OpenFeature.setContext(globalContext);
await OpenFeature.setContext(clientName, testContext);

// provider one should get global and specific context calls
expect(defaultProviderSpy).toHaveBeenCalledWith({}, globalContext);
expect(provider1Spy).toHaveBeenCalledWith(globalContext, testContext);
});

it('should only call a providers onContextChange once when clearing context', async () => {
const globalContext: EvaluationContext = { scope: 'global' };
const testContext: EvaluationContext = { scope: 'test' };
Expand Down

0 comments on commit 4fbc710

Please sign in to comment.