Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"env": {
"browser": true,
"node": true,
"jest": true
"jest": true,
"es6": true
},
"settings": {
"react": {
Expand Down
28 changes: 28 additions & 0 deletions __tests__/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
loadState,
saveState
} from '../src/helpers/localStorage';

describe('LocalStorage utilities', () => {
beforeEach(() => {
window.localStorage.clear();
});

const data = {
foo: 1
};

it('should save and load state', () => {
saveState(data);

const state = loadState();
expect(state).toMatchObject(data);
});

it('should not throw exceptions if localStorage is undefined', () => {
Reflect.deleteProperty(window, 'localStorage');

expect(saveState(data)).toBeUndefined();
expect(loadState()).toBeUndefined();
});
});
57 changes: 57 additions & 0 deletions __tests__/store/store.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
TOGGLE_JS,
TOGGLE_MODE
} from '../../src/static/constants/actions';
import { loadState, saveState } from '../../src/helpers/localStorage';

describe('Store', () => {
beforeEach(() => {
jest.resetModules();
window.localStorage.clear();
});

it('should work without saved state', () => {
const {
default: store,
initialState
} = require('../../src/store');

expect(store.getState()).toMatchObject(initialState);
});

it('should load saved state from localStorage', () => {
const savedState = {
mode: 'light',
js: 'es6'
};
saveState(savedState);

const state = require('../../src/store').default.getState();
expect(state.mode).toBe('light');
expect(state.js).toBe('es6');
});

it('should save state to localStorage', () => {
const toggleJSAction = {
type: TOGGLE_JS,
payload: 'es6'
};

const toggleModeAction = {
type: TOGGLE_MODE,
payload: 'light'
};

const {
default: store
} = require('../../src/store');

store.dispatch(toggleJSAction);
store.dispatch(toggleModeAction);

expect(loadState()).toMatchObject({
mode: 'light',
js: 'es6'
});
});
});
18 changes: 18 additions & 0 deletions src/helpers/localStorage.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
export const loadState = () => {
try {
const serializedState = JSON.parse(localStorage.getItem('state'));

return serializedState === null ? undefined : serializedState;
} catch (e) {
return undefined;
}
};

export const saveState = (state) => {
try {
const serializedState = JSON.stringify(state);
localStorage.setItem('state', serializedState);
} catch (e) {
return undefined;
}
};
27 changes: 24 additions & 3 deletions src/store/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { createStore, applyMiddleware, compose } from 'redux';
import {
createStore,
applyMiddleware,
compose
} from 'redux';
const uuid = require('uuid/v4');
import reducer from '../reducers/index';
import patterns from '../static/patterns';
import middleware from '../middleware';
import {
loadState,
saveState
} from '../helpers/localStorage';

export const answers = patterns.map(pattern => ({
...pattern,
Expand All @@ -18,16 +26,29 @@ export const initialProgress = {
current: answers[0]
};

const initialState = {
export const initialState = {
js: 'es5',
mode: 'dark',
intro: true,
patterns: answers,
progress: initialProgress
};

const state = {
...initialState,
...loadState()
};

const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;

const store = createStore(reducer, initialState, composeEnhancers(applyMiddleware(...middleware)));
const store = createStore(reducer, state, composeEnhancers(applyMiddleware(...middleware)));

store.subscribe(() => {
const currentState = store.getState();
saveState({
mode: currentState.mode,
js: currentState.js
});
});

export default store;