-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathreevaluate.spec.js
64 lines (62 loc) · 2.23 KB
/
reevaluate.spec.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const { Rools, Rule } = require('..');
require('./setup');
describe('Rools.evaluate() / re-evaluate', () => {
it('should re-evaluate premises only if facts are changed / row', async () => {
const premisesEvaluated = [];
const actionsFired = [];
const rule1 = new Rule({
name: 'rule1',
when: (facts) => { premisesEvaluated.push(1); return facts.fact1; },
then: (facts) => { actionsFired.push(1); facts.fact2 = true; },
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => { premisesEvaluated.push(2); return facts.fact2; },
then: (facts) => { actionsFired.push(2); facts.fact3 = true; },
});
const rule3 = new Rule({
name: 'rule3',
when: (facts) => { premisesEvaluated.push(3); return facts.fact3; },
then: () => { actionsFired.push(3); },
});
const facts = {
fact1: true,
fact2: false,
fact3: false,
};
const rools = new Rools();
await rools.register([rule1, rule2, rule3]);
await rools.evaluate(facts);
expect(premisesEvaluated).to.be.deep.equal([1, 2, 3, 2, 3]);
expect(actionsFired).to.be.deep.equal([1, 2, 3]);
});
it('should re-evaluate premises only if facts are changed / complex', async () => {
const premisesEvaluated = [];
const actionsFired = [];
const rule1 = new Rule({
name: 'rule1',
when: (facts) => { premisesEvaluated.push(1); return facts.fact1; },
then: (facts) => { actionsFired.push(1); facts.fact2 = true; },
});
const rule2 = new Rule({
name: 'rule2',
when: (facts) => { premisesEvaluated.push(2); return facts.fact2; },
then: (facts) => { actionsFired.push(2); facts.fact1 = false; facts.fact3 = true; },
});
const rule3 = new Rule({
name: 'rule3',
when: (facts) => { premisesEvaluated.push(3); return facts.fact3; },
then: (facts) => { actionsFired.push(3); facts.fact2 = false; },
});
const facts = {
fact1: true,
fact2: false,
fact3: false,
};
const rools = new Rools();
await rools.register([rule1, rule2, rule3]);
await rools.evaluate(facts);
expect(premisesEvaluated).to.be.deep.equal([1, 2, 3, 2, 1, 3, 2]);
expect(actionsFired).to.be.deep.equal([1, 2, 3]);
});
});