@@ -3,6 +3,8 @@ import { AutoEnvAttributes } from '@launchdarkly/js-sdk-common';
33import { Hook , HookMetadata } from '../src/api' ;
44import LDClientImpl from '../src/LDClientImpl' ;
55import { createBasicPlatform } from './createBasicPlatform' ;
6+ import * as mockResponseJson from './evaluation/mockResponse.json' ;
7+ import { MockEventSource } from './streaming/LDClientImpl.mocks' ;
68import { makeTestDataManagerFactory } from './TestDataManager' ;
79
810it ( '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+ } ) ;
0 commit comments