Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions packages/shared/sdk-client/src/LDClientImpl.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
basicPlatform,
hasher,
logger,
MockStreamingProcessor,
setupMockStreamingProcessor,
} from '@launchdarkly/private-js-mocks';

Expand All @@ -12,13 +13,13 @@ import { Flags } from './types';

jest.mock('@launchdarkly/js-sdk-common', () => {
const actual = jest.requireActual('@launchdarkly/js-sdk-common');
const { MockStreamingProcessor } = jest.requireActual('@launchdarkly/private-js-mocks');
const actualMock = jest.requireActual('@launchdarkly/private-js-mocks');
return {
...actual,
...{
internal: {
...actual.internal,
StreamingProcessor: MockStreamingProcessor,
StreamingProcessor: actualMock.MockStreamingProcessor,
},
},
};
Expand Down Expand Up @@ -147,25 +148,53 @@ describe('sdk-client object', () => {

test('identify success', async () => {
defaultPutResponse['dev-test-flag'].value = false;
const carContext: LDContext = { kind: 'car', key: 'mazda-cx7' };
const carContext: LDContext = { kind: 'car', key: 'test-car' };

await ldc.identify(carContext);
const c = ldc.getContext();
const all = ldc.allFlags();

expect(c).toEqual({
kind: 'multi',
car: { key: 'mazda-cx7' },
car: { key: 'test-car' },
...autoEnv,
});
expect(all).toMatchObject({
'dev-test-flag': false,
});
expect(MockStreamingProcessor).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
'/stream/path',
expect.anything(),
undefined,
expect.anything(),
);
});

test('identify success withReasons', async () => {
const carContext: LDContext = { kind: 'car', key: 'test-car' };
ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, {
logger,
sendEvents: false,
withReasons: true,
});

await ldc.identify(carContext);

expect(MockStreamingProcessor).toHaveBeenCalledWith(
expect.anything(),
expect.anything(),
'/stream/path?withReasons=true',
expect.anything(),
undefined,
expect.anything(),
);
});

test('identify success without auto env', async () => {
defaultPutResponse['dev-test-flag'].value = false;
const carContext: LDContext = { kind: 'car', key: 'mazda-cx7' };
const carContext: LDContext = { kind: 'car', key: 'test-car' };
ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Disabled, basicPlatform, {
logger,
sendEvents: false,
Expand Down Expand Up @@ -209,7 +238,7 @@ describe('sdk-client object', () => {

test('identify error stream error', async () => {
setupMockStreamingProcessor(true);
const carContext: LDContext = { kind: 'car', key: 'mazda-3' };
const carContext: LDContext = { kind: 'car', key: 'test-car' };

await expect(ldc.identify(carContext)).rejects.toMatchObject({
code: 401,
Expand All @@ -225,10 +254,10 @@ describe('sdk-client object', () => {

await ldc.identify(context);

const carContext1: LDContext = { kind: 'car', key: 'mazda-cx' };
const carContext1: LDContext = { kind: 'car', key: 'test-car' };
await ldc.identify(carContext1);

const carContext2: LDContext = { kind: 'car', key: 'subaru-forrester' };
const carContext2: LDContext = { kind: 'car', key: 'test-car-2' };
await ldc.identify(carContext2);

expect(emitter.listenerCount('change')).toEqual(1);
Expand Down
6 changes: 5 additions & 1 deletion packages/shared/sdk-client/src/LDClientImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,14 @@ export default class LDClientImpl implements LDClient {
}
} else {
this.streamer?.close();
let streamUri = this.createStreamUriPath(context);
if (this.config.withReasons) {
streamUri = `${streamUri}?withReasons=true`;
}
this.streamer = new internal.StreamingProcessor(
this.sdkKey,
this.clientContext,
this.createStreamUriPath(context),
streamUri,
this.createStreamListeners(context, checkedContext.canonicalKey, identifyResolve),
this.diagnosticsManager,
(e) => {
Expand Down