Skip to content

Commit 725fb66

Browse files
condition reference can be part of the result
1 parent cb5f3c7 commit 725fb66

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

test/engine-event.test.js

+31
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,26 @@ describe('Engine: event', () => {
116116
engine.addFact('age', 21)
117117
}
118118

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+
119139
context('engine events: simple', () => {
120140
beforeEach(() => simpleSetup())
121141

@@ -635,4 +655,15 @@ describe('Engine: event', () => {
635655
expect(JSON.stringify(ruleResult)).to.equal(expected)
636656
})
637657
})
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+
})
638669
})

types/index.d.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ export type RuleSerializable = Pick<
155155

156156
export type RuleResultSerializable = Pick<
157157
Required<RuleResult>,
158-
"name" | "conditions" | "event" | "priority" | "result">
158+
"name" | "event" | "priority" | "result"> & {
159+
conditions: TopLevelConditionResultSerializable
160+
}
159161

160162
export interface RuleResult {
161163
name: string;
@@ -229,6 +231,7 @@ type ConditionReference = {
229231
name?: string;
230232
priority?: number;
231233
};
234+
type ConditionReferenceResult = ConditionReference & BooleanConditionResultProperties
232235
export type TopLevelCondition =
233236
| AllConditions
234237
| AnyConditions
@@ -238,3 +241,9 @@ export type TopLevelConditionResult =
238241
| AllConditionsResult
239242
| AnyConditionsResult
240243
| NotConditionsResult
244+
| ConditionReferenceResult
245+
export type TopLevelConditionResultSerializable =
246+
| AllConditionsResult
247+
| AnyConditionsResult
248+
| NotConditionsResult
249+
| ConditionReference

0 commit comments

Comments
 (0)