From 4ffeab701ad2275fbea61da97f25d42e2f0795f0 Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Wed, 18 Dec 2019 21:49:55 -0800 Subject: [PATCH] add reducer action SET_OPTIONS --- src/reducers/stateReducer.js | 4 +++ src/reducers/stateReducer.options.js | 20 ++++++++++++ src/reducers/stateReducer.options.test.js | 38 +++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/reducers/stateReducer.options.js create mode 100644 src/reducers/stateReducer.options.test.js diff --git a/src/reducers/stateReducer.js b/src/reducers/stateReducer.js index 24e7bc3..0b110b5 100644 --- a/src/reducers/stateReducer.js +++ b/src/reducers/stateReducer.js @@ -5,6 +5,8 @@ import { addEpoch, setEpoch, removeEpoch } from './stateReducer.epochs'; import type { epochAction } from './stateReducer.epochs'; import { addFeature } from './stateReducer.features'; import type { featureAction } from './stateReducer.features'; +import type { optionsAction } from './stateReducer.options'; +import { setOptions } from './stateReducer.options'; import { run } from './stateReducer.results'; import type { resultsAction } from './stateReducer.results' import { initState } from './stateReducer.init'; @@ -53,6 +55,8 @@ export const stateReducer = (state: stateType, action: actionType): stateType => case 'ADD_FEATURE': return addFeature(state, action); + case 'SET_OPTIONS': return setOptions(state, action); + case 'RUN': return run(state, action); default: return state; diff --git a/src/reducers/stateReducer.options.js b/src/reducers/stateReducer.options.js new file mode 100644 index 0000000..76263b0 --- /dev/null +++ b/src/reducers/stateReducer.options.js @@ -0,0 +1,20 @@ +// @flow +import type { stateType } from './stateReducer'; + +export type optionAction = { + type: 'SET_OPTIONS', + value: { + option: string, + setValue: string + } +}; + +export const setOptions = (state: stateType, action: optionAction): stateType => { + const option = action.value.option; + let value = action.value.setValue; + if (value === 'true') value = true; + if (value === 'false') value = false; + const mutatedState = {...state}; + mutatedState.options[option] = value; + return mutatedState; +} \ No newline at end of file diff --git a/src/reducers/stateReducer.options.test.js b/src/reducers/stateReducer.options.test.js new file mode 100644 index 0000000..08820b9 --- /dev/null +++ b/src/reducers/stateReducer.options.test.js @@ -0,0 +1,38 @@ +import { stateReducer } from './stateReducer'; +import { initState } from './stateReducer.init'; + +describe('Options', () => { + let state = {} + beforeEach(() => { + state = initState(); + }); + + it('Options returned unaltered', () => { + const action = {type: ''}; + expect(stateReducer(state, action)).toBe(state); + }); + + // output: 'default', save: false + it('Options change to output returns with changed value', () => { + const action = {type: 'SET_OPTIONS', value: {option: 'output', setValue: 'proto'}}; + expect(stateReducer(state, action)).toEqual( + {...state, + options: {...state.options, + output: 'proto' + } + } + ); + }); + + it('Options change to save returns with changed value', () => { + const action = {type: 'SET_OPTIONS', value: {option: 'save', setValue: 'true'}}; + expect(stateReducer(state, action)).toEqual( + {...state, + options: {...state.options, + save: true + } + } + ); + }); + +}); \ No newline at end of file