This repository was archived by the owner on Nov 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtests.js
More file actions
194 lines (159 loc) · 7.06 KB
/
tests.js
File metadata and controls
194 lines (159 loc) · 7.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*global it, describe, expect */
var BEMHelper = require('./index');
function resultWithClassName(className) {
return {
className: className
};
}
describe('react-bem-helper', function() {
var bemhelper = new BEMHelper('block');
it('should return className for the block when no arguments given', function() {
var bemhelperWithoutPrefix = new BEMHelper({
name: 'block',
prefix: ''
});
expect(bemhelper('')).toEqual(resultWithClassName('block'));
expect(bemhelper()).toEqual(resultWithClassName('block'));
});
it('should return classNames for the block and modifier when modifier given', function() {
expect(bemhelper('', 'funny')).toEqual(resultWithClassName('block block--funny'));
});
it('should return className for the element when element is given', function() {
expect(bemhelper('element')).toEqual(resultWithClassName('block__element'));
});
it('should return classNames for the element and the modifier when a modifier is given', function() {
expect(bemhelper('element', 'modifier')).toEqual(resultWithClassName('block__element block__element--modifier'));
});
describe('when using object as arguments', function() {
it ('should return className for the block when empty object given', function() {
expect(bemhelper({})).toEqual(resultWithClassName('block'));
});
it('should return className for the element when element is given', function() {
expect(bemhelper({
element: 'element'
})).toEqual(resultWithClassName('block__element'));
});
it('should return classNames for the block and modifier when modifier given', function() {
expect(bemhelper({
modifier: 'modifier'
})).toEqual(resultWithClassName('block block--modifier'));
});
it('should return classNames for the block and modifier when modifier given as object', function() {
expect(bemhelper({
modifiers: { 'modifier': true }
})).toEqual(resultWithClassName('block block--modifier'));
});
it('should return classNames for the element and the modifier when a modifier is given', function() {
expect(bemhelper({
element: 'element',
modifier: 'modifier'
})).toEqual(resultWithClassName('block__element block__element--modifier'));
});
it('should return classNames for the element and the modifier when a modifier is given', function() {
expect(bemhelper({
element: 'element',
modifier: 'modifier'
})).toEqual(resultWithClassName('block__element block__element--modifier'));
});
});
describe('when given multiple modifiers', function() {
it('as an array, should return classNames for the element and each modifier given', function() {
var modifiers = ['one', 'two three'];
expect(bemhelper('', modifiers)).toEqual(resultWithClassName('block block--one block--two block--three'));
});
it('as an array containing falsy values, should ignore them', function() {
var modifiers = ['one', false, 0, null, '', undefined];
expect(bemhelper('', modifiers)).toEqual(resultWithClassName('block block--one'));
})
it('as an object, should return classNames for the element and each modifier that is truthy', function() {
var modifiers = {
'one': false,
'two': true,
'three': false,
'four': function() { return false; },
'five': function() { return true; },
'six seven': true,
'': true
};
var result = 'block block--two block--five block--six block--seven';
expect(bemhelper('', modifiers)).toEqual(resultWithClassName(result));
expect(bemhelper(null, modifiers)).toEqual(resultWithClassName(result));
expect(bemhelper({ modifiers: modifiers })).toEqual(resultWithClassName(result));
});
});
it('should append extra classNames when given as an array', function() {
var extraClasses = ['one', 'two'];
expect(bemhelper('', null, extraClasses)).toEqual(resultWithClassName('block one two'));
expect(bemhelper('element', '', extraClasses)).toEqual(resultWithClassName('block__element one two'));
expect(bemhelper({ extra: extraClasses })).toEqual(resultWithClassName('block one two'));
});
it('should ignore falsy extra classNames when part of an array', function() {
var extraClasses = [0, null, false, undefined, ''];
expect(bemhelper('', '', extraClasses)).toEqual(resultWithClassName('block'));
})
it('should append extra classNames for truthy values when given as an object', function() {
var extraClasses = {
'one': false,
'two': true,
'three': false,
'four five': true
};
expect(bemhelper('', '', extraClasses)).toEqual(resultWithClassName('block two four five'));
expect(bemhelper({ extra: extraClasses })).toEqual(resultWithClassName('block two four five'));
});
it('when given a prefix, should append generated BEM classes with that', function() {
var prefixedBEM = new BEMHelper({
name: 'block',
prefix: 'mh-'
});
expect(prefixedBEM('')).toEqual(resultWithClassName('mh-block'));
expect(prefixedBEM('element')).toEqual(resultWithClassName('mh-block__element'));
expect(prefixedBEM('', 'modifier')).toEqual(resultWithClassName('mh-block mh-block--modifier'));
expect(prefixedBEM('', '', 'class')).toEqual(resultWithClassName('mh-block class'));
});
it('when modifierDelimiter option is set, should prefix modifier with that', function() {
var modifierBem = new BEMHelper({
name: 'block',
modifierDelimiter: '_'
});
expect(modifierBem('', 'modifier')).toEqual(resultWithClassName('block block_modifier'));
var modifierBem = new BEMHelper({
name: 'block',
modifierDelimiter: '🐘'
});
expect(modifierBem('', 'modifier')).toEqual(resultWithClassName('block block🐘modifier'));
});
it('should return a string instead of an object when outputIsString is set true', function() {
var stringBem = new BEMHelper({
name: 'block',
outputIsString: true
});
expect(stringBem('')).toBe('block');
expect(stringBem('element')).toBe('block__element');
expect(stringBem('element', 'modifier')).toBe('block__element block__element--modifier');
});
describe('when using withDefaults', function() {
var HelperWithDefaults = BEMHelper.withDefaults({
prefix: 'pfx-',
modifierDelimiter: '_',
outputIsString: true
});
it('should apply the defaults', function() {
var bem1 = new HelperWithDefaults('block')
var bem2 = new HelperWithDefaults({
name: 'block'
});
expect(bem1('element', 'modifier')).toBe('pfx-block__element pfx-block__element_modifier');
expect(bem2('element', 'modifier')).toBe('pfx-block__element pfx-block__element_modifier');
});
it('should be able to override the defaults', function() {
var bem = new HelperWithDefaults({
name: 'block',
prefix: '',
modifierDelimiter: '@',
outputIsString: false
});
expect(bem('element', 'modifier')).toEqual(resultWithClassName('block__element block__element@modifier'));
});
});
});