🐛 simplifie le code pour plier/déplier pour corriger les doublons d'unfold

pull/110/head
Laurent Bossavit 2017-10-19 17:20:13 +02:00
parent ad291efa1d
commit 1aef35d7a4
2 changed files with 12 additions and 9 deletions

View File

@ -97,14 +97,14 @@ export let reduceSteps = (tracker, flatRules, answerSource) => (state, action) =
tracker.push(['trackEvent', 'unfold', action.step]);
let stepFinder = R.propEq('name', action.step),
foldedSteps = R.reject(stepFinder)(state.foldedSteps).concat(state.unfoldedSteps),
extraSteps = R.reject(stepFinder)(state.extraSteps)
previous = R.head(state.unfoldedSteps),
foldedSteps = R.reject(stepFinder)(R.concat(state.foldedSteps, previous ? [previous] : [])),
unfolded = R.find(stepFinder)(R.concat(state.foldedSteps, state.extraSteps))
return {
...newState,
foldedSteps,
extraSteps,
unfoldedSteps: [R.find(stepFinder)(R.concat(state.foldedSteps,state.extraSteps))]
unfoldedSteps: R.concat([unfolded], state.unfoldedSteps)
}
}
}

View File

@ -40,11 +40,13 @@ describe('fold', function() {
let rawRules = [
// TODO - this won't work without the indirection, figure out why
{nom: "startHere", formule: {somme: ["a","b"]}, espace: "top"},
{nom: "startHere", formule: {somme: ["a","b","c"]}, espace: "top"},
{nom: "a", espace: "top", formule: "aa"},
{nom: "b", espace: "top", formule: "bb"},
{nom: "c", espace: "top", formule: "cc"},
{nom: "aa", question: "?", titre: "a", espace: "top"},
{nom: "bb", question: "?", titre: "b", espace: "top"}],
{nom: "bb", question: "?", titre: "b", espace: "top"},
{nom: "cc", question: "?", titre: "c", espace: "top"}],
rules = rawRules.map(enrichRule),
reducer = reduceSteps(tracker, rules, stateSelector)
@ -59,11 +61,12 @@ describe('fold', function() {
let result = step5
expect(result).to.have.property('unfoldedSteps')
expect(result.unfoldedSteps).to.have.lengthOf(1)
expect(result.unfoldedSteps).to.have.lengthOf(3)
expect(result.unfoldedSteps[0]).to.have.deep.property("name","top . bb")
expect(result).to.have.property('foldedSteps')
expect(result.foldedSteps).to.have.lengthOf(1)
expect(result.foldedSteps[0]).to.have.deep.property("name","top . aa")
expect(result.foldedSteps).to.have.lengthOf(2)
expect(result.foldedSteps[0]).to.have.deep.property("name","top . cc")
expect(result.foldedSteps[1]).to.have.deep.property("name","top . aa")
});
});