@@ -42,41 +42,90 @@ describe('Engine: fact evaluation', () => {
4242 demographic : 'under50'
4343 }
4444 }
45- let baseConditions = {
46- any : [ {
47- fact : 'eligibilityField' ,
48- operator : 'lessThan' ,
49- params : {
50- eligibilityId : 1 ,
51- field : 'age'
52- } ,
53- value : 50
54- } ]
45+ function baseConditions ( ) {
46+ return {
47+ any : [ {
48+ fact : 'eligibilityField' ,
49+ operator : 'lessThan' ,
50+ params : {
51+ eligibilityId : 1 ,
52+ field : 'age'
53+ } ,
54+ value : 50
55+ } ]
56+ }
5557 }
56- let eventSpy = sinon . spy ( )
57- function setup ( conditions = baseConditions ) {
58- eventSpy . reset ( )
59- engine = engineFactory ( )
58+ let successSpy = sinon . spy ( )
59+ let failureSpy = sinon . spy ( )
60+ function setup ( conditions = baseConditions ( ) , engineOptions = { } ) {
61+ successSpy . reset ( )
62+ failureSpy . reset ( )
63+ engine = engineFactory ( [ ] , engineOptions )
6064 let rule = factories . rule ( { conditions, event } )
6165 engine . addRule ( rule )
6266 engine . addFact ( 'eligibilityField' , eligibilityField )
6367 engine . addFact ( 'eligibilityData' , eligibilityData )
64- engine . on ( 'success' , eventSpy )
68+ engine . on ( 'success' , successSpy )
69+ engine . on ( 'failure' , failureSpy )
6570 }
6671
72+ describe ( 'options' , ( ) => {
73+ describe ( 'options.allowUndefinedFacts' , ( ) => {
74+ it ( 'throws when fact is undefined by default' , async ( ) => {
75+ let conditions = Object . assign ( { } , baseConditions ( ) )
76+ conditions . any . push ( {
77+ fact : 'undefined-fact' ,
78+ operator : 'equal' ,
79+ value : true
80+ } )
81+ setup ( conditions )
82+ return expect ( engine . run ( ) ) . to . be . rejectedWith ( / U n d e f i n e d f a c t : u n d e f i n e d - f a c t / )
83+ } )
84+
85+ context ( 'treats undefined facts as falsey when allowUndefinedFacts is set' , ( ) => {
86+ it ( 'emits "success" when the condition succeeds' , async ( ) => {
87+ let conditions = Object . assign ( { } , baseConditions ( ) )
88+ conditions . any . push ( {
89+ fact : 'undefined-fact' ,
90+ operator : 'equal' ,
91+ value : true
92+ } )
93+ setup ( conditions , { allowUndefinedFacts : true } )
94+ await engine . run ( )
95+ expect ( successSpy ) . to . have . been . called
96+ expect ( failureSpy ) . to . not . have . been . called
97+ } )
98+
99+ it ( 'emits "failure" when the condition fails' , async ( ) => {
100+ let conditions = Object . assign ( { } , baseConditions ( ) )
101+ conditions . any . push ( {
102+ fact : 'undefined-fact' ,
103+ operator : 'equal' ,
104+ value : true
105+ } )
106+ conditions . any [ 0 ] . params . eligibilityId = 2
107+ setup ( conditions , { allowUndefinedFacts : true } )
108+ await engine . run ( )
109+ expect ( successSpy ) . to . not . have . been . called
110+ expect ( failureSpy ) . to . have . been . called
111+ } )
112+ } )
113+ } )
114+ } )
115+
67116 describe ( 'params' , ( ) => {
68117 it ( 'emits when the condition is met' , async ( ) => {
69118 setup ( )
70119 await engine . run ( )
71- expect ( eventSpy ) . to . have . been . calledWith ( event )
120+ expect ( successSpy ) . to . have . been . calledWith ( event )
72121 } )
73122
74123 it ( 'does not emit when the condition fails' , async ( ) => {
75- let conditions = Object . assign ( { } , baseConditions )
124+ let conditions = Object . assign ( { } , baseConditions ( ) )
76125 conditions . any [ 0 ] . params . eligibilityId = 2
77126 setup ( conditions )
78127 await engine . run ( )
79- expect ( eventSpy ) . to . not . have . been . called
128+ expect ( successSpy ) . to . not . have . been . called
80129 } )
81130 } )
82131
@@ -97,15 +146,15 @@ describe('Engine: fact evaluation', () => {
97146 it ( 'emits when the condition is met' , async ( ) => {
98147 setup ( conditions ( ) )
99148 await engine . run ( )
100- expect ( eventSpy ) . to . have . been . calledWith ( event )
149+ expect ( successSpy ) . to . have . been . calledWith ( event )
101150 } )
102151
103152 it ( 'does not emit when the condition fails' , async ( ) => {
104153 let failureCondition = conditions ( )
105154 failureCondition . any [ 0 ] . params . eligibilityId = 2
106155 setup ( failureCondition )
107156 await engine . run ( )
108- expect ( eventSpy ) . to . not . have . been . called
157+ expect ( successSpy ) . to . not . have . been . called
109158 } )
110159
111160 it ( 'emits when complex object paths meet the conditions' , async ( ) => {
@@ -115,7 +164,7 @@ describe('Engine: fact evaluation', () => {
115164 complexCondition . any [ 0 ] . operator = 'equal'
116165 setup ( complexCondition )
117166 await engine . run ( )
118- expect ( eventSpy ) . to . have . been . calledWith ( event )
167+ expect ( successSpy ) . to . have . been . calledWith ( event )
119168 } )
120169
121170 it ( 'does not emit when complex object paths fail the condition' , async ( ) => {
@@ -125,7 +174,7 @@ describe('Engine: fact evaluation', () => {
125174 complexCondition . any [ 0 ] . operator = 'equal'
126175 setup ( complexCondition )
127176 await engine . run ( )
128- expect ( eventSpy ) . to . not . have . been . calledWith ( event )
177+ expect ( successSpy ) . to . not . have . been . calledWith ( event )
129178 } )
130179
131180 it ( 'treats invalid object paths as undefined' , async ( ) => {
@@ -135,7 +184,7 @@ describe('Engine: fact evaluation', () => {
135184 complexCondition . any [ 0 ] . operator = 'equal'
136185 setup ( complexCondition )
137186 await engine . run ( )
138- expect ( eventSpy ) . to . have . been . calledWith ( event )
187+ expect ( successSpy ) . to . have . been . calledWith ( event )
139188 } )
140189
141190 it ( 'ignores "path" when facts return non-objects' , async ( ) => {
@@ -145,7 +194,7 @@ describe('Engine: fact evaluation', () => {
145194 }
146195 engine . addFact ( 'eligibilityData' , eligibilityData )
147196 await engine . run ( )
148- expect ( eventSpy ) . to . have . been . calledWith ( event )
197+ expect ( successSpy ) . to . have . been . calledWith ( event )
149198 } )
150199 } )
151200
@@ -161,7 +210,7 @@ describe('Engine: fact evaluation', () => {
161210 }
162211 engine . addFact ( 'eligibilityField' , eligibilityField )
163212 await engine . run ( )
164- expect ( eventSpy ) . to . have . been . called
213+ expect ( successSpy ) . to . have . been . called
165214 } )
166215 } )
167216
@@ -173,7 +222,7 @@ describe('Engine: fact evaluation', () => {
173222 }
174223 engine . addFact ( 'eligibilityField' , eligibilityField )
175224 await engine . run ( )
176- expect ( eventSpy ) . to . have . been . called
225+ expect ( successSpy ) . to . have . been . called
177226 } )
178227
179228 it ( 'works with synchronous, non-promise evaluations that are falsey' , async ( ) => {
@@ -183,7 +232,7 @@ describe('Engine: fact evaluation', () => {
183232 }
184233 engine . addFact ( 'eligibilityField' , eligibilityField )
185234 await engine . run ( )
186- expect ( eventSpy ) . to . not . have . been . called
235+ expect ( successSpy ) . to . not . have . been . called
187236 } )
188237 } )
189238} )
0 commit comments