Skip to content

Commit

Permalink
♻️ Move savedSettings Middleware to subscribers
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeltomasik committed Sep 18, 2018
1 parent 9fa1349 commit b1a087d
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 67 deletions.
2 changes: 2 additions & 0 deletions src/store/index.js
Expand Up @@ -6,6 +6,7 @@ import env from '../constants/env';
import * as reducers from './reducers';
import middleWares from './middlewares';
import followedAccountsSubscriber from './subscribers/followedAccounts';
import settingsSubscriber from './subscribers/settings';

const App = combineReducers(reducers);

Expand All @@ -20,6 +21,7 @@ if (!env.test) {
}

store.subscribe(throttle(followedAccountsSubscriber.bind(null, store), 1000));
store.subscribe(throttle(settingsSubscriber.bind(null, store), 1000));

// ignore this in coverage as it is hard to test and does not run in production
/* istanbul ignore if */
Expand Down
2 changes: 0 additions & 2 deletions src/store/middlewares/index.js
Expand Up @@ -9,7 +9,6 @@ import offlineMiddleware from './offline';
import votingMiddleware from './voting';
import followedAccountsMiddleware from './followedAccounts';
import socketMiddleware from './socket';
import savedSettingsMiddleware from './savedSettings';

export default [
thunk,
Expand All @@ -22,5 +21,4 @@ export default [
// notificationMiddleware,
votingMiddleware,
followedAccountsMiddleware,
savedSettingsMiddleware,
];
18 changes: 0 additions & 18 deletions src/store/middlewares/savedSettings.js

This file was deleted.

47 changes: 0 additions & 47 deletions src/store/middlewares/savedSettings.test.js

This file was deleted.

8 changes: 8 additions & 0 deletions src/store/subscribers/settings.js
@@ -0,0 +1,8 @@
import { setSettingsInLocalStorage } from '../../utils/settings';

const settingsSubscriber = (store) => {
const { settings } = store.getState();
setSettingsInLocalStorage(settings);
};

export default settingsSubscriber;
24 changes: 24 additions & 0 deletions src/store/subscribers/settings.test.js
@@ -0,0 +1,24 @@
import { expect } from 'chai';
import { spy } from 'sinon';
import settings from './settings';
import * as settingsUtils from '../../utils/settings';

describe('Subscriber: settings(state)', () => {
const settingsObject = {
currency: 'USD',
autoLogout: true,
};

it('should save accounts in localStorage', () => {
spy(settingsUtils, 'setSettingsInLocalStorage');
const state = { settings: settingsObject };
const store = { getState: () => state };

settings(store);
expect(settingsUtils.setSettingsInLocalStorage)
.to.have.been.calledWith(state.settings);

settingsUtils.setSettingsInLocalStorage.restore();
});
});

5 changes: 5 additions & 0 deletions src/utils/settings.js
@@ -0,0 +1,5 @@
import localJSONStorage from './localJSONStorage';

export const setSettingsInLocalStorage = settings => localJSONStorage.set('settings', settings);

export const getSettingsFromLocalStorage = () => localJSONStorage.get('settings', {});
48 changes: 48 additions & 0 deletions src/utils/settings.test.js
@@ -0,0 +1,48 @@
import { expect } from 'chai';
import { stub } from 'sinon';
import {
getSettingsFromLocalStorage,
setSettingsInLocalStorage,
} from './settings';

describe('settings', () => {
const settingsObject = {
currency: 'USD',
autoLogout: true,
};

beforeEach(() => {
stub(localStorage, 'getItem');
stub(localStorage, 'setItem');
});

afterEach(() => {
localStorage.getItem.restore();
localStorage.setItem.restore();
});

describe('getSettingsFromLocalStorage', () => {
it('returns {} if if localStorage.getItem(\'settings\') returns undefined', () => {
expect(getSettingsFromLocalStorage()).to.deep.equal({});
});

it('returns {} if if localStorage.getItem(\'settings\') returns invalid JSON string', () => {
localStorage.getItem.returns('{]');
expect(getSettingsFromLocalStorage()).to.deep.equal({});
localStorage.getItem.returns('{}');
expect(getSettingsFromLocalStorage()).to.deep.equal({});
});

it('returns array parsed from json in localStorage.getItem(\'settings\')', () => {
localStorage.getItem.returns(JSON.stringify(settingsObject));
expect(getSettingsFromLocalStorage()).to.deep.equal(settingsObject);
});
});

describe('setSettingsInLocalStorage', () => {
it('sets settings in localStorage and also returns it', () => {
setSettingsInLocalStorage(settingsObject);
expect(localStorage.setItem).to.have.been.calledWith('settings', JSON.stringify(settingsObject));
});
});
});

0 comments on commit b1a087d

Please sign in to comment.