-
-
Notifications
You must be signed in to change notification settings - Fork 257
Migrate storage from localStorage to IndexedDB using localForage #2102
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 all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
86ee247
Add localforage dependency for improved storage management
RodriSanchez1 12b01e9
Migrate storage from localStorage to IndexedDB using localForage
RodriSanchez1 1aaab10
Remove log
RodriSanchez1 2bb5fc0
Add storage migration tests
RodriSanchez1 081a48e
Add appInsights tracking for the migration process
RodriSanchez1 498b09c
Add clean up insights
RodriSanchez1 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 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,7 +1,109 @@ | ||
| import createReducer from '../reducers'; | ||
| import createReducer, { createMigratingStorage } from '../reducers'; | ||
|
|
||
| describe('reducers', () => { | ||
| it('should create Reducer', () => { | ||
| const red = createReducer(); | ||
| expect(red).toBeDefined; | ||
| expect(red).toBeDefined(); | ||
| }); | ||
| }); | ||
|
|
||
| const createMockStorage = () => ({ | ||
| getItem: jest.fn(), | ||
| setItem: jest.fn(), | ||
| removeItem: jest.fn() | ||
| }); | ||
|
|
||
| describe('createMigratingStorage', () => { | ||
| let oldStorage, newStorage, storage; | ||
|
|
||
| beforeEach(() => { | ||
| oldStorage = createMockStorage(); | ||
| newStorage = createMockStorage(); | ||
| storage = createMigratingStorage(oldStorage, newStorage); | ||
| }); | ||
|
|
||
| describe('getItem', () => { | ||
| it('returns value from new storage when it exists, without touching old storage', async () => { | ||
| newStorage.getItem.mockResolvedValue('new-data'); | ||
|
|
||
| const result = await storage.getItem('persist:root'); | ||
|
|
||
| expect(result).toBe('new-data'); | ||
| expect(oldStorage.getItem).not.toHaveBeenCalled(); | ||
| expect(oldStorage.removeItem).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('migrates from old to new storage when only old has the key', async () => { | ||
| newStorage.getItem.mockResolvedValue(null); | ||
| oldStorage.getItem.mockResolvedValue('legacy-data'); | ||
| newStorage.setItem.mockResolvedValue(undefined); | ||
| oldStorage.removeItem.mockResolvedValue(undefined); | ||
|
|
||
| const result = await storage.getItem('persist:root'); | ||
|
|
||
| expect(result).toBe('legacy-data'); | ||
| expect(newStorage.setItem).toHaveBeenCalledWith( | ||
| 'persist:root', | ||
| 'legacy-data' | ||
| ); | ||
| expect(oldStorage.removeItem).toHaveBeenCalledWith('persist:root'); | ||
| }); | ||
|
|
||
| it('returns null when neither storage has the key', async () => { | ||
| newStorage.getItem.mockResolvedValue(null); | ||
| oldStorage.getItem.mockResolvedValue(null); | ||
|
|
||
| const result = await storage.getItem('persist:root'); | ||
|
|
||
| expect(result).toBeNull(); | ||
| }); | ||
|
|
||
| it('falls back to old storage when new storage read fails', async () => { | ||
| newStorage.getItem.mockRejectedValue(new Error('IndexedDB error')); | ||
| oldStorage.getItem.mockResolvedValue('legacy-data'); | ||
| newStorage.setItem.mockResolvedValue(undefined); | ||
| oldStorage.removeItem.mockResolvedValue(undefined); | ||
|
|
||
| const result = await storage.getItem('persist:root'); | ||
|
|
||
| expect(result).toBe('legacy-data'); | ||
| expect(newStorage.setItem).toHaveBeenCalledWith( | ||
| 'persist:root', | ||
| 'legacy-data' | ||
| ); | ||
| }); | ||
|
|
||
| it('returns old value but does not remove from old storage when migration write fails', async () => { | ||
| newStorage.getItem.mockResolvedValue(null); | ||
| oldStorage.getItem.mockResolvedValue('legacy-data'); | ||
| newStorage.setItem.mockRejectedValue(new Error('write failed')); | ||
|
|
||
| const result = await storage.getItem('persist:root'); | ||
|
|
||
| expect(result).toBe('legacy-data'); | ||
| expect(oldStorage.removeItem).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('setItem', () => { | ||
| it('delegates to new storage only', async () => { | ||
| newStorage.setItem.mockResolvedValue(undefined); | ||
|
|
||
| await storage.setItem('persist:root', 'data'); | ||
|
|
||
| expect(newStorage.setItem).toHaveBeenCalledWith('persist:root', 'data'); | ||
| expect(oldStorage.setItem).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
|
|
||
| describe('removeItem', () => { | ||
| it('delegates to new storage only', async () => { | ||
| newStorage.removeItem.mockResolvedValue(undefined); | ||
|
|
||
| await storage.removeItem('persist:root'); | ||
|
|
||
| expect(newStorage.removeItem).toHaveBeenCalledWith('persist:root'); | ||
| expect(oldStorage.removeItem).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
| }); |
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
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.
Uh oh!
There was an error while loading. Please reload this page.