Skip to content

Commit

Permalink
fix: Prerequisites should not trigger hooks. (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
kinyoklion authored Oct 17, 2024
1 parent 2d2accd commit 70cf3c3
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 1 deletion.
69 changes: 69 additions & 0 deletions packages/shared/sdk-client/__tests__/LDClientImpl.hooks.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { AutoEnvAttributes } from '@launchdarkly/js-sdk-common';
import { Hook, HookMetadata } from '../src/api';
import LDClientImpl from '../src/LDClientImpl';
import { createBasicPlatform } from './createBasicPlatform';
import * as mockResponseJson from './evaluation/mockResponse.json';
import { MockEventSource } from './streaming/LDClientImpl.mocks';
import { makeTestDataManagerFactory } from './TestDataManager';

it('should use hooks registered during configuration', async () => {
Expand Down Expand Up @@ -239,3 +241,70 @@ it('should execute both initial hooks and hooks added using addHook', async () =
},
);
});

it('should not execute hooks for prerequisite evaluations', async () => {
const testHook: Hook = {
beforeEvaluation: jest.fn(),
afterEvaluation: jest.fn(),
beforeIdentify: jest.fn(),
afterIdentify: jest.fn(),
getMetadata(): HookMetadata {
return {
name: 'test hook',
};
},
};

const platform = createBasicPlatform();
let mockEventSource: MockEventSource;
const simulatedEvents = [{ data: JSON.stringify(mockResponseJson) }];
platform.requests.createEventSource.mockImplementation(
(streamUri: string = '', options: any = {}) => {
mockEventSource = new MockEventSource(streamUri, options);
mockEventSource.simulateEvents('put', simulatedEvents);
return mockEventSource;
},
);

const factory = makeTestDataManagerFactory('sdk-key', platform);
const client = new LDClientImpl(
'sdk-key',
AutoEnvAttributes.Disabled,
platform,
{
sendEvents: false,
hooks: [testHook],
logger: {
debug: jest.fn(),
info: jest.fn(),
warn: jest.fn(),
error: jest.fn(),
},
},
factory,
);

await client.identify({ key: 'user-key' });
await client.variation('has-prereq-depth-1', false);

expect(testHook.beforeEvaluation).toHaveBeenCalledTimes(1);

expect(testHook.beforeEvaluation).toHaveBeenCalledWith(
{ context: { key: 'user-key' }, defaultValue: false, flagKey: 'has-prereq-depth-1' },
{},
);

expect(testHook.afterEvaluation).toHaveBeenCalledTimes(1);

expect(testHook.afterEvaluation).toHaveBeenCalledWith(
{ context: { key: 'user-key' }, defaultValue: false, flagKey: 'has-prereq-depth-1' },
{},
{
reason: {
kind: 'FALLTHROUGH',
},
value: true,
variationIndex: 0,
},
);
});
2 changes: 1 addition & 1 deletion packages/shared/sdk-client/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ export default class LDClientImpl implements LDClient {
}

prerequisites?.forEach((prereqKey) => {
this.variation(prereqKey, undefined);
this._variationInternal(prereqKey, undefined, this._eventFactoryDefault);
});
this._eventProcessor?.sendEvent(
eventFactory.evalEventClient(
Expand Down

0 comments on commit 70cf3c3

Please sign in to comment.