mon-entreprise/test/conversation.test.js

159 lines
4.1 KiB
JavaScript
Raw Normal View History

import { expect } from 'chai'
import { enrichRule, rulesFr as rules } from 'Engine/rules'
import { assocPath, merge } from 'ramda'
import reducers from 'Reducers/rootReducer'
import salariéConfig from '../source/components/simulationConfigs/salarié.yaml'
import {
currentQuestionSelector,
nextStepsSelector
} from '../source/selectors/analyseSelectors'
let baseState = {
conversationSteps: { foldedSteps: [] },
simulation: { situation: {} }
}
describe('conversation', function() {
it('should start with the first missing variable', function() {
let rawRules = [
// TODO - this won't work without the indirection, figure out why
{ nom: 'top . startHere', formule: { somme: ['a', 'b'] } },
{ nom: 'top . a', formule: 'aa' },
{ nom: 'top . b', formule: 'bb' },
{ nom: 'top . aa', question: '?', titre: 'a' },
{ nom: 'top . bb', question: '?', titre: 'b' }
],
rules = rawRules.map(enrichRule),
state = merge(baseState, {
simulation: { config: { objectifs: ['startHere'] } }
}),
currentQuestion = currentQuestionSelector(state, { rules })
expect(currentQuestion).to.equal('top . aa')
})
it('should deal with double unfold', function() {
let rawRules = [
// TODO - this won't work without the indirection, figure out why
{
nom: 'top . startHere',
formule: { somme: ['a', 'b', 'c'] }
},
{ nom: 'top . a', formule: 'aa' },
{ nom: 'top . b', formule: 'bb' },
{ nom: 'top . c', formule: 'cc' },
{ nom: 'top . aa', question: '?', titre: 'a' },
{ nom: 'top . bb', question: '?', titre: 'b' },
{ nom: 'top . cc', question: '?', titre: 'c' }
],
rules = rawRules.map(enrichRule)
let step1 = merge(baseState, {
rules,
simulation: { config: { objectifs: ['startHere'] } }
})
let step2 = reducers(
assocPath(['simulation', 'situation'], { 'top . aa': '1' }, step1),
{
type: 'STEP_ACTION',
name: 'fold',
step: 'top . aa'
}
)
let step3 = reducers(
assocPath(
['simulation', 'situation'],
{ 'top . aa': '1', 'top . bb': '1' },
step2
),
{
type: 'STEP_ACTION',
name: 'fold',
step: 'top . bb'
}
)
let step4 = reducers(step3, {
type: 'STEP_ACTION',
name: 'unfold',
step: 'top . aa'
})
let lastStep = reducers(step4, {
type: 'STEP_ACTION',
name: 'unfold',
step: 'top . bb'
})
expect(currentQuestionSelector(lastStep)).to.equal('top . bb')
expect(lastStep.conversationSteps).to.have.property('foldedSteps')
expect(lastStep.conversationSteps.foldedSteps).to.have.lengthOf(0)
})
it('should first ask for questions without defaults, then those with defaults', function() {
let rawRules = [
{ nom: 'net', formule: 'brut - cotisation' },
{
nom: 'brut',
question: 'Quel est le salaire brut ?',
unité: '€'
},
{
nom: 'cotisation',
formule: {
multiplication: {
assiette: 'brut',
variations: [
{
si: 'cadre',
alors: {
taux: '77%'
}
},
{
sinon: {
taux: '80%'
}
}
]
}
}
},
{
nom: 'cadre',
question: 'Est-ce un cadre ?',
'par défaut': 'non'
}
],
rules = rawRules.map(enrichRule)
let step1 = merge(baseState, {
rules,
simulation: { config: { objectifs: ['net'] } }
})
expect(currentQuestionSelector(step1)).to.equal('brut')
let step2 = reducers(
assocPath(['simulation', 'situation', 'brut'], '2300', step1),
{
type: 'STEP_ACTION',
name: 'fold',
step: 'brut'
}
)
expect(step2.conversationSteps).to.have.property('foldedSteps')
expect(step2.conversationSteps.foldedSteps).to.have.lengthOf(1)
expect(step2.conversationSteps.foldedSteps[0]).to.equal('brut')
expect(currentQuestionSelector(step2, { rules })).to.equal('cadre')
})
})
describe('real conversation', function() {
it('should not have more than X questions', function() {
let state = merge(baseState, {
simulation: { config: salariéConfig }
}),
nextSteps = nextStepsSelector(state, { rules })
expect(nextSteps.length).to.be.below(30) // If this breaks, that's good news
expect(nextSteps.length).to.be.above(10)
})
})