Skip to content
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

localStorageStore now delete only items prefixed on version change #8315

Merged
merged 2 commits into from
Oct 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion packages/ra-core/src/store/localStorageStore.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import expect from 'expect';

import { localStorageStore } from './localStorageStore';
import { localStorageStore, getStorage } from './localStorageStore';

describe('localStorageStore', () => {
it('should allow to store and retrieve a value', () => {
Expand Down Expand Up @@ -40,4 +40,32 @@ describe('localStorageStore', () => {
expect(store.getItem('foo')).toEqual(undefined);
});
});
describe('reset preserving items outside Ra-Store', () => {
it('should reset the store', () => {
const store = localStorageStore();
store.setItem('foo', 'bar');
getStorage().setItem('baz', 'baz'); //set custom item in localstorage
store.reset();
expect(getStorage().getItem('baz')).toEqual('baz'); //expect not to be wiped on store reset
expect(store.getItem('foo')).toEqual(undefined);
});
});
Comment on lines +43 to +52
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is good (and should be kept), but you are actually not testing the code you added. You should add a test about version change.

describe('changing version preserve localStorage items', () => {
it('should preserve localStorage items', () => {
const store = localStorageStore('1');
store.setItem('foo', 'bar');
getStorage().setItem('baz', 'baz'); //set custom item in localstorage
//change the localStorageStore version
//because actually the RA_STORE const is not exported, i search for the string "version" that is actually hardcoded in the keys
//also providing an actual default
const storeVersionName =
Object.getOwnPropertyNames(getStorage()).find(
i => i.indexOf('.version') > 0
) || 'RaStore.version';
getStorage().setItem(storeVersionName, '2');
store.setup();
expect(getStorage().getItem('baz')).toEqual('baz'); //expect not to be wiped on store reset
expect(store.getItem('foo')).toEqual(undefined); //deleted during setup
});
});
});
7 changes: 6 additions & 1 deletion packages/ra-core/src/store/localStorageStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,12 @@ export const localStorageStore = (
if (localStorageAvailable) {
const storedVersion = getStorage().getItem(`${prefix}.version`);
if (storedVersion && storedVersion !== version) {
getStorage().clear();
const storage = getStorage();
Object.keys(storage).forEach(key => {
if (key.startsWith(prefix)) {
storage.removeItem(key);
}
});
}
getStorage().setItem(`${prefix}.version`, version);
window.addEventListener('storage', onLocalStorageChange);
Expand Down