Skip to content

Commit d2fbd1b

Browse files
Unit test for factory destroy
1 parent aed68a7 commit d2fbd1b

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

src/sdkFactory/__tests__/index.spec.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { EventEmitter } from '../../utils/MinEvents';
66

77
/** Mocks */
88

9-
const clientInstance = 'client';
9+
const clientInstance = { destroy: jest.fn() };
1010
const managerInstance = 'manager';
1111
const mockStorage = {
1212
splits: jest.fn(),
@@ -30,7 +30,7 @@ jest.mock('../../trackers/telemetryTracker', () => {
3030
const paramsForAsyncSDK = {
3131
settings: fullSettings,
3232
storageFactory: jest.fn(() => mockStorage),
33-
sdkClientMethodFactory: jest.fn(() => clientInstance),
33+
sdkClientMethodFactory: jest.fn(({ clients }) => (key?: string) => { clients[key || ''] = clientInstance; return clientInstance; }),
3434
sdkManagerFactory: jest.fn(() => managerInstance),
3535
impressionsObserverFactory: jest.fn(),
3636
platform: {
@@ -64,6 +64,7 @@ function assertSdkApi(sdk: SplitIO.IAsyncSDK | SplitIO.ISDK | SplitIO.ICsSDK, pa
6464
expect(sdk.settings).toBe(params.settings);
6565
expect(sdk.client).toBe(params.sdkClientMethodFactory.mock.results[0].value);
6666
expect(sdk.manager()).toBe(params.sdkManagerFactory.mock.results[0].value);
67+
expect(sdk.destroy()).toBeDefined();
6768
}
6869

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

9394
afterEach(jest.clearAllMocks);
9495

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

97-
const sdk = sdkFactory(paramsForAsyncSDK as unknown as ISdkFactoryParams);
98+
const sdk = sdkFactory(params as unknown as ISdkFactoryParams);
9899

99100
// should return an object that conforms to SDK interface
100-
assertSdkApi(sdk, paramsForAsyncSDK);
101+
assertSdkApi(sdk, params);
101102

102-
assertModulesCalled(paramsForAsyncSDK);
103-
});
104-
105-
test('creates ISDK instance', () => {
106-
const sdk = sdkFactory(fullParamsForSyncSDK as unknown as ISdkFactoryParams);
107-
108-
// should return an object that conforms to SDK interface
109-
assertSdkApi(sdk, fullParamsForSyncSDK);
103+
assertModulesCalled(params);
110104

111-
assertModulesCalled(fullParamsForSyncSDK);
105+
// Factory destroy should call client destroy
106+
expect(sdk.client()).toBe(clientInstance);
107+
expect(await sdk.destroy()).toBeUndefined();
108+
expect(sdk.client().destroy).toBeCalledTimes(1);
112109
});
113110
});

0 commit comments

Comments
 (0)