2018-01-03 15:54:19 +00:00
|
|
|
import { expect } from 'chai'
|
|
|
|
import dedent from 'dedent-js'
|
2020-05-15 14:16:13 +00:00
|
|
|
import Engine from '../source/index'
|
2018-01-03 15:54:19 +00:00
|
|
|
|
|
|
|
describe('inversions', () => {
|
|
|
|
it('should handle non inverted example', () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-20 12:16:18 +00:00
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2019-07-05 13:00:24 +00:00
|
|
|
unité: €
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +00:00
|
|
|
const result = new Engine(rules)
|
2020-03-26 15:03:19 +00:00
|
|
|
.setSituation({ brut: 2300 })
|
|
|
|
.evaluate('net')
|
2017-11-06 17:05:45 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
expect(result.nodeValue).to.be.closeTo(1771, 0.001)
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it('should handle simple inversion', () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-20 12:16:18 +00:00
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2019-07-05 13:00:24 +00:00
|
|
|
unité: €
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2019-02-03 16:27:55 +00:00
|
|
|
inversion numérique:
|
2017-11-20 12:16:18 +00:00
|
|
|
avec:
|
|
|
|
- net
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +00:00
|
|
|
const result = new Engine(rules)
|
2020-03-26 15:03:19 +00:00
|
|
|
.setSituation({ net: 2000 })
|
|
|
|
.evaluate('brut')
|
2017-11-07 08:48:13 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
expect(result.nodeValue).to.be.closeTo(2000 / (77 / 100), 0.0001 * 2000)
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2017-11-07 08:48:13 +00:00
|
|
|
|
2020-01-14 14:13:37 +00:00
|
|
|
it('should handle inversion with value at 0', () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2020-01-14 14:13:37 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2020-01-14 14:13:37 +00:00
|
|
|
assiette: brut
|
|
|
|
taux: 77%
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2020-01-14 14:13:37 +00:00
|
|
|
unité: €
|
|
|
|
formule:
|
|
|
|
inversion numérique:
|
|
|
|
avec:
|
|
|
|
- net
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +00:00
|
|
|
const result = new Engine(rules).setSituation({ net: 0 }).evaluate('brut')
|
2020-01-14 14:13:37 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
expect(result.nodeValue).to.be.closeTo(0, 0.0001)
|
2020-01-14 14:13:37 +00:00
|
|
|
})
|
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it('should ask the input of one of the possible inversions', () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-20 12:16:18 +00:00
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 80%
|
|
|
|
- sinon:
|
|
|
|
taux: 70%
|
2017-11-20 12:16:18 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2019-07-05 13:00:24 +00:00
|
|
|
unité: €
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2019-02-03 16:27:55 +00:00
|
|
|
inversion numérique:
|
2017-11-20 12:16:18 +00:00
|
|
|
avec:
|
|
|
|
- net
|
2020-03-26 15:03:19 +00:00
|
|
|
cadre:
|
|
|
|
assiette:
|
2020-04-23 07:30:03 +00:00
|
|
|
formule: 67€ + brut
|
2017-11-20 12:16:18 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +00:00
|
|
|
const result = new Engine(rules).evaluate('brut')
|
2017-11-16 13:04:51 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
expect(result.nodeValue).to.be.null
|
|
|
|
expect(Object.keys(result.missingVariables)).to.include('brut')
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2017-11-16 13:04:51 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it('should handle inversions with missing variables', () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-20 12:16:18 +00:00
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2019-02-03 16:27:55 +00:00
|
|
|
alors:
|
2018-08-07 19:20:25 +00:00
|
|
|
taux: 80%
|
|
|
|
- sinon:
|
|
|
|
taux: 70%
|
2017-11-20 12:16:18 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2019-07-05 13:00:24 +00:00
|
|
|
unité: €
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2019-02-03 16:27:55 +00:00
|
|
|
inversion numérique:
|
2017-11-20 12:16:18 +00:00
|
|
|
avec:
|
|
|
|
- net
|
2020-03-26 15:03:19 +00:00
|
|
|
cadre:
|
|
|
|
assiette:
|
2018-04-10 15:16:27 +00:00
|
|
|
formule:
|
|
|
|
somme:
|
|
|
|
- 1200
|
|
|
|
- brut
|
|
|
|
- taxeOne
|
2020-03-26 15:03:19 +00:00
|
|
|
taxeOne:
|
2018-04-10 15:16:27 +00:00
|
|
|
non applicable si: cadre
|
|
|
|
formule: taxe + taxe
|
2020-03-26 15:03:19 +00:00
|
|
|
taxe:
|
2018-04-10 15:16:27 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2020-04-23 07:30:03 +00:00
|
|
|
assiette: 1200 €
|
2018-04-10 15:16:27 +00:00
|
|
|
variations:
|
|
|
|
- si: cadre
|
2019-02-03 16:27:55 +00:00
|
|
|
alors:
|
2018-08-07 19:20:25 +00:00
|
|
|
taux: 80%
|
|
|
|
- sinon:
|
|
|
|
taux: 70%
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +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')
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2018-01-03 15:54:19 +00:00
|
|
|
it("shouldn't report a missing salary if another salary was input", () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2017-11-14 15:01:16 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-14 15:01:16 +00:00
|
|
|
assiette: assiette
|
|
|
|
variations:
|
|
|
|
- si: cadre
|
2018-08-07 19:20:25 +00:00
|
|
|
alors:
|
|
|
|
taux: 80%
|
2019-07-11 15:28:43 +00:00
|
|
|
- si: cadre != oui
|
2019-02-03 16:27:55 +00:00
|
|
|
alors:
|
2018-08-07 19:20:25 +00:00
|
|
|
taux: 70%
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
total:
|
2017-11-14 15:01:16 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-14 15:01:16 +00:00
|
|
|
assiette: assiette
|
|
|
|
taux: 150%
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2019-07-05 13:00:24 +00:00
|
|
|
unité: €
|
2017-11-20 12:16:18 +00:00
|
|
|
formule:
|
2019-02-03 16:27:55 +00:00
|
|
|
inversion numérique:
|
2017-11-20 12:16:18 +00:00
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
- total
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
cadre:
|
2017-11-14 15:01:16 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
assiette:
|
2017-11-14 15:01:16 +00:00
|
|
|
formule: 67 + brut
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +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
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|
2020-03-26 15:03:19 +00:00
|
|
|
|
2018-09-19 13:16:04 +00:00
|
|
|
it('complex inversion with composantes', () => {
|
2020-03-26 15:03:19 +00:00
|
|
|
const rules = dedent`
|
|
|
|
net:
|
2017-11-23 13:48:42 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-23 13:48:42 +00:00
|
|
|
assiette: 67 + brut
|
|
|
|
taux: 80%
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
cotisation:
|
2017-11-23 13:48:42 +00:00
|
|
|
formule:
|
2020-03-11 13:08:41 +00:00
|
|
|
produit:
|
2017-11-23 13:48:42 +00:00
|
|
|
assiette: 67 + brut
|
|
|
|
composantes:
|
|
|
|
- attributs:
|
|
|
|
dû par: employeur
|
|
|
|
taux: 100%
|
|
|
|
- attributs:
|
|
|
|
dû par: salarié
|
|
|
|
taux: 50%
|
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
total:
|
2019-11-28 11:03:23 +00:00
|
|
|
formule: cotisation .employeur + cotisation .salarié
|
2017-11-23 13:48:42 +00:00
|
|
|
|
2020-03-26 15:03:19 +00:00
|
|
|
brut:
|
2019-07-05 13:00:24 +00:00
|
|
|
unité: €
|
2017-11-23 13:48:42 +00:00
|
|
|
formule:
|
2019-02-03 16:27:55 +00:00
|
|
|
inversion numérique:
|
2017-11-23 13:48:42 +00:00
|
|
|
avec:
|
|
|
|
- net
|
|
|
|
- total
|
2020-03-26 15:03:19 +00:00
|
|
|
`
|
2020-04-23 07:30:03 +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
|
2018-09-19 13:16:04 +00:00
|
|
|
})
|
2018-01-03 15:54:19 +00:00
|
|
|
})
|