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

Make the localStorage shim compliant with the Storage interface #1545

Merged
merged 3 commits into from
Feb 12, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 6 additions & 15 deletions www/src/js/storage/localStorage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import getLocalStorage, { createLocalStorageShim, canUseBrowserLocalStorage } from './localStorage';

describe('#createLocalStorageShim', () => {
describe(createLocalStorageShim, () => {
test('should store and return data', () => {
const shim = createLocalStorageShim();
const toStore = JSON.stringify({ key: 'value', key2: 2 });
Expand All @@ -24,20 +24,9 @@ describe('#createLocalStorageShim', () => {
expect(shim.privData).toEqual({});
});

test('should not throw when setting null/undefined', () => {
test('should return null when getting nonexistent data', () => {
const shim = createLocalStorageShim();
// @ts-ignore
expect(() => shim.setItem('key', null)).not.toThrow();
// @ts-ignore
expect(() => shim.setItem('key', undefined)).not.toThrow();
});

test('should return undefined when getting nonexistent data', () => {
const shim = createLocalStorageShim();
expect(shim.getItem('key')).toBeUndefined();
// @ts-ignore
shim.setItem('key', undefined);
expect(shim.getItem('key')).toBeUndefined();
expect(shim.getItem('key')).toBeNull();
});

test('should not throw when removing nonexistent key', () => {
Expand Down Expand Up @@ -69,11 +58,12 @@ describe('#canUseBrowserLocalStorage', () => {
test('should return true if localStorage is localStorage-like object', () => {
// @ts-ignore
window.localStorage = createLocalStorageShim();

expect(canUseBrowserLocalStorage()).toEqual(true);
});
});

describe('#getLocalStorage', () => {
describe(getLocalStorage, () => {
test("should return the actual browser's localStorage if the browser can use localStorage", () => {
expect(canUseBrowserLocalStorage()).toEqual(true);
expect(getLocalStorage()).toBe(window.localStorage);
Expand All @@ -82,6 +72,7 @@ describe('#getLocalStorage', () => {
test('should return a shim if browser cannot use localStorage', () => {
// @ts-ignore
delete window.localStorage;

expect(canUseBrowserLocalStorage()).toEqual(false);
expect(getLocalStorage()).toMatchObject(
expect.objectContaining({
Expand Down
7 changes: 3 additions & 4 deletions www/src/js/storage/localStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,14 @@ export function createLocalStorageShim(): Storage {
},

setItem(key: string, val: string) {
storage.privData[String(key)] = JSON.stringify(val);
storage.privData[key] = val;
},

getItem(key: string) {
const storedValue = storage.privData[String(key)];
return storedValue && JSON.parse(storedValue);
return storage.privData[key] || null;
},

removeItem: (key: string) => delete storage.privData[String(key)],
removeItem: (key: string) => delete storage.privData[key],
};

return storage;
Expand Down