1 | 'use strict'
|
2 |
|
3 | import engineFactory from '../src/index'
|
4 | import sinon from 'sinon'
|
5 |
|
6 | describe('Engine: run', () => {
|
7 | let engine, rule, rule2
|
8 |
|
9 | let event = { type: 'generic' }
|
10 | let condition21 = {
|
11 | any: [{
|
12 | fact: 'age',
|
13 | operator: 'greaterThanInclusive',
|
14 | value: 21
|
15 | }]
|
16 | }
|
17 | let condition75 = {
|
18 | any: [{
|
19 | fact: 'age',
|
20 | operator: 'greaterThanInclusive',
|
21 | value: 75
|
22 | }]
|
23 | }
|
24 | let eventSpy = sinon.spy()
|
25 |
|
26 | beforeEach(() => {
|
27 | eventSpy.reset()
|
28 | engine = engineFactory()
|
29 | rule = factories.rule({ conditions: condition21, event })
|
30 | engine.addRule(rule)
|
31 | rule2 = factories.rule({ conditions: condition75, event })
|
32 | engine.addRule(rule2)
|
33 | engine.on('success', eventSpy)
|
34 | })
|
35 |
|
36 | describe('independent runs', () => {
|
37 | it('treats each run() independently', async () => {
|
38 | await Promise.all([50, 10, 12, 30, 14, 15, 25].map((age) => engine.run({age})))
|
39 | expect(eventSpy).to.have.been.calledThrice()
|
40 | })
|
41 |
|
42 | it('allows runtime facts to override engine facts for a single run()', async () => {
|
43 | engine.addFact('age', 30)
|
44 |
|
45 | await engine.run({ age: 85 })
|
46 | expect(eventSpy).to.have.been.calledTwice()
|
47 |
|
48 | eventSpy.reset()
|
49 | await engine.run()
|
50 | expect(eventSpy).to.have.been.calledOnce()
|
51 |
|
52 | eventSpy.reset()
|
53 | await engine.run({ age: 2 })
|
54 | expect(eventSpy.callCount).to.equal(0)
|
55 | })
|
56 | })
|
57 |
|
58 | describe('returns', () => {
|
59 | it('activated events', () => {
|
60 | return engine.run({age: 30}).then(results => {
|
61 | expect(results.length).to.equal(1)
|
62 | expect(results).to.deep.include(rule.event)
|
63 | })
|
64 | })
|
65 |
|
66 | it('multiple activated events', () => {
|
67 | return engine.run({age: 90}).then(results => {
|
68 | expect(results.length).to.equal(2)
|
69 | expect(results).to.deep.include(rule.event)
|
70 | expect(results).to.deep.include(rule2.event)
|
71 | })
|
72 | })
|
73 |
|
74 | it('does not include unactived triggers', () => {
|
75 | return engine.run({age: 10}).then(results => {
|
76 | expect(results.length).to.equal(0)
|
77 | })
|
78 | })
|
79 | })
|
80 | })
|