diff --git a/source/reducers.js b/source/reducers.js index d735f25ff..e7afc6c91 100644 --- a/source/reducers.js +++ b/source/reducers.js @@ -23,10 +23,7 @@ let assume = (evaluator, assumptions) => state => name => { return userInput != null ? userInput : assumptions[name] } -export let reduceSteps = (state, action) => { - - let flatRules = rules - +export let reduceSteps = (flatRules, answerSource) => (state, action) => { if (![START_CONVERSATION, STEP_ACTION].includes(action.type)) return state @@ -38,7 +35,7 @@ export let reduceSteps = (state, action) => { hardAssumptions = R.pathOr({},['simulateur','hypothèses'],sim), // Soft assumptions are revealed after the simulation ends, and can be changed softAssumptions = R.pathOr({},['simulateur','par défaut'],sim), - intermediateSituation = assume(fromConversation, hardAssumptions), + intermediateSituation = assume(answerSource, hardAssumptions), completeSituation = assume(intermediateSituation,softAssumptions) let situationGate = completeSituation(state), @@ -52,10 +49,12 @@ export let reduceSteps = (state, action) => { } if (action.type == START_CONVERSATION) { + let unfoldedSteps = buildNextSteps(situationGate, flatRules, newState.analysedSituation) + return { ...newState, foldedSteps: [], - unfoldedSteps: buildNextSteps(situationGate, flatRules, newState.analysedSituation) + unfoldedSteps } } if (action.type == STEP_ACTION && action.name == 'fold') { @@ -148,5 +147,5 @@ export default reduceReducers( }), // cross-cutting concerns because here `state` is the whole state tree - reduceSteps + reduceSteps(rules, fromConversation) ) diff --git a/test/reducers.test.js b/test/reducers.test.js new file mode 100644 index 000000000..0a55c5560 --- /dev/null +++ b/test/reducers.test.js @@ -0,0 +1,34 @@ +import {expect} from 'chai' +import {rules as realRules, enrichRule} from '../source/engine/rules' +import {analyseSituation, analyseTopDown} from '../source/engine/traverse' +import {buildNextSteps, collectMissingVariables, getObjectives} from '../source/engine/generateQuestions' + +import {reduceSteps} from '../source/reducers' + +let stateSelector = state => name => null + +describe('fold', function() { + + it('should start conversation with only unfolded questions', function() { + let rawRules = [ + // TODO - this won't work without the indirection, figure out why + {nom: "startHere", formule: {somme: ["a","b"]}, espace: "top"}, + {nom: "a", espace: "top", formule: "aa"}, + {nom: "b", espace: "top", formule: "bb"}, + {nom: "aa", question: "?", titre: "a", espace: "top"}, + {nom: "bb", question: "?", titre: "b", espace: "top"}], + rules = rawRules.map(enrichRule), + reducer = reduceSteps(rules, stateSelector), + action = {type:'START_CONVERSATION', rootVariable: 'startHere'}, + // situation = analyseTopDown(rules,"startHere")(stateSelector({})), + // objectives = getObjectives(stateSelector({}), situation.root, situation.parsedRules), + // missing = collectMissingVariables()(stateSelector({}),situation), + result = reducer({},action) + + expect(result).to.have.property('unfoldedSteps') + expect(result.unfoldedSteps).to.have.lengthOf(2) + expect(result.unfoldedSteps[0]).to.have.deep.property("name","top . aa") + expect(result.unfoldedSteps[1]).to.have.deep.property("name","top . bb") + }); + +});