@@ -20,6 +20,10 @@ describe('Engine: event', () => {
20
20
canOrderDrinks : true
21
21
}
22
22
}
23
+
24
+ const awesomeEvent = {
25
+ type : 'awesome'
26
+ }
23
27
/**
24
28
* sets up a simple 'any' rule with 2 conditions
25
29
*/
@@ -92,6 +96,46 @@ describe('Engine: event', () => {
92
96
engine . addFact ( 'gender' , 'male' ) // gender succeeds
93
97
}
94
98
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
+
95
139
context ( 'engine events: simple' , ( ) => {
96
140
beforeEach ( ( ) => simpleSetup ( ) )
97
141
@@ -602,4 +646,24 @@ describe('Engine: event', () => {
602
646
expect ( JSON . stringify ( ruleResult ) ) . to . equal ( expected )
603
647
} )
604
648
} )
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
+ } )
605
669
} )
0 commit comments