@@ -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,78 @@ 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
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
324
+ onPreAssignment ( subject : string ) : Promise < string > {
325
+ return Promise . resolve ( 'my-overridden-variation' ) ;
326
+ } ,
327
+
328
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
329
+ onPostAssignment ( variation : string ) : Promise < void > {
330
+ return Promise . resolve ( ) ;
331
+ } ,
332
+ } ,
333
+ ) ;
334
+
335
+ expect ( variation ) . toEqual ( 'my-overridden-variation' ) ;
336
+ } ) ;
337
+
338
+ it ( 'uses regular assignment logic if onPreAssignment returns null' , async ( ) => {
339
+ const variation = await client . getAssignmentWithHooks (
340
+ 'subject-identifer' ,
341
+ experimentName ,
342
+ { } ,
343
+ {
344
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
345
+ onPreAssignment ( subject : string ) : Promise < string | null > {
346
+ return Promise . resolve ( null ) ;
347
+ } ,
348
+
349
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
350
+ onPostAssignment ( variation : string ) : Promise < void > {
351
+ return Promise . resolve ( ) ;
352
+ } ,
353
+ } ,
354
+ ) ;
355
+
356
+ expect ( variation ) . not . toEqual ( null ) ;
357
+ } ) ;
358
+ } ) ;
359
+
360
+ describe ( 'onPostAssignment' , ( ) => {
361
+ it ( 'called with assigned variation after assignment' , async ( ) => {
362
+ const mockHooks = td . object < IAssignmentHooks > ( ) ;
363
+ const variation = await client . getAssignmentWithHooks (
364
+ 'subject-identifer' ,
365
+ experimentName ,
366
+ { } ,
367
+ mockHooks ,
368
+ ) ;
369
+ expect ( td . explain ( mockHooks . onPostAssignment ) . callCount ) . toEqual ( 1 ) ;
370
+ expect ( td . explain ( mockHooks . onPostAssignment ) . calls [ 0 ] . args [ 0 ] ) . toEqual ( variation ) ;
371
+ } ) ;
372
+ } ) ;
373
+ } ) ;
299
374
} ) ;
0 commit comments