|
| 1 | +import { IAssignmentEvent } from './assignment-logger'; |
| 2 | +import EppoClient from './client/eppo-client'; |
| 3 | +import { IConfigurationStore } from './configuration-store/configuration-store'; |
| 4 | +import { EppoAssignmentLogger } from './eppo-assignment-logger'; |
| 5 | +import { Flag } from './interfaces'; |
| 6 | + |
| 7 | +jest.mock('./client/eppo-client'); |
| 8 | + |
| 9 | +describe('EppoAssignmentLogger', () => { |
| 10 | + let mockEppoClient: jest.Mocked<EppoClient>; |
| 11 | + let logger: EppoAssignmentLogger; |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + jest.clearAllMocks(); |
| 15 | + mockEppoClient = new EppoClient({ |
| 16 | + flagConfigurationStore: {} as IConfigurationStore<Flag>, |
| 17 | + }) as jest.Mocked<EppoClient>; |
| 18 | + mockEppoClient.track = jest.fn(); |
| 19 | + logger = new EppoAssignmentLogger(mockEppoClient); |
| 20 | + }); |
| 21 | + |
| 22 | + it('should log assignment events correctly', () => { |
| 23 | + // Arrange |
| 24 | + const assignmentEvent: IAssignmentEvent = { |
| 25 | + subject: 'user-123', |
| 26 | + experiment: 'experiment-abc', |
| 27 | + variant: 'control', |
| 28 | + entityId: 456, // Changed to number |
| 29 | + holdoutKey: 'holdout-xyz', |
| 30 | + holdoutVariation: 'holdout-variant-1', |
| 31 | + // Add required properties based on the IAssignmentEvent interface |
| 32 | + allocation: 'allocation-1', |
| 33 | + featureFlag: 'feature-flag-1', |
| 34 | + variation: 'variation-1', |
| 35 | + timestamp: new Date().toISOString(), |
| 36 | + subjectAttributes: {}, |
| 37 | + flagKey: 'flag-key-1', |
| 38 | + format: 'json', |
| 39 | + evaluationDetails: null, |
| 40 | + }; |
| 41 | + |
| 42 | + // Act |
| 43 | + logger.logAssignment(assignmentEvent); |
| 44 | + |
| 45 | + // Assert |
| 46 | + expect(mockEppoClient.track).toHaveBeenCalledTimes(1); |
| 47 | + expect(mockEppoClient.track).toHaveBeenCalledWith('__eppo_assignment', { |
| 48 | + subject_id: 'user-123', |
| 49 | + experiment: 'experiment-abc', |
| 50 | + variant: 'control', |
| 51 | + entity_id: 456, |
| 52 | + holdout: 'holdout-xyz', |
| 53 | + holdout_variant: 'holdout-variant-1', |
| 54 | + }); |
| 55 | + }); |
| 56 | + |
| 57 | + it('should handle missing optional fields', () => { |
| 58 | + // Arrange |
| 59 | + const assignmentEvent: IAssignmentEvent = { |
| 60 | + subject: 'user-123', |
| 61 | + experiment: 'experiment-abc', |
| 62 | + variant: 'control', |
| 63 | + // Add required properties based on the IAssignmentEvent interface |
| 64 | + allocation: 'allocation-1', |
| 65 | + featureFlag: 'feature-flag-1', |
| 66 | + variation: 'variation-1', |
| 67 | + timestamp: new Date().toISOString(), |
| 68 | + subjectAttributes: {}, |
| 69 | + flagKey: 'flag-key-1', |
| 70 | + format: 'json', |
| 71 | + evaluationDetails: null, |
| 72 | + }; |
| 73 | + |
| 74 | + // Act |
| 75 | + logger.logAssignment(assignmentEvent); |
| 76 | + |
| 77 | + // Assert |
| 78 | + expect(mockEppoClient.track).toHaveBeenCalledTimes(1); |
| 79 | + expect(mockEppoClient.track).toHaveBeenCalledWith('__eppo_assignment', { |
| 80 | + subject_id: 'user-123', |
| 81 | + experiment: 'experiment-abc', |
| 82 | + variant: 'control', |
| 83 | + entity_id: undefined, |
| 84 | + holdout: undefined, |
| 85 | + holdout_variant: undefined, |
| 86 | + }); |
| 87 | + }); |
| 88 | +}); |
0 commit comments