@@ -13,6 +13,10 @@ describe("Engine: event", () => {
1313 canOrderDrinks : true ,
1414 } ,
1515 } ;
16+
17+ const awesomeEvent = {
18+ type : 'awesome'
19+ }
1620 /**
1721 * sets up a simple 'any' rule with 2 conditions
1822 */
@@ -85,6 +89,46 @@ describe("Engine: event", () => {
8589 engine . addFact ( "gender" , "male" ) ; // gender succeeds
8690 }
8791
92+ function setupWithConditionReference ( ) {
93+ const conditionName = 'awesomeCondition'
94+ const conditions = {
95+ any : [ { condition : conditionName } ]
96+ }
97+ engine = engineFactory ( )
98+ const ruleOptions = { conditions, event : awesomeEvent , priority : 100 }
99+ const rule = ruleFactory ( ruleOptions )
100+ engine . addRule ( rule )
101+ engine . setCondition ( conditionName , {
102+ all : [ {
103+ name : 'over 21' ,
104+ fact : 'age' ,
105+ operator : 'greaterThanInclusive' ,
106+ value : 21
107+ } ]
108+ } )
109+ engine . addFact ( 'age' , 21 )
110+ }
111+
112+ function setupWithUndefinedCondition ( ) {
113+ const conditionName = 'conditionThatIsNotDefined'
114+ const conditions = {
115+ any : [
116+ { condition : conditionName , name : 'nameOfTheUndefinedConditionReference' } ,
117+ {
118+ name : 'over 21' ,
119+ fact : 'age' ,
120+ operator : 'greaterThanInclusive' ,
121+ value : 21
122+ }
123+ ]
124+ }
125+ engine = engineFactory ( [ ] , { allowUndefinedConditions : true } )
126+ const ruleOptions = { conditions, event : awesomeEvent , priority : 100 }
127+ const rule = ruleFactory ( ruleOptions )
128+ engine . addRule ( rule )
129+ engine . addFact ( 'age' , 21 )
130+ }
131+
88132 describe ( "engine events: simple" , ( ) => {
89133 beforeEach ( ( ) => simpleSetup ( ) ) ;
90134
@@ -647,4 +691,24 @@ describe("Engine: event", () => {
647691 expect ( JSON . stringify ( ruleResult ) ) . toBe ( expected ) ;
648692 } ) ;
649693 } ) ;
694+
695+ describe ( 'rule events: json serializing with condition reference' , ( ) => {
696+ beforeEach ( ( ) => setupWithConditionReference ( ) )
697+ it ( 'serializes properties' , async ( ) => {
698+ const { results : [ ruleResult ] } = await engine . run ( )
699+ 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}'
700+ expect ( JSON . stringify ( ruleResult ) ) . toEqual ( expected )
701+ } )
702+ } )
703+
704+ describe ( 'rule events: json serializing with condition reference that is undefined' , ( ) => {
705+ beforeEach ( ( ) => setupWithUndefinedCondition ( ) )
706+ it ( 'serializes properties' , async ( ) => {
707+ const { results : [ ruleResult ] } = await engine . run ( )
708+ const { conditions : { any : [ conditionReference ] } } = ruleResult
709+ expect ( conditionReference . result ) . toEqual ( false )
710+ 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}'
711+ expect ( JSON . stringify ( ruleResult ) ) . toEqual ( expected )
712+ } )
713+ } )
650714} ) ;
0 commit comments