Skip to content

Commit 28f44b8

Browse files
committed
fix: Prerequisites should not trigger hooks.
1 parent 2d2accd commit 28f44b8

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

packages/shared/sdk-client/__tests__/LDClientImpl.hooks.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { AutoEnvAttributes } from '@launchdarkly/js-sdk-common';
33
import { Hook, HookMetadata } from '../src/api';
44
import LDClientImpl from '../src/LDClientImpl';
55
import { createBasicPlatform } from './createBasicPlatform';
6+
import * as mockResponseJson from './evaluation/mockResponse.json';
7+
import { MockEventSource } from './streaming/LDClientImpl.mocks';
68
import { makeTestDataManagerFactory } from './TestDataManager';
79

810
it('should use hooks registered during configuration', async () => {
@@ -239,3 +241,70 @@ it('should execute both initial hooks and hooks added using addHook', async () =
239241
},
240242
);
241243
});
244+
245+
it('should not execute hooks for prerequisite evaluations', async () => {
246+
const testHook: Hook = {
247+
beforeEvaluation: jest.fn(),
248+
afterEvaluation: jest.fn(),
249+
beforeIdentify: jest.fn(),
250+
afterIdentify: jest.fn(),
251+
getMetadata(): HookMetadata {
252+
return {
253+
name: 'test hook',
254+
};
255+
},
256+
};
257+
258+
const platform = createBasicPlatform();
259+
let mockEventSource: MockEventSource;
260+
const simulatedEvents = [{ data: JSON.stringify(mockResponseJson) }];
261+
platform.requests.createEventSource.mockImplementation(
262+
(streamUri: string = '', options: any = {}) => {
263+
mockEventSource = new MockEventSource(streamUri, options);
264+
mockEventSource.simulateEvents('put', simulatedEvents);
265+
return mockEventSource;
266+
},
267+
);
268+
269+
const factory = makeTestDataManagerFactory('sdk-key', platform);
270+
const client = new LDClientImpl(
271+
'sdk-key',
272+
AutoEnvAttributes.Disabled,
273+
platform,
274+
{
275+
sendEvents: false,
276+
hooks: [testHook],
277+
logger: {
278+
debug: jest.fn(),
279+
info: jest.fn(),
280+
warn: jest.fn(),
281+
error: jest.fn(),
282+
},
283+
},
284+
factory,
285+
);
286+
287+
await client.identify({ key: 'user-key' });
288+
await client.variation('has-prereq-depth-1', false);
289+
290+
expect(testHook.beforeEvaluation).toHaveBeenCalledTimes(1);
291+
292+
expect(testHook.beforeEvaluation).toHaveBeenCalledWith(
293+
{ context: { key: 'user-key' }, defaultValue: false, flagKey: 'has-prereq-depth-1' },
294+
{},
295+
);
296+
297+
expect(testHook.afterEvaluation).toHaveBeenCalledTimes(1);
298+
299+
expect(testHook.afterEvaluation).toHaveBeenCalledWith(
300+
{ context: { key: 'user-key' }, defaultValue: false, flagKey: 'has-prereq-depth-1' },
301+
{},
302+
{
303+
reason: {
304+
kind: 'FALLTHROUGH',
305+
},
306+
value: true,
307+
variationIndex: 0,
308+
},
309+
);
310+
});

packages/shared/sdk-client/src/LDClientImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ export default class LDClientImpl implements LDClient {
357357
}
358358

359359
prerequisites?.forEach((prereqKey) => {
360-
this.variation(prereqKey, undefined);
360+
this._variationInternal(prereqKey, undefined, this._eventFactoryDefault);
361361
});
362362
this._eventProcessor?.sendEvent(
363363
eventFactory.evalEventClient(

0 commit comments

Comments
 (0)