Skip to content

Commit d95111f

Browse files
committed
making change detection tests a little less sensitive
1 parent c4879f5 commit d95111f

File tree

1 file changed

+20
-27
lines changed

1 file changed

+20
-27
lines changed

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

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -89,10 +89,8 @@ describe('sdk-client storage', () => {
8989

9090
expect(mockPlatform.storage.get).toHaveBeenCalledWith(flagStorageKey);
9191

92-
expect(emitter.emit).toHaveBeenCalledTimes(3);
93-
expect(emitter.emit).toHaveBeenNthCalledWith(1, 'change', context, defaultFlagKeys);
94-
expect(emitter.emit).toHaveBeenNthCalledWith(
95-
2,
92+
expect(emitter.emit).toHaveBeenCalledWith('change', context, defaultFlagKeys);
93+
expect(emitter.emit).toHaveBeenCalledWith(
9694
'error',
9795
context,
9896
expect.objectContaining({ message: 'test-error' }),
@@ -135,15 +133,12 @@ describe('sdk-client storage', () => {
135133
);
136134

137135
// 'change' should not have been emitted
138-
expect(emitter.emit).toHaveBeenCalledTimes(3);
139-
expect(emitter.emit).toHaveBeenNthCalledWith(
140-
1,
136+
expect(emitter.emit).toHaveBeenCalledWith(
141137
'change',
142138
expect.objectContaining(toMulti(context)),
143139
defaultFlagKeys,
144140
);
145-
expect(emitter.emit).toHaveBeenNthCalledWith(
146-
2,
141+
expect(emitter.emit).toHaveBeenCalledWith(
147142
'error',
148143
expect.objectContaining(toMulti(context)),
149144
expect.objectContaining({ message: 'test-error' }),
@@ -170,15 +165,17 @@ describe('sdk-client storage', () => {
170165

171166
// @ts-ignore
172167
emitter = ldc.emitter;
173-
jest.spyOn(emitter as LDEmitter, 'emit');
168+
const spy = jest.spyOn(emitter as LDEmitter, 'emit');
174169

175170
// expect emission
176171
await ldc.identify(context);
172+
expect(emitter.emit).toHaveBeenCalledWith('change', context, defaultFlagKeys);
177173

178-
// expit no emission
174+
// clear the spy so we can tell if change was invoked again
175+
spy.mockClear();
176+
// expect no emission
179177
await ldc.identify(context);
180-
181-
expect(emitter.emit).toHaveBeenCalledTimes(1);
178+
expect(emitter.emit).not.toHaveBeenCalledWith('change', context, defaultFlagKeys);
182179
});
183180

184181
test('no storage, cold start from streaming', async () => {
@@ -251,8 +248,8 @@ describe('sdk-client storage', () => {
251248
JSON.stringify(putResponse),
252249
);
253250

254-
expect(emitter.emit).toHaveBeenNthCalledWith(1, 'change', context, defaultFlagKeys);
255-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['dev-test-flag']);
251+
expect(emitter.emit).toHaveBeenCalledWith('change', context, defaultFlagKeys);
252+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
256253
});
257254

258255
test('syncing storage when a flag is added', async () => {
@@ -291,7 +288,7 @@ describe('sdk-client storage', () => {
291288
flagStorageKey,
292289
JSON.stringify(putResponse),
293290
);
294-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['another-dev-test-flag']);
291+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['another-dev-test-flag']);
295292
});
296293

297294
test('syncing storage when a flag is updated', async () => {
@@ -314,7 +311,7 @@ describe('sdk-client storage', () => {
314311
await jest.runAllTimersAsync();
315312

316313
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': false });
317-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['dev-test-flag']);
314+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
318315
});
319316

320317
test('syncing storage on multiple flag operations', async () => {
@@ -342,7 +339,7 @@ describe('sdk-client storage', () => {
342339

343340
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': false, 'another-dev-test-flag': true });
344341
expect(ldc.allFlags()).not.toHaveProperty('moonshot-demo');
345-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, [
342+
expect(emitter.emit).toHaveBeenCalledWith('change', context, [
346343
'moonshot-demo',
347344
'dev-test-flag',
348345
'another-dev-test-flag',
@@ -375,8 +372,7 @@ describe('sdk-client storage', () => {
375372
);
376373

377374
// we expect one change from the local storage init, but no further change from the PUT
378-
expect(emitter.emit).toHaveBeenCalledTimes(2);
379-
expect(emitter.emit).toHaveBeenNthCalledWith(1, 'change', context, defaultFlagKeys);
375+
expect(emitter.emit).toHaveBeenCalledWith('change', context, defaultFlagKeys);
380376

381377
// this is defaultPutResponse
382378
expect(ldc.allFlags()).toEqual({
@@ -418,7 +414,7 @@ describe('sdk-client storage', () => {
418414

419415
// both previous and current are true but inExperiment has changed
420416
// so a change event should be emitted
421-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['dev-test-flag']);
417+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
422418
});
423419

424420
test('patch should emit change event', async () => {
@@ -447,8 +443,7 @@ describe('sdk-client storage', () => {
447443
expect(ldc.allFlags()).toMatchObject({ 'dev-test-flag': false });
448444
expect(mockPlatform.storage.set).toHaveBeenCalledTimes(4);
449445
expect(flagsInStorage['dev-test-flag'].version).toEqual(patchResponse.version);
450-
expect(emitter.emit).toHaveBeenCalledTimes(3);
451-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['dev-test-flag']);
446+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
452447
});
453448

454449
test('patch should add new flags', async () => {
@@ -479,8 +474,7 @@ describe('sdk-client storage', () => {
479474
expect.stringContaining(JSON.stringify(patchResponse)),
480475
);
481476
expect(flagsInStorage).toHaveProperty('another-dev-test-flag');
482-
expect(emitter.emit).toHaveBeenCalledTimes(3);
483-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['another-dev-test-flag']);
477+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['another-dev-test-flag']);
484478
});
485479

486480
test('patch should ignore older version', async () => {
@@ -552,8 +546,7 @@ describe('sdk-client storage', () => {
552546
expect.stringContaining('dev-test-flag'),
553547
);
554548
expect(flagsInStorage['dev-test-flag']).toMatchObject({ ...deleteResponse, deleted: true });
555-
expect(emitter.emit).toHaveBeenCalledTimes(3);
556-
expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, ['dev-test-flag']);
549+
expect(emitter.emit).toHaveBeenCalledWith('change', context, ['dev-test-flag']);
557550
});
558551

559552
test('delete should not delete equal version', async () => {

0 commit comments

Comments
 (0)