add reducer action SET_OPTIONS

This commit is contained in:
Sorrel Bri 2019-12-18 21:49:55 -08:00
parent 77ebc5e1b9
commit 4ffeab701a
3 changed files with 62 additions and 0 deletions

View file

@ -5,6 +5,8 @@ import { addEpoch, setEpoch, removeEpoch } from './stateReducer.epochs';
import type { epochAction } from './stateReducer.epochs'; import type { epochAction } from './stateReducer.epochs';
import { addFeature } from './stateReducer.features'; import { addFeature } from './stateReducer.features';
import type { featureAction } 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 { run } from './stateReducer.results';
import type { resultsAction } from './stateReducer.results' import type { resultsAction } from './stateReducer.results'
import { initState } from './stateReducer.init'; 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 'ADD_FEATURE': return addFeature(state, action);
case 'SET_OPTIONS': return setOptions(state, action);
case 'RUN': return run(state, action); case 'RUN': return run(state, action);
default: return state; default: return state;

View file

@ -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;
}

View file

@ -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
}
}
);
});
});