@@ -10,6 +10,7 @@ import {
10
10
readAssignmentTestData ,
11
11
readMockRacResponse ,
12
12
} from '../../test/testHelpers' ;
13
+ import { IAssignmentHooks } from '../assignment-hooks' ;
13
14
import { IAssignmentLogger } from '../assignment-logger' ;
14
15
import { IConfigurationStore } from '../configuration-store' ;
15
16
import { MAX_EVENT_QUEUE_SIZE } from '../constants' ;
@@ -296,4 +297,76 @@ describe('EppoClient E2E test', () => {
296
297
return globalClient . getAssignment ( subject . subjectKey , experiment , subject . subjectAttributes ) ;
297
298
} ) ;
298
299
}
300
+
301
+ describe ( 'getAssignmentWithHooks' , ( ) => {
302
+ let client : EppoClient ;
303
+
304
+ beforeAll ( ( ) => {
305
+ storage . setEntries ( { [ experimentName ] : mockExperimentConfig } ) ;
306
+ client = new EppoClient ( storage ) ;
307
+ } ) ;
308
+
309
+ describe ( 'onPreAssignment' , ( ) => {
310
+ it ( 'called with subject ID' , ( ) => {
311
+ const mockHooks = td . object < IAssignmentHooks > ( ) ;
312
+ client . getAssignmentWithHooks ( 'subject-identifer' , experimentName , { } , mockHooks ) ;
313
+ expect ( td . explain ( mockHooks . onPreAssignment ) . callCount ) . toEqual ( 1 ) ;
314
+ expect ( td . explain ( mockHooks . onPreAssignment ) . calls [ 0 ] . args [ 0 ] ) . toEqual ( 'subject-identifer' ) ;
315
+ } ) ;
316
+
317
+ it ( 'overrides returned assignment' , async ( ) => {
318
+ const variation = await client . getAssignmentWithHooks (
319
+ 'subject-identifer' ,
320
+ experimentName ,
321
+ { } ,
322
+ {
323
+ onPreAssignment ( subject : string ) : Promise < string > {
324
+ return Promise . resolve ( 'my-overridden-variation' ) ;
325
+ } ,
326
+
327
+ onPostAssignment ( variation : string ) : Promise < void > {
328
+ return Promise . resolve ( ) ;
329
+ } ,
330
+ } ,
331
+ ) ;
332
+
333
+ expect ( variation ) . toEqual ( 'my-overridden-variation' ) ;
334
+ } ) ;
335
+
336
+ it ( 'uses regular assignment logic if onPreAssignment returns null' , async ( ) => {
337
+ const variation = await client . getAssignmentWithHooks (
338
+ 'subject-identifer' ,
339
+ experimentName ,
340
+ { } ,
341
+ {
342
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
343
+ onPreAssignment ( subject : string ) : Promise < string | null > {
344
+ return Promise . resolve ( null ) ;
345
+ } ,
346
+
347
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
348
+ onPostAssignment ( variation : string ) : Promise < void > {
349
+ return Promise . resolve ( ) ;
350
+ } ,
351
+ } ,
352
+ ) ;
353
+
354
+ expect ( variation ) . not . toEqual ( null ) ;
355
+ } ) ;
356
+ } ) ;
357
+
358
+ describe ( 'onPostAssignment' , ( ) => {
359
+ it ( 'called with assigned variation after assignment' , async ( ) => {
360
+ const mockHooks = td . object < IAssignmentHooks > ( ) ;
361
+ const variation = await client . getAssignmentWithHooks (
362
+ 'subject-identifer' ,
363
+ experimentName ,
364
+ { } ,
365
+ mockHooks ,
366
+ ) ;
367
+ expect ( td . explain ( mockHooks . onPostAssignment ) . callCount ) . toEqual ( 1 ) ;
368
+ expect ( td . explain ( mockHooks . onPostAssignment ) . calls [ 0 ] . args [ 0 ] ) . toEqual ( variation ) ;
369
+ } ) ;
370
+ } ) ;
371
+ } ) ;
299
372
} ) ;
0 commit comments