Skip to content

Commit f636f1a

Browse files
author
Cache Hamm
committed
rule.id allow for "0" id rules
1 parent 8fd8e37 commit f636f1a

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/rule.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ class Rule extends EventEmitter {
3636
this.setName(options.name)
3737
}
3838

39-
this.setId(options ? options.id : null)
39+
if (!options || options.id === undefined) {
40+
// auto generate an id if none was provided
41+
this.setId('_' + Math.random().toString(36).substr(2, 9))
42+
} else {
43+
this.setId(options.id)
44+
}
4045

4146
const priority = (options && options.priority) || 1
4247
this.setPriority(priority)
@@ -61,11 +66,7 @@ class Rule extends EventEmitter {
6166
* @param {any} id - any truthy input
6267
*/
6368
setId (id) {
64-
if (!id) {
65-
this.id = '_' + Math.random().toString(36).substr(2, 9)
66-
} else {
67-
this.id = id
68-
}
69+
this.id = id
6970
return this
7071
}
7172

test/engine.test.js

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,7 @@ describe('Engine', () => {
9393
engine.updateRule(rule)
9494
expect(engine.rules[0].conditions.all.length).to.equal(0)
9595
})
96-
it('should generate id for rule if not provided', () => {
97-
const rule = new Rule(factories.rule())
98-
expect(rule.id).to.not.equal(null)
99-
expect(rule.id).to.not.equal(undefined)
100-
})
96+
10197
it('should throw error if rule not found', () => {
10298
const rule1 = new Rule(factories.rule())
10399
engine.addRule(rule1)

test/rule.test.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ describe('Rule', () => {
5454
expect(rule.ruleEvent).to.eql(opts.event)
5555
expect(rule.name).to.eql(opts.name)
5656
})
57+
58+
context('id', () => {
59+
it('autogenerates an id if none was provided', () => {
60+
const rule = new Rule()
61+
expect(rule.id).to.have.lengthOf(10)
62+
})
63+
64+
it('sets the id if provided', () => {
65+
let rule = new Rule({ id: 'test-id' })
66+
expect(rule.id).to.equal('test-id')
67+
68+
rule = new Rule({ id: 0 })
69+
expect(rule.id).to.equal(0)
70+
71+
rule = new Rule({ id: '' })
72+
expect(rule.id).to.equal('')
73+
74+
rule = new Rule({ id: null })
75+
expect(rule.id).to.equal(null)
76+
})
77+
})
5778
})
5879

5980
describe('event emissions', () => {
@@ -96,6 +117,24 @@ describe('Rule', () => {
96117
})
97118
})
98119

120+
describe('setId()', () => {
121+
it('changes the rule id', () => {
122+
const newId = 'test-id-123'
123+
rule.setId(newId)
124+
expect(rule.id).to.equal(newId)
125+
})
126+
})
127+
128+
describe('setEvent()', () => {
129+
it('throws if no argument provided', () => {
130+
expect(() => rule.setEvent()).to.throw(/Rule: setEvent\(\) requires event object/)
131+
})
132+
133+
it('throws if argument is missing "type" property', () => {
134+
expect(() => rule.setEvent({})).to.throw(/Rule: setEvent\(\) requires event object with "type" property/)
135+
})
136+
})
137+
99138
describe('setConditions()', () => {
100139
describe('validations', () => {
101140
it('throws an exception for invalid root conditions', () => {

0 commit comments

Comments
 (0)