This repository has been archived by the owner on Nov 29, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
don't fail if localStorage is not defined (fixes #4)
- Loading branch information
1 parent
0c641fb
commit 3ed1683
Showing
3 changed files
with
98 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
--globals localStorage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
}); |