Skip to content
This repository was archived by the owner on Feb 4, 2018. It is now read-only.

Commit e3a48e7

Browse files
authored
Merge pull request #61 from bem-sdk/issue-59
fix(valid): should throw if modifier is not valid
2 parents 608f588 + a8556b7 commit e3a48e7

File tree

3 files changed

+38
-19
lines changed

3 files changed

+38
-19
lines changed

index.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,20 @@ module.exports = class BemEntityName {
3232
}
3333

3434
const data = this._data = { block: obj.block };
35-
const modName = (typeof obj.mod === 'string' ? obj.mod : obj.mod && obj.mod.name) || obj.modName;
3635

3736
obj.elem && (data.elem = obj.elem);
3837

39-
if (modName) {
40-
const modVal = obj.hasOwnProperty('modVal') || obj.mod && obj.mod.hasOwnProperty('val')
41-
? obj.mod && obj.mod.val || obj.modVal
42-
: true;
38+
const modObj = obj.mod;
39+
const modName = (typeof modObj === 'string' ? modObj : modObj && modObj.name) || obj.modName;
40+
const hasModVal = modObj && modObj.hasOwnProperty('val') || obj.hasOwnProperty('modVal');
4341

42+
if (modName) {
4443
data.mod = {
4544
name: modName,
46-
val: modVal
45+
val: hasModVal ? modObj && modObj.val || obj.modVal : true
4746
};
47+
} else if (modObj || hasModVal) {
48+
throw new Error('This is not valid BEM entity: the field `mod.name` is undefined.');
4849
}
4950
}
5051

test/throws.test.js

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/valid.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const test = require('ava');
2+
3+
const BemEntityName = require('../index');
4+
5+
test('should throw error for if entity object is not valid', t => {
6+
t.throws(
7+
() => new BemEntityName({ elem: 'elem' }),
8+
'This is not valid BEM entity: the field `block` is undefined.'
9+
);
10+
});
11+
12+
test('should throw error for if mod object is empty', t => {
13+
t.throws(
14+
() => new BemEntityName({ block: 'block', mod: {} }),
15+
'This is not valid BEM entity: the field `mod.name` is undefined.'
16+
);
17+
});
18+
19+
test('should throw error for if mod name is undefined', t => {
20+
t.throws(
21+
() => new BemEntityName({ block: 'block', mod: { val: 'val' } }),
22+
'This is not valid BEM entity: the field `mod.name` is undefined.'
23+
);
24+
});
25+
26+
test('should throw error for if modName is undefined', t => {
27+
t.throws(
28+
() => new BemEntityName({ block: 'block', modVal: 'val' }),
29+
'This is not valid BEM entity: the field `mod.name` is undefined.'
30+
);
31+
});

0 commit comments

Comments
 (0)