Skip to content

Commit 8485a04

Browse files
committed
Add tests for not condition
1 parent 90272d6 commit 8485a04

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

test/engine-not.test.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
'use strict'
2+
3+
import sinon from 'sinon'
4+
import engineFactory from '../src/index'
5+
6+
describe('Engine: "not" conditions', () => {
7+
let engine
8+
let sandbox
9+
before(() => {
10+
sandbox = sinon.createSandbox()
11+
})
12+
afterEach(() => {
13+
sandbox.restore()
14+
})
15+
16+
describe('supports a single "not" condition', () => {
17+
const event = {
18+
type: 'ageTrigger',
19+
params: {
20+
demographic: 'under50'
21+
}
22+
}
23+
const conditions = {
24+
not: {
25+
fact: 'age',
26+
operator: 'greaterThanInclusive',
27+
value: 50
28+
}
29+
}
30+
let eventSpy
31+
let ageSpy
32+
beforeEach(() => {
33+
eventSpy = sandbox.spy()
34+
ageSpy = sandbox.stub()
35+
const rule = factories.rule({ conditions, event })
36+
engine = engineFactory()
37+
engine.addRule(rule)
38+
engine.addFact('age', ageSpy)
39+
engine.on('success', eventSpy)
40+
})
41+
42+
it('emits when the condition is met', async () => {
43+
ageSpy.returns(10)
44+
await engine.run()
45+
expect(eventSpy).to.have.been.calledWith(event)
46+
})
47+
48+
it('does not emit when the condition fails', () => {
49+
ageSpy.returns(75)
50+
engine.run()
51+
expect(eventSpy).to.not.have.been.calledWith(event)
52+
})
53+
})
54+
})

test/engine-recusive-rules.test.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,72 @@ describe('Engine: recursive rules', () => {
162162
expect(eventSpy).to.not.have.been.calledOnce()
163163
})
164164
})
165+
166+
const notNotCondition = {
167+
not: {
168+
not: {
169+
fact: 'age',
170+
operator: 'lessThan',
171+
value: 65
172+
}
173+
}
174+
}
175+
176+
describe('"not" nested directly within a "not"', () => {
177+
it('evaluates true when facts pass rules', async () => {
178+
setup(notNotCondition)
179+
engine.addFact('age', 30)
180+
await engine.run()
181+
expect(eventSpy).to.have.been.calledOnce()
182+
})
183+
184+
it('evaluates false when facts do not pass rules', async () => {
185+
setup(notNotCondition)
186+
engine.addFact('age', 65)
187+
await engine.run()
188+
expect(eventSpy).to.not.have.been.calledOnce()
189+
})
190+
})
191+
192+
const nestedNotCondition = {
193+
not: {
194+
all: [
195+
{
196+
fact: 'age',
197+
operator: 'lessThan',
198+
value: 65
199+
},
200+
{
201+
fact: 'age',
202+
operator: 'greaterThan',
203+
value: 21
204+
},
205+
{
206+
not: {
207+
fact: 'income',
208+
operator: 'lessThanInclusive',
209+
value: 100
210+
}
211+
}
212+
]
213+
}
214+
}
215+
216+
describe('outer "not" with nested "all" and nested "not" condition', () => {
217+
it('evaluates true when facts pass rules', async () => {
218+
setup(nestedNotCondition)
219+
engine.addFact('age', 30)
220+
engine.addFact('income', 100)
221+
await engine.run()
222+
expect(eventSpy).to.have.been.calledOnce()
223+
})
224+
225+
it('evaluates false when facts do not pass rules', async () => {
226+
setup(nestedNotCondition)
227+
engine.addFact('age', 30)
228+
engine.addFact('income', 101)
229+
await engine.run()
230+
expect(eventSpy).to.not.have.been.calledOnce()
231+
})
232+
})
165233
})

0 commit comments

Comments
 (0)