Skip to content
This repository has been archived by the owner on Nov 29, 2018. It is now read-only.

Commit

Permalink
don't fail if localStorage is not defined (fixes #4)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelcontento committed Mar 3, 2016
1 parent 0c641fb commit 3ed1683
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
1 change: 1 addition & 0 deletions .moccarc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
--globals localStorage
82 changes: 82 additions & 0 deletions src/__tests__/index-test.js
Original file line number Diff line number Diff line change
@@ -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');
});
});
});
17 changes: 15 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -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();
}
});

0 comments on commit 3ed1683

Please sign in to comment.