From a6b362efdaff19d552e6189ec5a30bb90ffad8fa Mon Sep 17 00:00:00 2001 From: Sorrel Bri Date: Wed, 4 Dec 2019 15:52:46 -0800 Subject: [PATCH] add reducer actions ADD_LEXEME, SET_LEXEME --- src/reducers/stateReducer.features.test.js | 17 +++++++ src/reducers/stateReducer.js | 16 +++++- src/reducers/stateReducer.lexicon.test.js | 58 ++++++++++++++++++++++ src/reducers/stateReducer.test.js | 8 +++ 4 files changed, 98 insertions(+), 1 deletion(-) create mode 100644 src/reducers/stateReducer.features.test.js create mode 100644 src/reducers/stateReducer.lexicon.test.js diff --git a/src/reducers/stateReducer.features.test.js b/src/reducers/stateReducer.features.test.js new file mode 100644 index 0000000..67b9682 --- /dev/null +++ b/src/reducers/stateReducer.features.test.js @@ -0,0 +1,17 @@ +import {stateReducer} from './stateReducer'; + +describe('Features', () => { + const state = { + features: [ + 'low', 'high','back', 'rounded', 'sonorant', + 'nasal', 'obstruent', 'occlusive', 'plosive', + 'prenasalized', 'aspirated', 'coronal', 'anterior' + ] + }; + + it('features returned unaltered', () => { + const action = {type: ''}; + expect(stateReducer(state, action)).toBe(state); + }); + +}); \ No newline at end of file diff --git a/src/reducers/stateReducer.js b/src/reducers/stateReducer.js index e2a4923..ca3173c 100644 --- a/src/reducers/stateReducer.js +++ b/src/reducers/stateReducer.js @@ -9,7 +9,21 @@ const stateReducer = (state, action) => { case 'INIT': { return initState(); } - + + case 'ADD_LEXEME': { + let newLexeme = action.value; + if (!newLexeme.epoch) newLexeme.epoch = state.epochs[0].name; + return {...state, lexicon:[...state.lexicon, newLexeme]} + } + + case 'SET_LEXICON': { + let newLexicon = action.value; + newLexicon = newLexicon.map(lexeme => lexeme.epoch + ? lexeme + : {...lexeme, epoch: state.epochs[0].name}); + return {...state, lexicon: newLexicon} + } + default: return state; } diff --git a/src/reducers/stateReducer.lexicon.test.js b/src/reducers/stateReducer.lexicon.test.js new file mode 100644 index 0000000..75da307 --- /dev/null +++ b/src/reducers/stateReducer.lexicon.test.js @@ -0,0 +1,58 @@ +import {stateReducer} from './stateReducer'; + +describe('Lexicon', () => { + const state = { + lexicon: [ + {lexeme:'anta', epoch:'epoch 1'}, + {lexeme:'anat', epoch:'epoch 1'}, + {lexeme:'anət', epoch:'epoch 1'}, + {lexeme:'anna', epoch:'epoch 1'}, + {lexeme:'tan', epoch:'epoch 1'}, + {lexeme:'ənta', epoch:'epoch 1'} + ], + epochs: [{name: 'epoch 1'}] + }; + + it('lexicon returned unaltered', () => { + const action = {type: ''}; + expect(stateReducer(state, action)).toBe(state); + }); + + it('lexicon addition without epoch returns updated lexicon with default epoch', () => { + const action = {type: 'ADD_LEXEME', value: {lexeme:'ntʰa'}} + expect(stateReducer(state, action)).toEqual({...state, lexicon:[...state.lexicon, {lexeme:'ntʰa', epoch:'epoch 1'}]}); + }); + + it('lexicon addition with epoch returns updated lexicon with correct epoch', () => { + const action = {type: 'ADD_LEXEME', value: {lexeme:'ntʰa', epoch: 'epoch 2'}} + expect(stateReducer(state, action)).toEqual({...state, lexicon:[...state.lexicon, action.value]}); + }); + + it('lexicon set returns updated lexicon with correct epoch', () => { + const newLexicon = [ + {lexeme:'anta', epoch:'epoch 1'}, + {lexeme:'anat', epoch:'epoch 1'}, + {lexeme:'anət', epoch:'epoch 1'}, + {lexeme:'anna', epoch:'epoch 1'} + ] + const action = {type: 'SET_LEXICON', value: newLexicon} + expect(stateReducer(state, action)).toEqual({...state, lexicon:newLexicon}); + }); + + it('lexicon set with no epoch returns updated lexicon with defaul epoch', () => { + const newLexicon = [ + {lexeme:'anta', epoch:'epoch 1'}, + {lexeme:'anat', epoch:'epoch 1'}, + {lexeme:'anət', epoch:'epoch 2'}, + {lexeme:'anna', epoch:'epoch 1'} + ] + const inputLexicon = [ + {lexeme:'anta'}, + {lexeme:'anat'}, + {lexeme:'anət', epoch:'epoch 2'}, + {lexeme:'anna'} + ] + const action = {type: 'SET_LEXICON', value: inputLexicon} + expect(stateReducer(state, action)).toEqual({...state, lexicon:newLexicon}); + }) +}); \ No newline at end of file diff --git a/src/reducers/stateReducer.test.js b/src/reducers/stateReducer.test.js index e69de29..a4920d8 100644 --- a/src/reducers/stateReducer.test.js +++ b/src/reducers/stateReducer.test.js @@ -0,0 +1,8 @@ +import {stateReducer} from './stateReducer'; + +it('default returns state unaltered', () => { + const state = {data: 'example'}; + const action = {type: ''}; + expect(stateReducer(state, action)).toBe(state); +}); +