Skip to content

Commit

Permalink
Unit test for factory destroy
Browse files Browse the repository at this point in the history
  • Loading branch information
EmilianoSanchez committed Sep 20, 2024
1 parent aed68a7 commit d2fbd1b
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/sdkFactory/__tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { EventEmitter } from '../../utils/MinEvents';

/** Mocks */

const clientInstance = 'client';
const clientInstance = { destroy: jest.fn() };
const managerInstance = 'manager';
const mockStorage = {
splits: jest.fn(),
Expand All @@ -30,7 +30,7 @@ jest.mock('../../trackers/telemetryTracker', () => {
const paramsForAsyncSDK = {
settings: fullSettings,
storageFactory: jest.fn(() => mockStorage),
sdkClientMethodFactory: jest.fn(() => clientInstance),
sdkClientMethodFactory: jest.fn(({ clients }) => (key?: string) => { clients[key || ''] = clientInstance; return clientInstance; }),
sdkManagerFactory: jest.fn(() => managerInstance),
impressionsObserverFactory: jest.fn(),
platform: {
Expand Down Expand Up @@ -64,6 +64,7 @@ function assertSdkApi(sdk: SplitIO.IAsyncSDK | SplitIO.ISDK | SplitIO.ICsSDK, pa
expect(sdk.settings).toBe(params.settings);
expect(sdk.client).toBe(params.sdkClientMethodFactory.mock.results[0].value);
expect(sdk.manager()).toBe(params.sdkManagerFactory.mock.results[0].value);
expect(sdk.destroy()).toBeDefined();
}

function assertModulesCalled(params: any) {
Expand Down Expand Up @@ -92,22 +93,18 @@ describe('sdkFactory', () => {

afterEach(jest.clearAllMocks);

test('creates IAsyncSDK instance', () => {
test.each([paramsForAsyncSDK, fullParamsForSyncSDK])('creates SDK instance', async (params) => {

const sdk = sdkFactory(paramsForAsyncSDK as unknown as ISdkFactoryParams);
const sdk = sdkFactory(params as unknown as ISdkFactoryParams);

// should return an object that conforms to SDK interface
assertSdkApi(sdk, paramsForAsyncSDK);
assertSdkApi(sdk, params);

assertModulesCalled(paramsForAsyncSDK);
});

test('creates ISDK instance', () => {
const sdk = sdkFactory(fullParamsForSyncSDK as unknown as ISdkFactoryParams);

// should return an object that conforms to SDK interface
assertSdkApi(sdk, fullParamsForSyncSDK);
assertModulesCalled(params);

assertModulesCalled(fullParamsForSyncSDK);
// Factory destroy should call client destroy
expect(sdk.client()).toBe(clientInstance);
expect(await sdk.destroy()).toBeUndefined();
expect(sdk.client().destroy).toBeCalledTimes(1);
});
});

0 comments on commit d2fbd1b

Please sign in to comment.