-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functions to save data to localStorage
- Loading branch information
1 parent
c69805f
commit 90fac36
Showing
11 changed files
with
112 additions
and
41 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
node_modules/* | ||
public/* | ||
coverage/* |
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
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
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,23 @@ | ||
// @flow | ||
import { tryCatch, always } from 'ramda'; | ||
import { logError } from './loggers'; | ||
|
||
type SavedState = Object; | ||
|
||
const dangerouslyLoadState = (): ?SavedState => { | ||
const serializedState = window.localStorage.getItem('state'); | ||
|
||
return serializedState | ||
? JSON.parse(serializedState) | ||
: undefined; | ||
}; | ||
|
||
const dangerouslySaveState = (state) => { | ||
localStorage.setItem('state', JSON.stringify(state)); | ||
}; | ||
|
||
export const loadState = | ||
tryCatch(dangerouslyLoadState, always(undefined)); | ||
|
||
export const saveState = | ||
tryCatch(dangerouslySaveState, logError); |
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,6 @@ | ||
/* eslint-disable import/prefer-default-export, no-console */ | ||
|
||
export const logError = err => { | ||
if (process.env.NODE_ENV !== 'development') return; | ||
console.log(err); | ||
}; |
This file was deleted.
Oops, something went wrong.
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
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
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,9 @@ | ||
import { LocalStorage } from 'node-localstorage'; | ||
import { jsdom } from 'jsdom'; | ||
|
||
|
||
global.localStorage = new LocalStorage(''); | ||
global.document = jsdom('<body></body>'); | ||
|
||
global.window = document.defaultView; | ||
global.window.localStorage = global.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 |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import test, { afterEach } from 'ava'; | ||
import sinon from 'sinon'; | ||
import { loadState, saveState } from '../app/local_storage'; | ||
|
||
const testData = { test: 'It works!' }; | ||
const testPayload = JSON.stringify(testData); | ||
const setItemStub = sinon.stub(); | ||
const getItemStub = sinon.stub(); | ||
|
||
|
||
localStorage.getItem = getItemStub; | ||
localStorage.setItem = setItemStub; | ||
|
||
|
||
afterEach(() => { | ||
setItemStub.reset(); | ||
getItemStub.reset(); | ||
}); | ||
|
||
test('loadState returns the payload', (t) => { | ||
getItemStub.returns(testPayload); | ||
t.deepEqual(loadState(), testData); | ||
}); | ||
|
||
test('loadState returns undefined if no state found', (t) => { | ||
getItemStub.returns(null); | ||
t.is(loadState(), undefined); | ||
}); | ||
|
||
test('loadState returns undefined if error in serializing JSON or accessing localStorage', (t) => { | ||
getItemStub.throws(new Error()); | ||
t.is(loadState(), undefined); | ||
}); | ||
|
||
test('saveState saves the payload to localStorage', (t) => { | ||
saveState(testData); | ||
t.plan(2); | ||
t.true(setItemStub.called); | ||
t.is(setItemStub.lastCall.args[1], testPayload); | ||
}); | ||
|
||
test('saveState returns undefined if error in parsing JSON or accessing localStorage', (t) => { | ||
setItemStub.throws(new Error()); | ||
saveState(testData); | ||
t.is(saveState(), undefined); | ||
}); |
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