Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Move savedSettings Middleware to subscribers
- Loading branch information
1 parent
9fa1349
commit b1a087d
Showing
8 changed files
with
87 additions
and
67 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
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 was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { setSettingsInLocalStorage } from '../../utils/settings'; | ||
|
||
const settingsSubscriber = (store) => { | ||
const { settings } = store.getState(); | ||
setSettingsInLocalStorage(settings); | ||
}; | ||
|
||
export default settingsSubscriber; |
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,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(); | ||
}); | ||
}); | ||
|
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,5 @@ | ||
import localJSONStorage from './localJSONStorage'; | ||
|
||
export const setSettingsInLocalStorage = settings => localJSONStorage.set('settings', settings); | ||
|
||
export const getSettingsFromLocalStorage = () => localJSONStorage.get('settings', {}); |
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,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)); | ||
}); | ||
}); | ||
}); |