mon-entreprise/publicodes/test/inversion.test.js

222 lines
5.0 KiB
JavaScript
Raw Normal View History

import { expect } from 'chai'
import dedent from 'dedent-js'
import Engine from '../source/index'
describe('inversions', () => {
it('should handle non inverted example', () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: brut
taux: 77%
2020-03-26 15:03:19 +00:00
brut:
unité:
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules)
2020-03-26 15:03:19 +00:00
.setSituation({ brut: 2300 })
.evaluate('net')
2020-03-26 15:03:19 +00:00
expect(result.nodeValue).to.be.closeTo(1771, 0.001)
})
it('should handle simple inversion', () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: brut
taux: 77%
2020-03-26 15:03:19 +00:00
brut:
unité:
formule:
inversion numérique:
avec:
- net
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules)
2020-03-26 15:03:19 +00:00
.setSituation({ net: 2000 })
.evaluate('brut')
2020-03-26 15:03:19 +00:00
expect(result.nodeValue).to.be.closeTo(2000 / (77 / 100), 0.0001 * 2000)
})
it('should handle inversion with value at 0', () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: brut
taux: 77%
2020-03-26 15:03:19 +00:00
brut:
unité:
formule:
inversion numérique:
avec:
- net
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules).setSituation({ net: 0 }).evaluate('brut')
2020-03-26 15:03:19 +00:00
expect(result.nodeValue).to.be.closeTo(0, 0.0001)
})
it('should ask the input of one of the possible inversions', () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: assiette
variations:
- si: cadre
alors:
taux: 80%
- sinon:
taux: 70%
2020-03-26 15:03:19 +00:00
brut:
unité:
formule:
inversion numérique:
avec:
- net
2020-03-26 15:03:19 +00:00
cadre:
assiette:
formule: 67 + brut
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules).evaluate('brut')
2020-03-26 15:03:19 +00:00
expect(result.nodeValue).to.be.null
expect(Object.keys(result.missingVariables)).to.include('brut')
})
it('should handle inversions with missing variables', () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: assiette
variations:
- si: cadre
alors:
taux: 80%
- sinon:
taux: 70%
2020-03-26 15:03:19 +00:00
brut:
unité:
formule:
inversion numérique:
avec:
- net
2020-03-26 15:03:19 +00:00
cadre:
assiette:
formule:
somme:
- 1200
- brut
- taxeOne
2020-03-26 15:03:19 +00:00
taxeOne:
non applicable si: cadre
formule: taxe + taxe
2020-03-26 15:03:19 +00:00
taxe:
formule:
produit:
assiette: 1200
variations:
- si: cadre
alors:
taux: 80%
- sinon:
taux: 70%
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules)
2020-03-26 15:03:19 +00:00
.setSituation({ net: 2000 })
.evaluate('brut')
expect(result.nodeValue).to.be.null
expect(Object.keys(result.missingVariables)).to.include('cadre')
})
it("shouldn't report a missing salary if another salary was input", () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: assiette
variations:
- si: cadre
alors:
taux: 80%
- si: cadre != oui
alors:
taux: 70%
2020-03-26 15:03:19 +00:00
total:
formule:
produit:
assiette: assiette
taux: 150%
2020-03-26 15:03:19 +00:00
brut:
unité:
formule:
inversion numérique:
avec:
- net
- total
2020-03-26 15:03:19 +00:00
cadre:
2020-03-26 15:03:19 +00:00
assiette:
formule: 67 + brut
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules)
2020-03-26 15:03:19 +00:00
.setSituation({ net: 2000, cadre: 'oui' })
.evaluate('total')
expect(result.nodeValue).to.be.closeTo(3750, 1)
expect(Object.keys(result.missingVariables)).to.be.empty
})
2020-03-26 15:03:19 +00:00
it('complex inversion with composantes', () => {
2020-03-26 15:03:19 +00:00
const rules = dedent`
net:
formule:
produit:
assiette: 67 + brut
taux: 80%
2020-03-26 15:03:19 +00:00
cotisation:
formule:
produit:
assiette: 67 + brut
composantes:
- attributs:
par: employeur
taux: 100%
- attributs:
par: salarié
taux: 50%
2020-03-26 15:03:19 +00:00
total:
formule: cotisation .employeur + cotisation .salarié
2020-03-26 15:03:19 +00:00
brut:
unité:
formule:
inversion numérique:
avec:
- net
- total
2020-03-26 15:03:19 +00:00
`
const result = new Engine(rules)
2020-03-26 15:03:19 +00:00
.setSituation({ net: 2000 })
.evaluate('total')
expect(result.nodeValue).to.be.closeTo(3750, 1)
expect(Object.keys(result.missingVariables)).to.be.empty
})
})