Skip to content

Commit ec3c3d3

Browse files
Update tests
1 parent 56b890a commit ec3c3d3

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed

src/__tests__/asyncActions.browser.test.ts

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -293,14 +293,13 @@ describe('getTreatments', () => {
293293
expect(store.getActions().length).toBe(4);
294294

295295
// getting the evaluation result and validating it matches the results from SDK calls
296-
const treatments = action.payload.treatments;
297296
expect(splitSdk.factory.client().getTreatmentsWithConfig).toHaveBeenNthCalledWith(3, ['split1'], undefined);
298297
expect(splitSdk.factory.client().getTreatmentsWithConfig).toHaveBeenNthCalledWith(4, ['split2', 'split3'], attributes);
299298
const expectedTreatments = {
300299
...(splitSdk.factory.client().getTreatmentsWithConfig as jest.Mock).mock.results[2].value,
301300
...(splitSdk.factory.client().getTreatmentsWithConfig as jest.Mock).mock.results[3].value,
302301
};
303-
expect(treatments).toEqual(expectedTreatments);
302+
expect(action.payload.treatments).toEqual(expectedTreatments);
304303

305304
expect(splitSdk.factory.client().getTreatmentsWithConfig).toBeCalledTimes(4); // control assertion - getTreatmentsWithConfig calls
306305
expect(getClient(splitSdk).evalOnUpdate).toEqual({}); // control assertion - cbs scheduled for update
@@ -524,41 +523,49 @@ describe('getTreatments', () => {
524523
// Init SDK and set ready
525524
const store = mockStore(STATE_INITIAL);
526525
const actionResult = store.dispatch<any>(initSplitSdk({ config: sdkBrowserConfig }));
526+
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE);
527527
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);
528528

529529
actionResult.then(() => {
530-
// SPLIT_READY should have been dispatched
531-
expect(store.getActions().length).toBe(1);
532-
let action = store.getActions()[0];
530+
// SDK_READY_FROM_CACHE & SPLIT_READY should have been dispatched
531+
expect(store.getActions()).toEqual([{
532+
type: SPLIT_READY_FROM_CACHE, payload: { timestamp: expect.any(Number) }
533+
}, {
534+
type: SPLIT_READY, payload: { timestamp: expect.any(Number) }
535+
}]);
536+
537+
// If getTreatment is dispatched for a different user key, the item is added to the 'evalOnReady' list of the new client
538+
store.dispatch<any>(getTreatments({ splitNames: 'split2', key: 'other-user-key' }));
539+
expect(getClient(splitSdk).evalOnReady.length).toEqual(0); // control assertion - no evaluations were registered for SDK_READY on main client
540+
expect(getClient(splitSdk, 'other-user-key').evalOnReady.length).toEqual(1); // control assertion - 1 evaluation was registered for SDK_READY on the new client
541+
expect(getClient(splitSdk).evalOnUpdate).toEqual({});
542+
543+
// If SDK was ready from cache, the SPLIT_READY_FROM_CACHE action is dispatched for the new clients
544+
let action = store.getActions()[2];
533545
expect(action).toEqual({
534-
type: SPLIT_READY,
546+
type: SPLIT_READY_FROM_CACHE,
535547
payload: {
548+
key: 'other-user-key',
536549
timestamp: expect.any(Number)
537550
}
538551
});
539552

540-
// If SDK is ready for the main key and a getTreatment is dispatched for a different user key:
541-
// the item is added to the 'evalOnReady' list of the new client,
542-
store.dispatch<any>(getTreatments({ splitNames: 'split2', key: 'other-user-key' }));
543-
expect(getClient(splitSdk).evalOnReady.length).toEqual(0); // control assertion - no evaluations were registered for SDK_READY on main client
544-
expect(getClient(splitSdk, 'other-user-key').evalOnReady.length).toEqual(1); // control assertion - 1 evaluation was registered for SDK_READY on the new client
545-
expect(getClient(splitSdk).evalOnUpdate).toEqual({});
546-
547-
// and an ADD_TREATMENTS action is dispatched with control treatments without calling SDK client.
548-
action = store.getActions()[1];
553+
// and an ADD_TREATMENTS action is dispatched calling SDK client to evaluate from cache
554+
action = store.getActions()[3];
549555
expect(action).toEqual({
550556
type: ADD_TREATMENTS,
551557
payload: {
552558
key: 'other-user-key',
553-
treatments: getControlTreatmentsWithConfig(['split2'])
559+
treatments: expect.any(Object)
554560
}
555561
});
556-
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).toBeCalledTimes(0);
562+
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).lastCalledWith(['split2'], undefined);
563+
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).toHaveLastReturnedWith(action.payload.treatments);
557564

558565
(splitSdk.factory as any).client('other-user-key').__emitter__.emit(Event.SDK_READY, 'other-user-key');
559566

560567
// The SPLIT_READY_WITH_EVALUATIONS action is dispatched synchronously once the SDK is ready for the new user key
561-
action = store.getActions()[2];
568+
action = store.getActions()[4];
562569
expect(action).toEqual({
563570
type: SPLIT_READY_WITH_EVALUATIONS,
564571
payload: {
@@ -570,16 +577,15 @@ describe('getTreatments', () => {
570577
});
571578

572579
// getting the evaluation result and validating it matches the results from SDK
573-
const treatments = action.payload.treatments;
574580
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).lastCalledWith(['split2'], undefined);
575-
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).toHaveLastReturnedWith(treatments);
581+
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).toHaveLastReturnedWith(action.payload.treatments);
576582

577583
expect(getClient(splitSdk).evalOnUpdate).toEqual({}); // control assertion
578584

579585
// The getTreatments is dispatched again, but this time is evaluated with attributes and registered for 'evalOnUpdate'
580586
const attributes = { att1: 'att1' };
581587
store.dispatch<any>(getTreatments({ splitNames: 'split2', attributes, key: 'other-user-key', evalOnUpdate: true }));
582-
action = store.getActions()[3];
588+
action = store.getActions()[5];
583589
expect(action).toEqual({
584590
type: ADD_TREATMENTS,
585591
payload: {
@@ -592,7 +598,7 @@ describe('getTreatments', () => {
592598

593599
// The SPLIT_UPDATE_WITH_EVALUATIONS action is dispatched when the SDK is updated for the new user key
594600
(splitSdk.factory as any).client('other-user-key').__emitter__.emit(Event.SDK_UPDATE);
595-
action = store.getActions()[4];
601+
action = store.getActions()[6];
596602
expect(action).toEqual({
597603
type: SPLIT_UPDATE_WITH_EVALUATIONS,
598604
payload: {
@@ -608,7 +614,7 @@ describe('getTreatments', () => {
608614

609615
// We deregister the item from evalOnUpdate.
610616
store.dispatch<any>(getTreatments({ splitNames: 'split2', key: 'other-user-key', evalOnUpdate: false }));
611-
action = store.getActions()[5];
617+
action = store.getActions()[7];
612618
expect(action).toEqual({
613619
type: ADD_TREATMENTS,
614620
payload: {
@@ -622,7 +628,7 @@ describe('getTreatments', () => {
622628

623629
// Now, SDK_UPDATE events do not trigger SPLIT_UPDATE_WITH_EVALUATIONS but SPLIT_UPDATE instead
624630
(splitSdk.factory as any).client('other-user-key').__emitter__.emit(Event.SDK_UPDATE);
625-
action = store.getActions()[6];
631+
action = store.getActions()[8];
626632
expect(action).toEqual({
627633
type: SPLIT_UPDATE,
628634
payload: {
@@ -631,8 +637,8 @@ describe('getTreatments', () => {
631637
}
632638
});
633639

634-
expect(store.getActions().length).toBe(7); // control assertion - no more actions after the update.
635-
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).toBeCalledTimes(4); // control assertion - called 4 times, in actions SPLIT_READY_FROM_CACHE_WITH_EVALUATIONS, SPLIT_READY_WITH_EVALUATIONS, SPLIT_UPDATE_WITH_EVALUATIONS and ADD_TREATMENTS.
640+
expect(store.getActions().length).toBe(9); // control assertion - no more actions after the update.
641+
expect(splitSdk.factory.client('other-user-key').getTreatmentsWithConfig).toBeCalledTimes(5); // control assertion - called 5 times, in actions ADD_TREATMENTS, SPLIT_READY_FROM_CACHE_WITH_EVALUATIONS, SPLIT_READY_WITH_EVALUATIONS, SPLIT_UPDATE_WITH_EVALUATIONS and ADD_TREATMENTS.
636642

637643
done();
638644
});

src/__tests__/helpers.browser.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,17 +188,17 @@ describe('getStatus', () => {
188188
it('should return the status of the client associated to the provided key', () => {
189189
initSplitSdk({ config: sdkBrowserConfig });
190190
getTreatments({ key: 'user_2', splitNames: ['split_1'] });
191-
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY_FROM_CACHE);
192-
(splitSdk.factory as any).client('user_2').__emitter__.emit(Event.SDK_READY);
191+
(splitSdk.factory as any).client().__emitter__.emit(Event.SDK_READY);
192+
(splitSdk.factory as any).client('user_2').__emitter__.emit(Event.SDK_READY_FROM_CACHE);
193193

194194
// Main client
195-
const MAIN_CLIENT_STATUS = { ...STATUS_INITIAL, isReadyFromCache: true, isOperational: true, lastUpdate: (splitSdk.factory.client() as any).__getStatus().lastUpdate };
195+
const MAIN_CLIENT_STATUS = { ...STATUS_INITIAL, isReadyFromCache: true, isReady: true, isOperational: true, lastUpdate: (splitSdk.factory.client() as any).__getStatus().lastUpdate };
196196
expect(getStatus()).toEqual(MAIN_CLIENT_STATUS);
197197
expect(getStatus(sdkBrowserConfig.core.key)).toEqual(MAIN_CLIENT_STATUS);
198198
expect(getStatus({ matchingKey: sdkBrowserConfig.core.key as string, bucketingKey: '' })).toEqual(MAIN_CLIENT_STATUS);
199199

200200
// Client for user_2
201-
const USER_2_STATUS = { ...STATUS_INITIAL, isReady: true, isReadyFromCache: true, isOperational: true, lastUpdate: (splitSdk.factory.client('user_2') as any).__getStatus().lastUpdate };
201+
const USER_2_STATUS = { ...STATUS_INITIAL, isReadyFromCache: true, isOperational: true, lastUpdate: (splitSdk.factory.client('user_2') as any).__getStatus().lastUpdate };
202202
expect(getStatus('user_2')).toEqual(USER_2_STATUS);
203203
expect(getStatus({ matchingKey: 'user_2', bucketingKey: '' })).toEqual(USER_2_STATUS);
204204

src/asyncActions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ export function getTreatments(params: IGetTreatmentsParams): Action | (() => voi
148148
client.evalOnReady.push(params);
149149
}
150150

151-
// @TODO remove `evalOnReadyFromCache` config option, since `false` value has no effect on shared clients (they are ready from cache immediately) and on the main client if its ready from cache when `getTreatments` is called
151+
// @TODO breaking: consider removing `evalOnReadyFromCache` config option, since `false` value has no effect on shared clients (they are ready from cache immediately) and on the main client if its ready from cache when `getTreatments` is called
152152
// If the SDK is not ready from cache and flag `evalOnReadyFromCache`, it stores the action to execute when ready from cache
153153
if (!status.isReadyFromCache && params.evalOnReadyFromCache) {
154154
client.evalOnReadyFromCache.push(params);

0 commit comments

Comments
 (0)