@@ -20,6 +20,10 @@ describe('Engine: event', () => {
2020 canOrderDrinks : true
2121 }
2222 }
23+
24+ const awesomeEvent = {
25+ type : 'awesome'
26+ }
2327 /**
2428 * sets up a simple 'any' rule with 2 conditions
2529 */
@@ -92,6 +96,46 @@ describe('Engine: event', () => {
9296 engine . addFact ( 'gender' , 'male' ) // gender succeeds
9397 }
9498
99+ function setupWithConditionReference ( ) {
100+ const conditionName = 'awesomeCondition'
101+ const conditions = {
102+ any : [ { condition : conditionName } ]
103+ }
104+ engine = engineFactory ( )
105+ const ruleOptions = { conditions, event : awesomeEvent , priority : 100 }
106+ const rule = factories . rule ( ruleOptions )
107+ engine . addRule ( rule )
108+ engine . setCondition ( conditionName , {
109+ all : [ {
110+ name : 'over 21' ,
111+ fact : 'age' ,
112+ operator : 'greaterThanInclusive' ,
113+ value : 21
114+ } ]
115+ } )
116+ engine . addFact ( 'age' , 21 )
117+ }
118+
119+ function setupWithUndefinedCondition ( ) {
120+ const conditionName = 'conditionThatIsNotDefined'
121+ const conditions = {
122+ any : [
123+ { condition : conditionName , name : 'nameOfTheUndefinedConditionReference' } ,
124+ {
125+ name : 'over 21' ,
126+ fact : 'age' ,
127+ operator : 'greaterThanInclusive' ,
128+ value : 21
129+ }
130+ ]
131+ }
132+ engine = engineFactory ( [ ] , { allowUndefinedConditions : true } )
133+ const ruleOptions = { conditions, event : awesomeEvent , priority : 100 }
134+ const rule = factories . rule ( ruleOptions )
135+ engine . addRule ( rule )
136+ engine . addFact ( 'age' , 21 )
137+ }
138+
95139 context ( 'engine events: simple' , ( ) => {
96140 beforeEach ( ( ) => simpleSetup ( ) )
97141
@@ -602,4 +646,24 @@ describe('Engine: event', () => {
602646 expect ( JSON . stringify ( ruleResult ) ) . to . equal ( expected )
603647 } )
604648 } )
649+
650+ context ( 'rule events: json serializing with condition reference' , ( ) => {
651+ beforeEach ( ( ) => setupWithConditionReference ( ) )
652+ it ( 'serializes properties' , async ( ) => {
653+ const { results : [ ruleResult ] } = await engine . run ( )
654+ const expected = '{"conditions":{"priority":1,"any":[{"priority":1,"all":[{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true}]}]},"event":{"type":"awesome"},"priority":100,"result":true}'
655+ expect ( JSON . stringify ( ruleResult ) ) . to . equal ( expected )
656+ } )
657+ } )
658+
659+ context ( 'rule events: json serializing with condition reference that is undefined' , ( ) => {
660+ beforeEach ( ( ) => setupWithUndefinedCondition ( ) )
661+ it ( 'serializes properties' , async ( ) => {
662+ const { results : [ ruleResult ] } = await engine . run ( )
663+ const { conditions : { any : [ conditionReference ] } } = ruleResult
664+ expect ( conditionReference . result ) . to . equal ( false )
665+ const expected = '{"conditions":{"priority":1,"any":[{"name":"nameOfTheUndefinedConditionReference","condition":"conditionThatIsNotDefined"},{"name":"over 21","operator":"greaterThanInclusive","value":21,"fact":"age","factResult":21,"result":true}]},"event":{"type":"awesome"},"priority":100,"result":true}'
666+ expect ( JSON . stringify ( ruleResult ) ) . to . equal ( expected )
667+ } )
668+ } )
605669} )
0 commit comments