-
Notifications
You must be signed in to change notification settings - Fork 31
refactor: added warning if async storage is not found #1044
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
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
9d5afd2
refactor: added warning if async storage is not found
joker23 3bc8c25
Merge branch 'main' into skz/sdk-1676/rn-storage-missing
joker23 db43ac2
docs: adding more details to docs for RN storage
joker23 b42da8a
Merge branch 'main' into skz/sdk-1676/rn-storage-missing
joker23 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
101 changes: 77 additions & 24 deletions
101
packages/sdk/react-native/__tests__/ReactNativeLDClient.storage.test.ts
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,31 +1,84 @@ | ||
| import { AutoEnvAttributes, LDLogger } from '@launchdarkly/js-client-sdk-common'; | ||
|
|
||
| import PlatformStorage from '../src/platform/PlatformStorage'; | ||
| import ReactNativeLDClient from '../src/ReactNativeLDClient'; | ||
|
|
||
| it('uses custom storage', async () => { | ||
| // This test just validates that the custom storage instance is being called. | ||
| // Other tests validate how the SDK interacts with storage generally. | ||
| const logger: LDLogger = { | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| info: jest.fn(), | ||
| debug: jest.fn(), | ||
| }; | ||
| const myStorage = { | ||
| get: jest.fn(), | ||
| set: jest.fn(), | ||
| clear: jest.fn(), | ||
| }; | ||
| const client = new ReactNativeLDClient('mobile-key', AutoEnvAttributes.Enabled, { | ||
| sendEvents: false, | ||
| initialConnectionMode: 'offline', | ||
| logger, | ||
| storage: myStorage, | ||
| jest.mock('../src/platform/PlatformStorage', () => ({ | ||
| __esModule: true, | ||
| default: jest.fn().mockImplementation((logger: LDLogger) => { | ||
| const ActualPlatformStorage = jest.requireActual('../src/platform/PlatformStorage').default; | ||
| return new ActualPlatformStorage(logger); | ||
| }), | ||
| })); | ||
|
|
||
| describe('ReactNativeLDClient storage', () => { | ||
| beforeEach(() => { | ||
| (PlatformStorage as jest.MockedClass<typeof PlatformStorage>).mockClear(); | ||
| }); | ||
|
|
||
| it('uses custom storage', async () => { | ||
| // This test just validates that the custom storage instance is being called. | ||
| // Other tests validate how the SDK interacts with storage generally. | ||
| const logger: LDLogger = { | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| info: jest.fn(), | ||
| debug: jest.fn(), | ||
| }; | ||
| const myStorage = { | ||
| get: jest.fn(), | ||
| set: jest.fn(), | ||
| clear: jest.fn(), | ||
| }; | ||
| const client = new ReactNativeLDClient('mobile-key', AutoEnvAttributes.Enabled, { | ||
| sendEvents: false, | ||
| initialConnectionMode: 'offline', | ||
| logger, | ||
| storage: myStorage, | ||
| }); | ||
|
|
||
| await client.identify({ key: 'potato', kind: 'user' }, { timeout: 15 }); | ||
| expect(myStorage.get).toHaveBeenCalled(); | ||
| expect(myStorage.clear).not.toHaveBeenCalled(); | ||
| // Ensure the base client is not emitting a warning for this. | ||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| // Ensure the default platform storage is not instantiated when custom storage is provided. | ||
| expect(PlatformStorage).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| await client.identify({ key: 'potato', kind: 'user' }, { timeout: 15 }); | ||
| expect(myStorage.get).toHaveBeenCalled(); | ||
| expect(myStorage.clear).not.toHaveBeenCalled(); | ||
| // Ensure the base client is not emitting a warning for this. | ||
| expect(logger.warn).not.toHaveBeenCalled(); | ||
| it('uses default platform storage when no custom storage is provided', async () => { | ||
| const logger: LDLogger = { | ||
| error: jest.fn(), | ||
| warn: jest.fn(), | ||
| info: jest.fn(), | ||
| debug: jest.fn(), | ||
| }; | ||
| const client = new ReactNativeLDClient('mobile-key', AutoEnvAttributes.Enabled, { | ||
| sendEvents: false, | ||
| initialConnectionMode: 'offline', | ||
| logger, | ||
| }); | ||
|
|
||
| // Verify that PlatformStorage was instantiated | ||
| expect(PlatformStorage).toHaveBeenCalledWith(logger); | ||
| expect(PlatformStorage).toHaveBeenCalledTimes(1); | ||
|
|
||
| // Get the storage instance and spy on its methods | ||
| const mockResults = (PlatformStorage as jest.MockedClass<typeof PlatformStorage>).mock.results; | ||
| expect(mockResults.length).toBeGreaterThan(0); | ||
| const storageInstance = mockResults[0].value; | ||
| expect(storageInstance).toBeDefined(); | ||
|
|
||
| const getSpy = jest.spyOn(storageInstance, 'get'); | ||
| const setSpy = jest.spyOn(storageInstance, 'set'); | ||
|
|
||
| await client.identify({ key: 'potato', kind: 'user' }, { timeout: 15 }); | ||
|
|
||
| // Verify that storage methods are being called | ||
| expect(getSpy).toHaveBeenCalled(); | ||
| expect(setSpy).toHaveBeenCalled(); | ||
|
|
||
| getSpy.mockRestore(); | ||
| setSpy.mockRestore(); | ||
| }); | ||
| }); |
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
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.
I think we may want to consider not using
mock. It doesn't convey what will actually happen.I think we want to do a few things:
I am also concerned implementation wise. It seems like even without storage we should be keeping things in memory to be stable for the application session. (This is a pre-existing problem we could have in a different PR.)
Also, will these be called even if RNStorage is specified? I feel like it probably would be, but wouldn't matter in that case.
Uh oh!
There was an error while loading. Please reload this page.
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.
Ah yea, it would be good to be able to fallback to inmemory context storage. I can create a work item for that.
If a custom storage is provided, then this line should not run
js-core/packages/sdk/react-native/src/platform/index.ts
Line 15 in 3bc8c25
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.
For the message wording, how about:
Additionally, I think we can put more details in the documentation with links to the default implementation for https://github.com/react-native-async-storage/async-storage
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.
Sounds good.
generate -> generated