-
Notifications
You must be signed in to change notification settings - Fork 488
/
Copy pathengine-any.test.js
107 lines (98 loc) · 2.52 KB
/
engine-any.test.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
'use strict'
import sinon from 'sinon'
import engineFactory from '../src/index'
describe('Engine: "any" conditions', () => {
let engine
let sandbox
before(() => {
sandbox = sinon.createSandbox()
})
afterEach(() => {
sandbox.restore()
})
describe('supports a single "any" condition', () => {
const event = {
type: 'ageTrigger',
params: {
demographic: 'under50'
}
}
const conditions = {
any: [{
fact: 'age',
operator: 'lessThan',
value: 50
}]
}
let eventSpy
let ageSpy
beforeEach(() => {
eventSpy = sandbox.spy()
ageSpy = sandbox.stub()
const rule = factories.rule({ conditions, event })
engine = engineFactory()
engine.addRule(rule)
engine.addFact('age', ageSpy)
engine.on('success', eventSpy)
})
it('emits when the condition is met', async () => {
ageSpy.returns(10)
await engine.run()
expect(eventSpy).to.have.been.calledWith(event)
})
it('does not emit when the condition fails', () => {
ageSpy.returns(75)
engine.run()
expect(eventSpy).to.not.have.been.calledWith(event)
})
})
describe('supports "any" with multiple conditions', () => {
const conditions = {
any: [{
fact: 'age',
operator: 'lessThan',
value: 50
}, {
fact: 'segment',
operator: 'equal',
value: 'european'
}]
}
const event = {
type: 'ageTrigger',
params: {
demographic: 'under50'
}
}
let eventSpy
let ageSpy
let segmentSpy
beforeEach(() => {
eventSpy = sandbox.spy()
ageSpy = sandbox.stub()
segmentSpy = sandbox.stub()
const rule = factories.rule({ conditions, event })
engine = engineFactory()
engine.addRule(rule)
engine.addFact('segment', segmentSpy)
engine.addFact('age', ageSpy)
engine.on('success', eventSpy)
})
it('emits an event when any condition is met', async () => {
segmentSpy.returns('north-american')
ageSpy.returns(25)
await engine.run()
expect(eventSpy).to.have.been.calledWith(event)
segmentSpy.returns('european')
ageSpy.returns(100)
await engine.run()
expect(eventSpy).to.have.been.calledWith(event)
})
it('does not emit when all conditions fail', async () => {
segmentSpy.returns('north-american')
ageSpy.returns(100)
await engine.run()
expect(eventSpy).to.not.have.been.calledWith(event)
})
})
})