Skip to content

Commit 56baae7

Browse files
committed
test: adding some tests for context types
1 parent cedcd99 commit 56baae7

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

packages/shared/sdk-client/__tests__/LDClientImpl.test.ts

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { AutoEnvAttributes, clone, Hasher, LDContext, LDLogger } from '@launchdarkly/js-sdk-common';
1+
import { AutoEnvAttributes, clone, Hasher, LDLogger } from '@launchdarkly/js-sdk-common';
22

3+
import { LDContext } from '../src/api/LDContext';
34
import { DataSourceState } from '../src/datasource/DataSourceStatus';
45
import LDClientImpl from '../src/LDClientImpl';
56
import { Flags } from '../src/types';
@@ -235,7 +236,7 @@ describe('sdk-client object', () => {
235236
defaultPutResponse['dev-test-flag'].value = false;
236237
simulatedEvents = [{ data: JSON.stringify(defaultPutResponse) }];
237238

238-
const carContext: LDContext = { kind: 'car', anonymous: true, key: '' };
239+
const carContext: LDContext = { kind: 'car', anonymous: true };
239240

240241
mockPlatform.crypto.randomUUID.mockReturnValue('random1');
241242

@@ -253,9 +254,46 @@ describe('sdk-client object', () => {
253254
});
254255
});
255256

257+
test('identify multi kind context with anonymous', async () => {
258+
defaultPutResponse['dev-test-flag'].value = false;
259+
simulatedEvents = [{ data: JSON.stringify(defaultPutResponse) }];
260+
261+
const carContext: LDContext = {
262+
kind: 'multi',
263+
user: { anonymous: true },
264+
org: { anonymous: true },
265+
};
266+
267+
mockPlatform.crypto.randomUUID.mockReturnValue('random1');
268+
269+
await ldc.identify(carContext);
270+
const c = ldc.getContext();
271+
const all = ldc.allFlags();
272+
273+
expect(c).toEqual({
274+
kind: 'multi',
275+
user: { anonymous: true, key: 'random1' },
276+
org: { anonymous: true, key: 'random1' },
277+
...autoEnv,
278+
});
279+
expect(all).toMatchObject({
280+
'dev-test-flag': false,
281+
});
282+
});
283+
256284
test('identify error invalid context', async () => {
257-
const carContext: LDContext = { kind: 'car', key: '' };
285+
const carContext = { kind: 'car' };
286+
287+
// @ts-expect-error - invalid context
288+
await expect(ldc.identify(carContext)).rejects.toThrow(/no key/);
289+
expect(logger.error).toHaveBeenCalledTimes(1);
290+
expect(ldc.getContext()).toBeUndefined();
291+
});
292+
293+
test('identify error invalid multi kindcontext', async () => {
294+
const carContext = { kind: 'multi', user: { name: 'test' } };
258295

296+
// @ts-ignore - invalid context
259297
await expect(ldc.identify(carContext)).rejects.toThrow(/no key/);
260298
expect(logger.error).toHaveBeenCalledTimes(1);
261299
expect(ldc.getContext()).toBeUndefined();

0 commit comments

Comments
 (0)