Skip to content

Commit 15db92c

Browse files
authored
fix: Bug in sdk-client where withReasons was not passed to streamer. (#387)
Fixes #383.
1 parent 5f23820 commit 15db92c

File tree

2 files changed

+42
-9
lines changed

2 files changed

+42
-9
lines changed

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

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {
33
basicPlatform,
44
hasher,
55
logger,
6+
MockStreamingProcessor,
67
setupMockStreamingProcessor,
78
} from '@launchdarkly/private-js-mocks';
89

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

1314
jest.mock('@launchdarkly/js-sdk-common', () => {
1415
const actual = jest.requireActual('@launchdarkly/js-sdk-common');
15-
const { MockStreamingProcessor } = jest.requireActual('@launchdarkly/private-js-mocks');
16+
const actualMock = jest.requireActual('@launchdarkly/private-js-mocks');
1617
return {
1718
...actual,
1819
...{
1920
internal: {
2021
...actual.internal,
21-
StreamingProcessor: MockStreamingProcessor,
22+
StreamingProcessor: actualMock.MockStreamingProcessor,
2223
},
2324
},
2425
};
@@ -147,25 +148,53 @@ describe('sdk-client object', () => {
147148

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

152153
await ldc.identify(carContext);
153154
const c = ldc.getContext();
154155
const all = ldc.allFlags();
155156

156157
expect(c).toEqual({
157158
kind: 'multi',
158-
car: { key: 'mazda-cx7' },
159+
car: { key: 'test-car' },
159160
...autoEnv,
160161
});
161162
expect(all).toMatchObject({
162163
'dev-test-flag': false,
163164
});
165+
expect(MockStreamingProcessor).toHaveBeenCalledWith(
166+
expect.anything(),
167+
expect.anything(),
168+
'/stream/path',
169+
expect.anything(),
170+
undefined,
171+
expect.anything(),
172+
);
173+
});
174+
175+
test('identify success withReasons', async () => {
176+
const carContext: LDContext = { kind: 'car', key: 'test-car' };
177+
ldc = new LDClientImpl(testSdkKey, AutoEnvAttributes.Enabled, basicPlatform, {
178+
logger,
179+
sendEvents: false,
180+
withReasons: true,
181+
});
182+
183+
await ldc.identify(carContext);
184+
185+
expect(MockStreamingProcessor).toHaveBeenCalledWith(
186+
expect.anything(),
187+
expect.anything(),
188+
'/stream/path?withReasons=true',
189+
expect.anything(),
190+
undefined,
191+
expect.anything(),
192+
);
164193
});
165194

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

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

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

226255
await ldc.identify(context);
227256

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

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

234263
expect(emitter.listenerCount('change')).toEqual(1);

packages/shared/sdk-client/src/LDClientImpl.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,10 +321,14 @@ export default class LDClientImpl implements LDClient {
321321
}
322322
} else {
323323
this.streamer?.close();
324+
let streamUri = this.createStreamUriPath(context);
325+
if (this.config.withReasons) {
326+
streamUri = `${streamUri}?withReasons=true`;
327+
}
324328
this.streamer = new internal.StreamingProcessor(
325329
this.sdkKey,
326330
this.clientContext,
327-
this.createStreamUriPath(context),
331+
streamUri,
328332
this.createStreamListeners(context, checkedContext.canonicalKey, identifyResolve),
329333
this.diagnosticsManager,
330334
(e) => {

0 commit comments

Comments
 (0)