@@ -4,7 +4,7 @@ import engineFactory from '../src/index'
44import Almanac from '../src/almanac'
55import sinon from 'sinon'
66
7- describe ( 'Engine: event' , ( ) => {
7+ describe . only ( 'Engine: event' , ( ) => {
88 let engine
99
1010 let event = {
@@ -13,18 +13,21 @@ describe('Engine: event', () => {
1313 canOrderDrinks : true
1414 }
1515 }
16- let conditions = {
17- any : [ {
18- fact : 'age' ,
19- operator : 'greaterThanInclusive' ,
20- value : 21
21- } , {
22- fact : 'qualified' ,
23- operator : 'equal' ,
24- value : true
25- } ]
26- }
27- beforeEach ( ( ) => {
16+ /**
17+ * sets up a simple 'any' rule with 2 conditions
18+ */
19+ function simpleSetup ( ) {
20+ let conditions = {
21+ any : [ {
22+ fact : 'age' ,
23+ operator : 'greaterThanInclusive' ,
24+ value : 21
25+ } , {
26+ fact : 'qualified' ,
27+ operator : 'equal' ,
28+ value : true
29+ } ]
30+ }
2831 engine = engineFactory ( )
2932 let ruleOptions = { conditions, event, priority : 100 }
3033 let determineDrinkingAgeRule = factories . rule ( ruleOptions )
@@ -33,10 +36,48 @@ describe('Engine: event', () => {
3336 engine . addFact ( 'age' , 21 )
3437 // set 'qualified' to fail. rule will succeed because of 'any'
3538 engine . addFact ( 'qualified' , false )
36- } )
39+ }
40+
41+ /**
42+ * sets up a complex rule with nested conditions
43+ */
44+ function advancedSetup ( ) {
45+ let conditions = {
46+ any : [ {
47+ fact : 'age' ,
48+ operator : 'greaterThanInclusive' ,
49+ value : 21
50+ } , {
51+ fact : 'qualified' ,
52+ operator : 'equal' ,
53+ value : true
54+ } , {
55+ all : [ {
56+ fact : 'zipCode' ,
57+ operator : 'in' ,
58+ value : [ 80211 , 80403 ]
59+ } , {
60+ fact : 'gender' ,
61+ operator : 'notEqual' ,
62+ value : 'female'
63+ } ]
64+ } ]
65+ }
66+ engine = engineFactory ( )
67+ let ruleOptions = { conditions, event, priority : 100 }
68+ let determineDrinkingAgeRule = factories . rule ( ruleOptions )
69+ engine . addRule ( determineDrinkingAgeRule )
70+ engine . addFact ( 'age' , 10 ) // age fails
71+ engine . addFact ( 'qualified' , false ) // qualified fails.
72+ engine . addFact ( 'zipCode' , 80403 ) // zipCode succeeds
73+ engine . addFact ( 'gender' , 'male' ) // gender succeeds
74+ // rule will succeed because of 'any'
75+ }
76+
77+ context ( 'engine events: simple' , ( ) => {
78+ beforeEach ( ( ) => simpleSetup ( ) )
3779
38- context ( 'engine events' , ( ) => {
39- it ( 'passes the event type and params' , async ( ) => {
80+ it ( '"success" passes the event, almanac, and results' , async ( ) => {
4081 let failureSpy = sinon . spy ( )
4182 let successSpy = sinon . spy ( )
4283 engine . on ( 'success' , function ( e , almanac , ruleResult ) {
@@ -53,15 +94,22 @@ describe('Engine: event', () => {
5394 expect ( successSpy . callCount ) . to . equal ( 1 )
5495 } )
5596
56- it ( 'emits using the event "type"' , ( done ) => {
57- engine . on ( 'setDrinkingFlag' , function ( params , e ) {
58- try {
59- expect ( params ) . to . eql ( event . params )
60- expect ( engine ) . to . eql ( e )
61- } catch ( e ) { return done ( e ) }
62- done ( )
97+ it ( '"failure" passes the event, almanac, and results' , async ( ) => {
98+ let failureSpy = sinon . spy ( )
99+ let successSpy = sinon . spy ( )
100+ engine . on ( 'failure' , function ( e , almanac , ruleResult ) {
101+ expect ( e ) . to . eql ( event )
102+ expect ( almanac ) . to . be . an . instanceof ( Almanac )
103+ expect ( ruleResult . result ) . to . be . false ( )
104+ expect ( ruleResult . conditions . any [ 0 ] . result ) . to . be . false ( )
105+ expect ( ruleResult . conditions . any [ 1 ] . result ) . to . be . false ( )
106+ failureSpy ( )
63107 } )
64- engine . run ( )
108+ engine . on ( 'success' , successSpy )
109+ engine . addFact ( 'age' , 10 ) // age fails
110+ await engine . run ( )
111+ expect ( failureSpy . callCount ) . to . equal ( 1 )
112+ expect ( successSpy . callCount ) . to . equal ( 0 )
65113 } )
66114
67115 it ( 'allows facts to be added by the event handler, affecting subsequent rules' , ( ) => {
@@ -101,7 +149,54 @@ describe('Engine: event', () => {
101149 } )
102150 } )
103151
104- context ( 'rule events' , ( ) => {
152+ context ( 'engine events: advanced' , ( ) => {
153+ beforeEach ( ( ) => advancedSetup ( ) )
154+
155+ it ( '"success" passes the event, almanac, and results' , async ( ) => {
156+ let failureSpy = sinon . spy ( )
157+ let successSpy = sinon . spy ( )
158+ engine . on ( 'success' , function ( e , almanac , ruleResult ) {
159+ expect ( e ) . to . eql ( event )
160+ expect ( almanac ) . to . be . an . instanceof ( Almanac )
161+ expect ( ruleResult . result ) . to . be . true ( )
162+ expect ( ruleResult . conditions . any [ 0 ] . result ) . to . be . false ( )
163+ expect ( ruleResult . conditions . any [ 1 ] . result ) . to . be . false ( )
164+ expect ( ruleResult . conditions . any [ 2 ] . result ) . to . be . true ( )
165+ expect ( ruleResult . conditions . any [ 2 ] . all [ 0 ] . result ) . to . be . true ( )
166+ expect ( ruleResult . conditions . any [ 2 ] . all [ 1 ] . result ) . to . be . true ( )
167+ successSpy ( )
168+ } )
169+ engine . on ( 'failure' , failureSpy )
170+ await engine . run ( )
171+ expect ( failureSpy . callCount ) . to . equal ( 0 )
172+ expect ( successSpy . callCount ) . to . equal ( 1 )
173+ } )
174+
175+ it ( '"failure" passes the event, almanac, and results' , async ( ) => {
176+ let failureSpy = sinon . spy ( )
177+ let successSpy = sinon . spy ( )
178+ engine . on ( 'failure' , function ( e , almanac , ruleResult ) {
179+ expect ( e ) . to . eql ( event )
180+ expect ( almanac ) . to . be . an . instanceof ( Almanac )
181+ expect ( ruleResult . result ) . to . be . false ( )
182+ expect ( ruleResult . conditions . any [ 0 ] . result ) . to . be . false ( )
183+ expect ( ruleResult . conditions . any [ 1 ] . result ) . to . be . false ( )
184+ expect ( ruleResult . conditions . any [ 2 ] . result ) . to . be . false ( )
185+ expect ( ruleResult . conditions . any [ 2 ] . all [ 0 ] . result ) . to . be . false ( )
186+ expect ( ruleResult . conditions . any [ 2 ] . all [ 1 ] . result ) . to . be . false ( )
187+ failureSpy ( )
188+ } )
189+ engine . on ( 'success' , successSpy )
190+ engine . addFact ( 'zipCode' , 99992 ) // zipCode fails
191+ engine . addFact ( 'gender' , 'female' ) // gender fails
192+ await engine . run ( )
193+ expect ( failureSpy . callCount ) . to . equal ( 1 )
194+ expect ( successSpy . callCount ) . to . equal ( 0 )
195+ } )
196+ } )
197+
198+ context ( 'rule events: simple' , ( ) => {
199+ beforeEach ( ( ) => simpleSetup ( ) )
105200 it ( 'on-success, it passes the event type and params' , async ( ) => {
106201 let failureSpy = sinon . spy ( )
107202 let successSpy = sinon . spy ( )
@@ -118,6 +213,7 @@ describe('Engine: event', () => {
118213 rule . on ( 'failure' , failureSpy )
119214 await engine . run ( )
120215 expect ( successSpy . callCount ) . to . equal ( 1 )
216+ expect ( failureSpy . callCount ) . to . equal ( 0 )
121217 } )
122218
123219 it ( 'on-failure, it passes the event type and params' , async ( ) => {
@@ -138,6 +234,7 @@ describe('Engine: event', () => {
138234 engine . addFact ( 'age' , 10 )
139235 await engine . run ( )
140236 expect ( failureSpy . callCount ) . to . equal ( 1 )
237+ expect ( successSpy . callCount ) . to . equal ( 0 )
141238 } )
142239 } )
143240} )
0 commit comments