Skip to content

Make Rule.conditions readonly (fixes #419) #420

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion test/engine.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ describe('Engine', () => {
expect(engine.rules[0].conditions.all.length).to.equal(2)
expect(engine.rules[1].conditions.all.length).to.equal(2)

rule1.conditions = { all: [] }
rule1.setConditions({ all: [] })
engine.updateRule(rule1)

rule1 = engine.rules.find(rule => rule.name === 'rule1')
Expand All @@ -102,6 +102,20 @@ describe('Engine', () => {
expect(rule2.conditions.all.length).to.equal(2)
})

it('updates rule incorrectly', async () => {
const rule1 = new Rule(factories.rule({ name: 'rule1' }))
engine.addRule(rule1)

rule1.conditions = { all: [] } // Rule.conditions should not be set to a value that is not an instance of Condition
engine.updateRule(rule1)

const successSpy = sandbox.spy()
rule1.on('success', successSpy)
await engine.run()
const ruleResult = successSpy.getCall(0).args[2]
expect(() => JSON.stringify(ruleResult)).to.throw(/toJSON is not a function/)
})

it('should throw error if rule not found', () => {
const rule1 = new Rule(factories.rule({ name: 'rule1' }))
engine.addRule(rule1)
Expand Down
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ export interface RuleResult {
export class Rule implements RuleProperties {
constructor(ruleProps: RuleProperties | string);
name: string;
conditions: TopLevelCondition;
readonly conditions: TopLevelCondition;
/**
* @deprecated Use {@link Rule.event} instead.
*/
Expand Down