-
Notifications
You must be signed in to change notification settings - Fork 490
/
Copy pathengine-operator.test.js
71 lines (65 loc) · 2 KB
/
engine-operator.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
'use strict'
import sinon from 'sinon'
import engineFactory from '../src/index'
async function dictionary (params, engine) {
const words = ['coffee', 'Aardvark', 'moose', 'ladder', 'antelope']
return words[params.wordIndex]
}
describe('Engine: operator', () => {
let sandbox
before(() => {
sandbox = sinon.createSandbox()
})
afterEach(() => {
sandbox.restore()
})
const event = {
type: 'operatorTrigger'
}
const baseConditions = {
any: [{
fact: 'dictionary',
operator: 'startsWithLetter',
value: 'a',
params: {
wordIndex: null
}
}]
}
let eventSpy
function setup (conditions = baseConditions) {
eventSpy = sandbox.spy()
const engine = engineFactory()
const rule = factories.rule({ conditions, event })
engine.addRule(rule)
engine.addOperator('startsWithLetter', (factValue, jsonValue) => {
if (!factValue.length) return false
return factValue[0].toLowerCase() === jsonValue.toLowerCase()
})
engine.addFact('dictionary', dictionary)
engine.on('success', eventSpy)
return engine
}
describe('evaluation', () => {
it('emits when the condition is met', async () => {
const conditions = Object.assign({}, baseConditions)
conditions.any[0].params.wordIndex = 1
const engine = setup()
await engine.run()
expect(eventSpy).to.have.been.calledWith(event)
})
it('does not emit when the condition fails', async () => {
const conditions = Object.assign({}, baseConditions)
conditions.any[0].params.wordIndex = 0
const engine = setup()
await engine.run()
expect(eventSpy).to.not.have.been.calledWith(event)
})
it('throws when it encounters an unregistered operator', async () => {
const conditions = Object.assign({}, baseConditions)
conditions.any[0].operator = 'unknown-operator'
const engine = setup()
return expect(engine.run()).to.eventually.be.rejectedWith('Unknown operator: unknown-operator')
})
})
})