Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Prerequisites should not trigger hooks. #628

Merged
merged 1 commit into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Variation internal will not execute hooks. We do not use the detailed evaluation factory. (Equivalent to calling variation.)

});
this._eventProcessor?.sendEvent(
eventFactory.evalEventClient(
Expand Down
Loading