-
Notifications
You must be signed in to change notification settings - Fork 31
chore: localstorage pr feedback #327
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
yusinto
merged 9 commits into
yus/sc-225809/use-localstorage-for-bootstrapping-rn-sdk
from
yus/sdk-client-pr-feedback
Dec 20, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9a5e232
chore: removed ready event. change event now returns flag keys only. …
yusinto 5670f63
chore: fixed unit tests.
yusinto 7458d60
Update streamingProcessor.ts
yusinto 1a4bd3b
chore: add unit test for cold start from streamer.
yusinto 1156637
Update LDClientImpl.ts
yusinto 61c816d
chore: add changed keys to change listener signature.
yusinto 559d626
chore: make sure PUT always updates the context, internal flags and s…
yusinto bea2f59
chore: add more logging.
yusinto 4fed630
Update LDClientImpl.ts
yusinto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,10 @@ | ||
| import { clone, type LDContext, LDFlagChangeset } from '@launchdarkly/js-sdk-common'; | ||
| import { clone, type LDContext } from '@launchdarkly/js-sdk-common'; | ||
| import { basicPlatform, logger, setupMockStreamingProcessor } from '@launchdarkly/private-js-mocks'; | ||
|
|
||
| import LDEmitter from './api/LDEmitter'; | ||
| import type { DeleteFlag, Flag, Flags, PatchFlag } from './evaluation/fetchFlags'; | ||
| import * as mockResponseJson from './evaluation/mockResponse.json'; | ||
| import LDClientImpl from './LDClientImpl'; | ||
| import { DeleteFlag, Flag, Flags, PatchFlag } from './types'; | ||
|
|
||
| jest.mock('@launchdarkly/js-sdk-common', () => { | ||
| const actual = jest.requireActual('@launchdarkly/js-sdk-common'); | ||
|
|
@@ -22,17 +22,18 @@ jest.mock('@launchdarkly/js-sdk-common', () => { | |
| }; | ||
| }); | ||
|
|
||
| const defaultPutResponse = mockResponseJson as Flags; | ||
| const testSdkKey = 'test-sdk-key'; | ||
| const context: LDContext = { kind: 'org', key: 'Testy Pizza' }; | ||
| let ldc: LDClientImpl; | ||
| let emitter: LDEmitter; | ||
| let defaultPutResponse: Flags; | ||
| let defaultFlagKeys: string[]; | ||
|
|
||
| // Promisify on.change listener so we can await it in tests. | ||
| const onChangePromise = () => | ||
| new Promise<LDFlagChangeset>((res) => { | ||
| ldc.on('change', (_context: LDContext, changeset: LDFlagChangeset) => { | ||
| res(changeset); | ||
| new Promise<string[]>((res) => { | ||
| ldc.on('change', (_context: LDContext, changes: string[]) => { | ||
| res(changes); | ||
| }); | ||
| }); | ||
|
|
||
|
|
@@ -69,6 +70,9 @@ const identifyGetAllFlags = async ( | |
| describe('sdk-client storage', () => { | ||
| beforeEach(() => { | ||
| jest.useFakeTimers(); | ||
| defaultPutResponse = clone<Flags>(mockResponseJson); | ||
| defaultFlagKeys = Object.keys(defaultPutResponse); | ||
|
|
||
| basicPlatform.storage.get.mockImplementation(() => JSON.stringify(defaultPutResponse)); | ||
| jest | ||
| .spyOn(LDClientImpl.prototype as any, 'createStreamUriPath') | ||
|
|
@@ -93,8 +97,8 @@ describe('sdk-client storage', () => { | |
|
|
||
| // 'change' should not have been emitted | ||
| expect(emitter.emit).toHaveBeenCalledTimes(3); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(1, 'initializing', context); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(2, 'ready', context); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(1, 'identifying', context); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, defaultFlagKeys); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith( | ||
| 3, | ||
| 'error', | ||
|
|
@@ -113,6 +117,43 @@ describe('sdk-client storage', () => { | |
| }); | ||
| }); | ||
|
|
||
| test('no storage, cold start from streamer', async () => { | ||
| // fake previously cached flags even though there's no storage for this context | ||
| // @ts-ignore | ||
| ldc.flags = defaultPutResponse; | ||
| basicPlatform.storage.get.mockImplementation(() => undefined); | ||
| setupMockStreamingProcessor(false, defaultPutResponse); | ||
|
|
||
| const p = ldc.identify(context); | ||
|
|
||
| // I'm not sure why but both runAllTimersAsync and runAllTicks are required | ||
| // here for the identify promise be resolved | ||
| await jest.runAllTimersAsync(); | ||
| jest.runAllTicks(); | ||
|
Comment on lines
+129
to
+132
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be a bug in jest fake timers. |
||
| await p; | ||
|
|
||
| expect(emitter.emit).toHaveBeenCalledTimes(1); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(1, 'identifying', context); | ||
| expect(basicPlatform.storage.set).toHaveBeenNthCalledWith( | ||
| 1, | ||
| 'org:Testy Pizza', | ||
| JSON.stringify(defaultPutResponse), | ||
| ); | ||
| expect(ldc.logger.debug).toHaveBeenCalledWith('Not emitting changes from PUT'); | ||
|
|
||
| // this is defaultPutResponse | ||
| expect(ldc.allFlags()).toEqual({ | ||
| 'dev-test-flag': true, | ||
| 'easter-i-tunes-special': false, | ||
| 'easter-specials': 'no specials', | ||
| fdsafdsafdsafdsa: true, | ||
| 'log-level': 'warn', | ||
| 'moonshot-demo': true, | ||
| test1: 's1', | ||
| 'this-is-a-test': true, | ||
| }); | ||
| }); | ||
|
|
||
| test('syncing storage when a flag is deleted', async () => { | ||
| const putResponse = clone<Flags>(defaultPutResponse); | ||
| delete putResponse['dev-test-flag']; | ||
|
|
@@ -123,11 +164,9 @@ describe('sdk-client storage', () => { | |
| 'org:Testy Pizza', | ||
| JSON.stringify(putResponse), | ||
| ); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(1, 'initializing', context); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(2, 'ready', context); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'dev-test-flag': { previous: true }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(1, 'identifying', context); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, defaultFlagKeys); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['dev-test-flag']); | ||
| }); | ||
|
|
||
| test('syncing storage when a flag is added', async () => { | ||
|
|
@@ -147,9 +186,7 @@ describe('sdk-client storage', () => { | |
| 'org:Testy Pizza', | ||
| JSON.stringify(putResponse), | ||
| ); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'another-dev-test-flag': { current: newFlag.value }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['another-dev-test-flag']); | ||
| }); | ||
|
|
||
| test('syncing storage when a flag is updated', async () => { | ||
|
|
@@ -159,12 +196,7 @@ describe('sdk-client storage', () => { | |
| const allFlags = await identifyGetAllFlags(false, putResponse); | ||
|
|
||
| expect(allFlags).toMatchObject({ 'dev-test-flag': false }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'dev-test-flag': { | ||
| previous: true, | ||
| current: putResponse['dev-test-flag'].value, | ||
| }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['dev-test-flag']); | ||
| }); | ||
|
|
||
| test('syncing storage on multiple flag operations', async () => { | ||
|
|
@@ -179,17 +211,14 @@ describe('sdk-client storage', () => { | |
|
|
||
| expect(allFlags).toMatchObject({ 'dev-test-flag': false, 'another-dev-test-flag': true }); | ||
| expect(allFlags).not.toHaveProperty('moonshot-demo'); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'dev-test-flag': { | ||
| previous: true, | ||
| current: putResponse['dev-test-flag'].value, | ||
| }, | ||
| 'another-dev-test-flag': { current: newFlag.value }, | ||
| 'moonshot-demo': { previous: true }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, [ | ||
| 'moonshot-demo', | ||
| 'dev-test-flag', | ||
| 'another-dev-test-flag', | ||
| ]); | ||
| }); | ||
|
|
||
| test('syncing storage when PUT is consistent so no updates needed', async () => { | ||
| test('syncing storage when PUT is consistent so no change', async () => { | ||
| const allFlags = await identifyGetAllFlags( | ||
| false, | ||
| defaultPutResponse, | ||
|
|
@@ -198,8 +227,13 @@ describe('sdk-client storage', () => { | |
| false, | ||
| ); | ||
|
|
||
| expect(basicPlatform.storage.set).not.toHaveBeenCalled(); | ||
| expect(emitter.emit).not.toHaveBeenCalledWith('change'); | ||
| expect(basicPlatform.storage.set).toHaveBeenNthCalledWith( | ||
| 1, | ||
| 'org:Testy Pizza', | ||
| JSON.stringify(defaultPutResponse), | ||
| ); | ||
| expect(emitter.emit).toHaveBeenCalledTimes(2); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(2, 'change', context, defaultFlagKeys); | ||
|
|
||
| // this is defaultPutResponse | ||
| expect(allFlags).toEqual({ | ||
|
|
@@ -229,12 +263,7 @@ describe('sdk-client storage', () => { | |
|
|
||
| // both previous and current are true but inExperiment has changed | ||
| // so a change event should be emitted | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'dev-test-flag': { | ||
| previous: true, | ||
| current: true, | ||
| }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['dev-test-flag']); | ||
| }); | ||
|
|
||
| test('patch should emit change event', async () => { | ||
|
|
@@ -244,46 +273,33 @@ describe('sdk-client storage', () => { | |
| patchResponse.version += 1; | ||
|
|
||
| const allFlags = await identifyGetAllFlags(false, defaultPutResponse, patchResponse); | ||
| const flagsInStorage = JSON.parse(basicPlatform.storage.set.mock.calls[0][1]) as Flags; | ||
| const flagsInStorage = JSON.parse(basicPlatform.storage.set.mock.lastCall[1]) as Flags; | ||
|
|
||
| expect(allFlags).toMatchObject({ 'dev-test-flag': false }); | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledTimes(1); | ||
| expect(basicPlatform.storage.set).toHaveBeenNthCalledWith( | ||
| 1, | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledWith( | ||
| 'org:Testy Pizza', | ||
| expect.stringContaining(JSON.stringify(patchResponse)), | ||
| ); | ||
| expect(flagsInStorage['dev-test-flag'].version).toEqual(patchResponse.version); | ||
| expect(emitter.emit).toHaveBeenCalledTimes(3); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'dev-test-flag': { | ||
| previous: true, | ||
| current: false, | ||
| }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['dev-test-flag']); | ||
| }); | ||
|
|
||
| test('patch should add new flags', async () => { | ||
| const patchResponse = clone<PatchFlag>(defaultPutResponse['dev-test-flag']); | ||
| patchResponse.key = 'another-dev-test-flag'; | ||
|
|
||
| const allFlags = await identifyGetAllFlags(false, defaultPutResponse, patchResponse); | ||
| const flagsInStorage = JSON.parse(basicPlatform.storage.set.mock.calls[0][1]) as Flags; | ||
| const flagsInStorage = JSON.parse(basicPlatform.storage.set.mock.lastCall[1]) as Flags; | ||
|
|
||
| expect(allFlags).toHaveProperty('another-dev-test-flag'); | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledTimes(1); | ||
| expect(basicPlatform.storage.set).toHaveBeenNthCalledWith( | ||
| 1, | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledWith( | ||
| 'org:Testy Pizza', | ||
| expect.stringContaining(JSON.stringify(patchResponse)), | ||
| ); | ||
| expect(flagsInStorage).toHaveProperty('another-dev-test-flag'); | ||
| expect(emitter.emit).toHaveBeenCalledTimes(3); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'another-dev-test-flag': { | ||
| current: true, | ||
| }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['another-dev-test-flag']); | ||
| }); | ||
|
|
||
| test('patch should ignore older version', async () => { | ||
|
|
@@ -300,7 +316,7 @@ describe('sdk-client storage', () => { | |
| false, | ||
| ); | ||
|
|
||
| expect(basicPlatform.storage.set).not.toHaveBeenCalled(); | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledTimes(1); | ||
| expect(emitter.emit).not.toHaveBeenCalledWith('change'); | ||
|
|
||
| // this is defaultPutResponse | ||
|
|
@@ -328,22 +344,16 @@ describe('sdk-client storage', () => { | |
| undefined, | ||
| deleteResponse, | ||
| ); | ||
| const flagsInStorage = JSON.parse(basicPlatform.storage.set.mock.calls[0][1]) as Flags; | ||
| const flagsInStorage = JSON.parse(basicPlatform.storage.set.mock.lastCall[1]) as Flags; | ||
|
|
||
| expect(allFlags).not.toHaveProperty('dev-test-flag'); | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledTimes(1); | ||
| expect(basicPlatform.storage.set).toHaveBeenNthCalledWith( | ||
| 1, | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledWith( | ||
| 'org:Testy Pizza', | ||
| expect.not.stringContaining('dev-test-flag'), | ||
| ); | ||
| expect(flagsInStorage['dev-test-flag']).toBeUndefined(); | ||
| expect(emitter.emit).toHaveBeenCalledTimes(3); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, { | ||
| 'dev-test-flag': { | ||
| previous: true, | ||
| }, | ||
| }); | ||
| expect(emitter.emit).toHaveBeenNthCalledWith(3, 'change', context, ['dev-test-flag']); | ||
| }); | ||
|
|
||
| test('delete should not delete newer version', async () => { | ||
|
|
@@ -361,7 +371,7 @@ describe('sdk-client storage', () => { | |
| ); | ||
|
|
||
| expect(allFlags).toHaveProperty('dev-test-flag'); | ||
| expect(basicPlatform.storage.set).not.toHaveBeenCalled(); | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledTimes(1); | ||
| expect(emitter.emit).not.toHaveBeenCalledWith('change'); | ||
| }); | ||
|
|
||
|
|
@@ -373,7 +383,7 @@ describe('sdk-client storage', () => { | |
|
|
||
| await identifyGetAllFlags(false, defaultPutResponse, undefined, deleteResponse, false); | ||
|
|
||
| expect(basicPlatform.storage.set).not.toHaveBeenCalled(); | ||
| expect(basicPlatform.storage.set).toHaveBeenCalledTimes(1); | ||
| expect(emitter.emit).not.toHaveBeenCalledWith('change'); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
readyhas been deleted in favor of a simpler design relying only onchange.