1
0
Fork 0
mirror of https://github.com/betagouv/mon-entreprise synced 2025-02-09 04:05:01 +00:00
mon-entreprise/test/regressions/simulations.jest.js
Johan Girod 00b122fa97 ⚙️ ajoute la conversion d'unité
Gros changements en perspective :
- Supprime la notion de période, au bénéfice de celle d'unité
  (`période : mensuelle` devient `unité: €/mois`)
- Améliore les rapports d'erreur avec des messages plus clair
- Ajoute un avertissement lorsque des types ne sont pas compatible
- Ajoute la conversion automatique d'unité dans le moteur
- Ajoute une notion d'unité par défaut de la simulation,
  c'est l'unité vers laquelle les règles qui ne spécifient pas
  d'unité seront converties
- Ajoute une notion d'unité par défaut des règles, qui spécifie
  l'unité de la règle qui prévaut lorsque qu'il n'y a pas
  d'unité par défaut de la simulation (utile pour les question ou
  pour s'assurer du bon type d'une règle)
2019-12-16 11:34:04 +01:00

93 lines
3.3 KiB
JavaScript

// The goal of these tests is to avoid deploying unwanted changes in the calculations. We run a number
// of simulations and persist their results in a snapshot (ie, a file commited in git). Our test runner,
// Jest, then compare the existing snapshot with the current Engine calculation and reports any difference.
//
// We only persist targets values in the file system, in order to be resilient to rule renaming (if a rule is
// renamed the test configuration may be adapted but the persisted snapshot will remain unchanged).
/* eslint-disable no-undef */
import artisteAuteurConfig from '../../source/components/simulationConfigs/artiste-auteur.yaml'
import autoentrepreneurConfig from '../../source/components/simulationConfigs/auto-entrepreneur.yaml'
import independantConfig from '../../source/components/simulationConfigs/indépendant.yaml'
import remunerationDirigeantConfig from '../../source/components/simulationConfigs/rémunération-dirigeant.yaml'
import employeeConfig from '../../source/components/simulationConfigs/salarié.yaml'
import Lib from '../../source/engine/index'
import artisteAuteurSituations from './simulations-artiste-auteur.yaml'
import autoEntrepreneurSituations from './simulations-auto-entrepreneur.yaml'
import independentSituations from './simulations-indépendant.yaml'
import remunerationDirigeantSituations from './simulations-rémunération-dirigeant.yaml'
import employeeSituations from './simulations-salarié.yaml'
const roundResult = arr => arr.map(x => Math.round(x))
const engine = new Lib.Engine()
const runSimulations = (
situations,
targets,
baseSituation = {},
defaultUnits,
namePrefix = ''
) =>
Object.entries(situations).map(([name, situations]) =>
situations.forEach(situation => {
const res = engine.evaluate(targets, {
situation: { ...baseSituation, ...situation },
defaultUnits
})
// Stringify is not required, but allows the result to be displayed in a single
// line in the snapshot, which considerably reduce the number of lines of this snapshot
// and improve its readability.
expect(JSON.stringify(roundResult(res))).toMatchSnapshot(
namePrefix + ' ' + name
)
})
)
it('calculate simulations-salarié', () => {
runSimulations(
employeeSituations,
employeeConfig.objectifs,
employeeConfig.situation,
['€/mois']
)
})
it('calculate simulations-indépendant', () => {
const targets = independantConfig.objectifs.reduce(
(acc, cur) => [...acc, ...cur.objectifs],
[]
)
runSimulations(independentSituations, targets, independantConfig.situation, [
'€/an'
])
})
it('calculate simulations-auto-entrepreneur', () => {
runSimulations(
autoEntrepreneurSituations,
autoentrepreneurConfig.objectifs,
autoentrepreneurConfig.situation,
['€/an']
)
})
it('calculate simulations-rémunération-dirigeant', () => {
const baseSituation = remunerationDirigeantConfig.situation
remunerationDirigeantConfig.branches.forEach(({ nom, situation }) => {
runSimulations(
remunerationDirigeantSituations,
remunerationDirigeantConfig.objectifs,
{ ...baseSituation, ...situation },
['€/an'],
`${nom} - `
)
})
})
it('calculate simulations-artiste-auteur', () => {
runSimulations(
artisteAuteurSituations,
artisteAuteurConfig.objectifs,
artisteAuteurConfig.situation,
['€/an']
)
})