-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathclasses.spec.js
79 lines (68 loc) · 1.75 KB
/
classes.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
const { Rools, Rule } = require('..');
require('./setup');
class Person {
constructor({ name, stars, salery }) {
this.name = name;
this.mood = 'unknown';
this.stars = stars;
this.salery = salery;
}
getStars() {
return this.stars;
}
setStars(stars) {
this.stars = stars;
}
getSalery() {
return this.salery;
}
setSalery(salery) {
this.salery = salery;
}
getMood() {
return this.mood;
}
setMood(mood) {
this.mood = mood;
}
}
const rule1 = new Rule({
name: 'mood is great if 200 stars or more',
when: (facts) => facts.user.getStars() >= 200,
then: (facts) => {
facts.user.setMood('great');
},
});
const rule2 = new Rule({
name: 'mark applicable if mood is great and salery greater 1000',
when: [
(facts) => facts.user.getMood() === 'great',
(facts) => facts.user.getSalery() > 1000,
],
then: (facts) => {
facts.result = true;
},
});
describe('Rools.evaluate() / classes with getters and setters', () => {
it('should set mood in 1 pass', async () => {
const facts = {
user: new Person({ name: 'frank', stars: 347, salery: 1234 }),
};
const rools = new Rools();
await rools.register([rule1]);
await rools.evaluate(facts);
// console.log(result); // eslint-disable-line no-console
expect(facts.user.mood).to.be.equal('great');
});
it('should set result in 2 passes', async () => {
const facts = {
user: new Person({ name: 'frank', stars: 347, salery: 1234 }),
};
const rools = new Rools();
await rools.register([rule1, rule2]);
await rools.evaluate(facts);
// console.log(result); // eslint-disable-line no-console
expect(facts.user.mood).to.be.equal('great');
expect(facts.result).to.be.equal(true);
});
});