Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewleap-optimizely committed Jan 12, 2024
1 parent b9b8356 commit 045ef85
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 21 deletions.
6 changes: 4 additions & 2 deletions tests/odpEventApiManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ const ODP_EVENTS = [

const API_KEY = 'test-api-key';
const API_HOST = 'https://odp.example.com';
const PIXEL_URL = 'https://odp.pixel.com';

const odpConfig = new OdpConfig(API_KEY, API_HOST, []);
const odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);

describe('NodeOdpEventApiManager', () => {
let mockLogger: LogHandler;
Expand Down Expand Up @@ -133,9 +134,10 @@ describe('NodeOdpEventApiManager', () => {
const updatedOdpConfig = new OdpConfig(
'updated-key',
'https://updatedhost.test',
'https://updatedpixel.test',
['updated-seg'],
)

manager.updateSettings(updatedOdpConfig);
await manager.sendEvents(ODP_EVENTS);

Expand Down
16 changes: 10 additions & 6 deletions tests/odpEventManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { UserAgentInfo } from '../lib/core/odp/user_agent_info';

const API_KEY = 'test-api-key';
const API_HOST = 'https://odp.example.com';
const PIXEL_URL = 'https://odp.pixel.com';
const MOCK_IDEMPOTENCE_ID = 'c1dc758e-f095-4f09-9b49-172d74c53880';
const EVENTS: OdpEvent[] = [
new OdpEvent(
Expand Down Expand Up @@ -145,7 +146,7 @@ describe('OdpEventManager', () => {
beforeAll(() => {
mockLogger = mock<LogHandler>();
mockApiManager = mock<IOdpEventApiManager>();
odpConfig = new OdpConfig(API_KEY, API_HOST, []);
odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);
logger = instance(mockLogger);
apiManager = instance(mockApiManager);
});
Expand All @@ -159,7 +160,7 @@ describe('OdpEventManager', () => {
when(mockApiManager.sendEvents(anything())).thenResolve(false);
when(mockApiManager.updateSettings(anything())).thenReturn(undefined);

const apiManager = instance(mockApiManager);
const apiManager = instance(mockApiManager);

const eventManager = new OdpEventManager({
odpConfig,
Expand All @@ -172,12 +173,12 @@ describe('OdpEventManager', () => {
const [passedConfig] = capture(mockApiManager.updateSettings).last();
expect(passedConfig).toEqual(odpConfig);
});

it('should update api manager setting with updatetd odp config on updateSettings', () => {
when(mockApiManager.sendEvents(anything())).thenResolve(false);
when(mockApiManager.updateSettings(anything())).thenReturn(undefined);

const apiManager = instance(mockApiManager);
const apiManager = instance(mockApiManager);

const eventManager = new OdpEventManager({
odpConfig,
Expand All @@ -190,9 +191,10 @@ describe('OdpEventManager', () => {
const updatedOdpConfig = new OdpConfig(
'updated-key',
'https://updatedhost.test',
'https://pixel.test',
['updated-seg'],
)

eventManager.updateSettings(updatedOdpConfig);

verify(mockApiManager.updateSettings(anything())).twice();
Expand Down Expand Up @@ -541,13 +543,15 @@ describe('OdpEventManager', () => {
});
const apiKey = 'testing-api-key';
const apiHost = 'https://some.other.example.com';
const pixelUrl = 'https://some.other.pixel.com';
const segmentsToCheck = ['empty-cart', '1-item-cart'];
const differentOdpConfig = new OdpConfig(apiKey, apiHost, segmentsToCheck);
const differentOdpConfig = new OdpConfig(apiKey, apiHost, pixelUrl, segmentsToCheck);

eventManager.updateSettings(differentOdpConfig);

expect(eventManager['odpConfig'].apiKey).toEqual(apiKey);
expect(eventManager['odpConfig'].apiHost).toEqual(apiHost);
expect(eventManager['odpConfig'].pixelUrl).toEqual(pixelUrl);
expect(eventManager['odpConfig'].segmentsToCheck).toContain(Array.from(segmentsToCheck)[0]);
expect(eventManager['odpConfig'].segmentsToCheck).toContain(Array.from(segmentsToCheck)[1]);
});
Expand Down
12 changes: 7 additions & 5 deletions tests/odpManager.browser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,19 @@ import { OdpEvent } from '../lib/core/odp/odp_event';

const keyA = 'key-a';
const hostA = 'host-a';
const pixelA = 'pixel-a';
const segmentsA = ['a'];
const userA = 'fs-user-a';
const vuidA = 'vuid_a';
const odpConfigA = new OdpConfig(keyA, hostA, segmentsA);
const odpConfigA = new OdpConfig(keyA, hostA, pixelA, segmentsA);

const keyB = 'key-b';
const hostB = 'host-b';
const pixelB = 'pixel-b';
const segmentsB = ['b'];
const userB = 'fs-user-b';
const vuidB = 'vuid_b';
const odpConfigB = new OdpConfig(keyB, hostB, segmentsB);
const odpConfigB = new OdpConfig(keyB, hostB, pixelB, segmentsB);

describe('OdpManager', () => {
let odpConfig: OdpConfig;
Expand Down Expand Up @@ -122,7 +124,7 @@ describe('OdpManager', () => {

verify(mockLogger.log(LogLevel.INFO, LOG_MESSAGES.ODP_DISABLED)).once();

browserOdpManager.updateSettings(new OdpConfig('valid', 'host', []));
browserOdpManager.updateSettings(new OdpConfig('valid', 'host', 'pixel-url', []));
expect(browserOdpManager.odpConfig).toBeUndefined;

await browserOdpManager.fetchQualifiedSegments('vuid_user1', []);
Expand Down Expand Up @@ -201,7 +203,7 @@ describe('OdpManager', () => {
},
});

const didUpdateA = browserOdpManager.updateSettings(new OdpConfig(keyA, hostA, segmentsA));
const didUpdateA = browserOdpManager.updateSettings(new OdpConfig(keyA, hostA, pixelA, segmentsA));
expect(didUpdateA).toBe(true);

browserOdpManager.fetchQualifiedSegments(vuidA);
Expand All @@ -213,7 +215,7 @@ describe('OdpManager', () => {
const fetchQualifiedSegmentsArgsA = capture(mockSegmentApiManager.fetchSegments).last();
expect(fetchQualifiedSegmentsArgsA).toStrictEqual([keyA, hostA, ODP_USER_KEY.VUID, vuidA, segmentsA]);

const didUpdateB = browserOdpManager.updateSettings(new OdpConfig(keyB, hostB, segmentsB));
const didUpdateB = browserOdpManager.updateSettings(new OdpConfig(keyB, hostB, pixelB, segmentsB));
expect(didUpdateB).toBe(true);

browserOdpManager.fetchQualifiedSegments(vuidB);
Expand Down
16 changes: 11 additions & 5 deletions tests/odpManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ import { ServerLRUCache } from '../lib/utils/lru_cache';

const keyA = 'key-a';
const hostA = 'host-a';
const pixelA = 'pixel-a';
const segmentsA = ['a'];
const userA = 'fs-user-a';

const keyB = 'key-b';
const hostB = 'host-b';
const pixelB = 'pixel-b';
const segmentsB = ['b'];
const userB = 'fs-user-b';

Expand Down Expand Up @@ -108,7 +110,7 @@ describe('OdpManager', () => {
});
verify(mockLogger.log(LogLevel.INFO, LOG_MESSAGES.ODP_DISABLED)).once();

odpManager.updateSettings(new OdpConfig('valid', 'host', []));
odpManager.updateSettings(new OdpConfig('valid', 'host', 'pixel-url', []));
expect(odpManager.odpConfig).toBeUndefined;

await odpManager.fetchQualifiedSegments('user1', []);
Expand Down Expand Up @@ -152,18 +154,20 @@ describe('OdpManager', () => {
},
});

odpManager.updateSettings(new OdpConfig(keyA, hostA, segmentsA));
odpManager.updateSettings(new OdpConfig(keyA, hostA, pixelA, segmentsA));

expect(odpManager.odpConfig.apiKey).toBe(keyA);
expect(odpManager.odpConfig.apiHost).toBe(hostA);
expect(odpManager.odpConfig.pixelUrl).toBe(pixelA);

// odpManager.identifyUser(userA);

// verify(mockEventApiManager.sendEvents(keyA, hostA, anything())).once();

odpManager.updateSettings(new OdpConfig(keyB, hostB, segmentsB));
odpManager.updateSettings(new OdpConfig(keyB, hostB, pixelB, segmentsB));
expect(odpManager.odpConfig.apiKey).toBe(keyB);
expect(odpManager.odpConfig.apiHost).toBe(hostB);
expect(odpManager.odpConfig.pixelUrl).toBe(pixelB);

// odpManager.identifyUser(userB);

Expand All @@ -179,17 +183,19 @@ describe('OdpManager', () => {
},
});

odpManager.updateSettings(new OdpConfig(keyA, hostA, segmentsA));
odpManager.updateSettings(new OdpConfig(keyA, hostA, pixelA, segmentsA));

expect(odpManager.odpConfig.apiKey).toBe(keyA);
expect(odpManager.odpConfig.apiHost).toBe(hostA);
expect(odpManager.odpConfig.pixelUrl).toBe(pixelA);

await odpManager.fetchQualifiedSegments(userA);
verify(mockSegmentApiManager.fetchSegments(keyA, hostA, ODP_USER_KEY.FS_USER_ID, userA, anything())).once();

odpManager.updateSettings(new OdpConfig(keyB, hostB, segmentsB));
odpManager.updateSettings(new OdpConfig(keyB, hostB, pixelB, segmentsB));
expect(odpManager.odpConfig.apiKey).toBe(keyB);
expect(odpManager.odpConfig.apiHost).toBe(hostB);
expect(odpManager.odpConfig.pixelUrl).toBe(pixelB);

await odpManager.fetchQualifiedSegments(userB);
verify(mockSegmentApiManager.fetchSegments(keyB, hostB, ODP_USER_KEY.FS_USER_ID, userB, anything())).once();
Expand Down
7 changes: 4 additions & 3 deletions tests/odpSegmentManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,17 @@ describe('OdpSegmentManager', () => {
const userKey: ODP_USER_KEY = ODP_USER_KEY.VUID;
const userValue = 'test-user';

const validTestOdpConfig = new OdpConfig('valid-key', 'host', ['new-customer']);
const invalidTestOdpConfig = new OdpConfig('invalid-key', 'host', ['new-customer']);
const validTestOdpConfig = new OdpConfig('valid-key', 'host', 'pixel-url', ['new-customer']);
const invalidTestOdpConfig = new OdpConfig('invalid-key', 'host', 'pixel-url', ['new-customer']);

beforeEach(() => {
resetCalls(mockLogHandler);
resetCalls(mockRequestHandler);

const API_KEY = 'test-api-key';
const API_HOST = 'https://odp.example.com';
odpConfig = new OdpConfig(API_KEY, API_HOST, []);
const PIXEL_URL = 'https://odp.pixel.com';
odpConfig = new OdpConfig(API_KEY, API_HOST, PIXEL_URL, []);
const segmentsCache = new LRUCache<string, string[]>({
maxSize: 1000,
timeout: 1000,
Expand Down

0 comments on commit 045ef85

Please sign in to comment.