diff --git a/modules/DOMStateStorage.js b/modules/DOMStateStorage.js index b659d67c2..643a4c950 100644 --- a/modules/DOMStateStorage.js +++ b/modules/DOMStateStorage.js @@ -11,7 +11,11 @@ function createKey(key) { export function saveState(key, state) { try { - window.sessionStorage.setItem(createKey(key), JSON.stringify(state)) + if (state == null) { + window.sessionStorage.removeItem(createKey(key)) + } else { + window.sessionStorage.setItem(createKey(key), JSON.stringify(state)) + } } catch (error) { if (error.name === SecurityError) { // Blocking cookies in Chrome/Firefox/Safari throws SecurityError on any diff --git a/modules/__tests__/DOMStateStorage-test.js b/modules/__tests__/DOMStateStorage-test.js new file mode 100644 index 000000000..3fda9e950 --- /dev/null +++ b/modules/__tests__/DOMStateStorage-test.js @@ -0,0 +1,17 @@ +import expect from 'expect' +import { saveState, readState } from '../DOMStateStorage' + +describe('dom state storage', function () { + it('saves and reads state data', function () { + saveState('key1', { id: 1 }) + expect(readState('key1')).toEqual({ id: 1 }) + + saveState('key1', null) + expect(readState('key1')).toEqual(null) + + saveState('key2', { id: 2 }) + expect(readState('key2')).toEqual({ id: 2 }) + + expect(readState('key3')).toEqual(null) + }) +})