From 3ed168309ec117c07817aff33831afa61077a573 Mon Sep 17 00:00:00 2001 From: Michael Contento Date: Thu, 3 Mar 2016 09:15:14 +0100 Subject: [PATCH] don't fail if localStorage is not defined (fixes #4) --- .moccarc | 1 + src/__tests__/index-test.js | 82 +++++++++++++++++++++++++++++++++++++ src/index.js | 17 +++++++- 3 files changed, 98 insertions(+), 2 deletions(-) create mode 100644 .moccarc diff --git a/.moccarc b/.moccarc new file mode 100644 index 0000000..91a18cc --- /dev/null +++ b/.moccarc @@ -0,0 +1 @@ +--globals localStorage diff --git a/src/__tests__/index-test.js b/src/__tests__/index-test.js index 4e2ffbf..8d38538 100644 --- a/src/__tests__/index-test.js +++ b/src/__tests__/index-test.js @@ -1,2 +1,84 @@ +import createEngine from '../'; + describe('engine', () => { + beforeEach(() => { + global.localStorage = { + getItem: sinon.stub().returns(null), + setItem: sinon.stub() + }; + }); + + afterEach(() => { + delete global.localStorage; + }); + + describe('load', () => { + it('should load via getItem', async () => { + localStorage.getItem.returns('{"a":1}'); + + const engine = createEngine('key'); + const result = await engine.load(); + + localStorage.getItem.should.have.been.called; + result.should.deep.equal({ a: 1 }); + }); + + it('should load with the given key', async () => { + const engine = createEngine('key'); + await engine.load(); + + localStorage.getItem.should.have.been.calledWith('key'); + }); + + it('should fallback to empty dict', async () => { + const engine = createEngine('key'); + const result = await engine.load(); + + localStorage.getItem.should.have.been.called; + result.should.be.a.dict; + result.should.be.empty; + }); + + it('should reject when localStorage is not present', () => { + delete global.localStorage; + + const engine = createEngine('key'); + return engine.load() + .should.eventually.be.rejectedWith('localStorage is not defined'); + }); + }); + + describe('save', () => { + it('should asve via setItem', async () => { + const engine = createEngine('key'); + await engine.save({}); + + localStorage.setItem + .should.have.been.called; + }); + + it('should load with the given key', async () => { + const engine = createEngine('key'); + await engine.save({}); + + localStorage.setItem + .should.have.been.calledWith('key'); + }); + + it('should save the passed state as json', async () => { + const engine = createEngine('key'); + await engine.save({ a: 1 }); + + localStorage.setItem + .should.have.been.calledWith(sinon.match.any, '{"a":1}'); + }); + + it('should reject when localStorage is not present', () => { + delete global.localStorage; + + const engine = createEngine('key'); + return engine.save({}) + .should.eventually.be.rejectedWith('localStorage is not defined'); + }); + }); }); diff --git a/src/index.js b/src/index.js index c1745a7..04a8668 100644 --- a/src/index.js +++ b/src/index.js @@ -1,12 +1,25 @@ export default (key) => ({ load() { - const jsonState = localStorage.getItem(key); + let jsonState; + + try { + jsonState = localStorage.getItem(key); + } catch (err) { + return Promise.reject(err.message); + } + return Promise.resolve(JSON.parse(jsonState) || {}); }, save(state) { const jsonState = JSON.stringify(state); - localStorage.setItem(key, jsonState); + + try { + localStorage.setItem(key, jsonState); + } catch (err) { + return Promise.reject(err.message); + } + return Promise.resolve(); } });