Skip to content

Commit 13cf993

Browse files
committed
Refine compatibility story.
1 parent e990e55 commit 13cf993

5 files changed

Lines changed: 209 additions & 215 deletions

File tree

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

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ import * as mockResponseJson from './evaluation/mockResponse.json';
99
import { MockEventSource } from './streaming/LDClientImpl.mocks';
1010
import { makeTestDataManagerFactory } from './TestDataManager';
1111

12-
/**
13-
* Extracts the flags from a stored cache record JSON string.
14-
* Handles both the new format `{flags: {...}, freshness: {...}}` and
15-
* the old bare `{flagKey: {...}}` format.
16-
*/
17-
function parseFlagsFromStorage(json: string): Flags {
18-
const parsed = JSON.parse(json);
19-
return parsed.flags ?? parsed;
20-
}
21-
2212
let mockPlatform: ReturnType<typeof createBasicPlatform>;
2313
let logger: LDLogger;
2414

@@ -220,9 +210,11 @@ describe('sdk-client storage', () => {
220210
expect.stringContaining('index'),
221211
);
222212

223-
const storedRecord = parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1]);
224-
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
225-
expect(storedRecord).toEqual(defaultPutResponse);
213+
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
214+
3,
215+
flagStorageKey,
216+
JSON.stringify(defaultPutResponse),
217+
);
226218

227219
// this is defaultPutResponse
228220
expect(ldc.allFlags()).toEqual({
@@ -258,14 +250,17 @@ describe('sdk-client storage', () => {
258250
await jest.runAllTimersAsync();
259251

260252
expect(ldc.allFlags()).not.toHaveProperty('dev-test-flag');
261-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(2);
253+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(3);
262254
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
263255
1,
264256
indexStorageKey,
265257
expect.stringContaining('index'),
266258
);
267-
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
268-
expect(parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1])).toEqual(putResponse);
259+
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
260+
3,
261+
flagStorageKey,
262+
JSON.stringify(putResponse),
263+
);
269264

270265
expect(emitter.emit).toHaveBeenCalledWith('change', context, defaultFlagKeys);
271266
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
@@ -296,14 +291,17 @@ describe('sdk-client storage', () => {
296291
await jest.runAllTimersAsync();
297292

298293
expect(ldc.allFlags()).toMatchObject({ 'another-dev-test-flag': false });
299-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(2);
294+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(3);
300295
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
301296
1,
302297
indexStorageKey,
303298
expect.stringContaining('index'),
304299
);
305-
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
306-
expect(parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1])).toEqual(putResponse);
300+
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
301+
3,
302+
flagStorageKey,
303+
JSON.stringify(putResponse),
304+
);
307305
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['another-dev-test-flag']);
308306
});
309307

@@ -375,15 +373,16 @@ describe('sdk-client storage', () => {
375373
await ldc.identify(context);
376374
await jest.runAllTimersAsync();
377375

378-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(2);
376+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(3);
379377
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
380378
1,
381379
indexStorageKey,
382380
expect.stringContaining('index'),
383381
);
384-
expect(mockPlatform.storage.set.mock.calls[1][0]).toBe(flagStorageKey);
385-
expect(parseFlagsFromStorage(mockPlatform.storage.set.mock.calls[1][1])).toEqual(
386-
defaultPutResponse,
382+
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
383+
3,
384+
flagStorageKey,
385+
JSON.stringify(defaultPutResponse),
387386
);
388387

389388
// we expect one change from the local storage init, but no further change from the PUT
@@ -422,7 +421,7 @@ describe('sdk-client storage', () => {
422421
await changePromise;
423422
await jest.runAllTimersAsync();
424423

425-
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
424+
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
426425
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': true });
427426
expect(flagsInStorage['dev-test-flag'].reason).toEqual({
428427
kind: 'RULE_MATCH',
@@ -456,9 +455,9 @@ describe('sdk-client storage', () => {
456455
await changePromise;
457456
await jest.runAllTimersAsync();
458457

459-
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
458+
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
460459
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': false });
461-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(4);
460+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(6);
462461
expect(flagsInStorage['dev-test-flag'].version).toEqual(patchResponse.version);
463462
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
464463
});
@@ -483,10 +482,10 @@ describe('sdk-client storage', () => {
483482
await changePromise;
484483
await jest.runAllTimersAsync();
485484

486-
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
485+
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
487486
expect(ldc.allFlags()).toHaveProperty('another-dev-test-flag');
488487
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
489-
4,
488+
6,
490489
flagStorageKey,
491490
expect.stringContaining(JSON.stringify(patchResponse)),
492491
);
@@ -517,7 +516,7 @@ describe('sdk-client storage', () => {
517516
await jest.runAllTimersAsync();
518517

519518
// the initial put is resulting in two sets, one for the index and one for the flag data
520-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(2);
519+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(3);
521520
expect(emitter.emit).not.toHaveBeenCalledWith('change');
522521

523522
// this is defaultPutResponse
@@ -557,10 +556,10 @@ describe('sdk-client storage', () => {
557556
await changePromise;
558557
await jest.runAllTimersAsync();
559558

560-
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
559+
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
561560
expect(ldc.allFlags()).not.toHaveProperty('dev-test-flag');
562561
expect(mockPlatform.storage.set).toHaveBeenNthCalledWith(
563-
4,
562+
6,
564563
flagStorageKey,
565564
expect.stringContaining('dev-test-flag'),
566565
);
@@ -592,7 +591,7 @@ describe('sdk-client storage', () => {
592591

593592
expect(ldc.allFlags()).toHaveProperty('dev-test-flag');
594593
// the initial put is resulting in two sets, one for the index and one for the flag data
595-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(2);
594+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(3);
596595
expect(emitter.emit).not.toHaveBeenCalledWith('change');
597596
});
598597

@@ -620,7 +619,7 @@ describe('sdk-client storage', () => {
620619

621620
expect(ldc.allFlags()).toHaveProperty('dev-test-flag');
622621
// the initial put is resulting in two sets, one for the index and one for the flag data
623-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(2);
622+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(3);
624623
expect(emitter.emit).not.toHaveBeenCalledWith('change');
625624
});
626625

@@ -646,9 +645,9 @@ describe('sdk-client storage', () => {
646645
await changePromise;
647646
await jest.runAllTimersAsync();
648647

649-
const flagsInStorage = parseFlagsFromStorage(mockPlatform.storage.set.mock.lastCall[1]);
648+
const flagsInStorage = JSON.parse(mockPlatform.storage.set.mock.lastCall[1]) as Flags;
650649

651-
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(4); // two index saves and two flag saves
650+
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(6); // two index saves and two flag saves
652651
expect(flagsInStorage['does-not-exist']).toMatchObject({ ...deleteResponse, deleted: true });
653652
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['does-not-exist']);
654653
});

0 commit comments

Comments
 (0)