-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.ts
232 lines (183 loc) · 6.66 KB
/
index.test.ts
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { QBem } from '../src'
describe('QBem', () => {
it('should be defined', () => {
expect(QBem).toBeDefined()
})
it('should throw if block name is undefined', () => {
try {
// @ts-ignore
const qb = new QBem()
/* fail the test */
expect(true).toBe(false)
} catch (e) {
expect(true).toBe(true)
}
})
it('should have a "block" method', () => {
const nameBlock = 'block'
const qb = new QBem(nameBlock)
expect(qb['block']).toBeDefined()
})
it('should have an "element" method', () => {
const nameBlock = 'block'
const qb = new QBem(nameBlock)
expect(qb['element']).toBeDefined()
})
it('should format sample HTML correctly', () => {
const result = `
<form class="form form--theme-xmas form--simple">
<input class="form__input" type="text" />
<input
class="form__submit form__submit--disabled"
type="submit" />
</form>
`
const qb = new QBem('form')
const template = `
<form class="${qb.block(['theme-xmas', 'simple'])}">
<input class="${qb.element('input')}" type="text" />
<input
class="${qb.element('submit', [{ disabled: true }])}"
type="submit" />
</form>
`
expect(template).toEqual(result)
})
describe('QBem.blockWithModifier', () => {
it('should return a BEM style string using the block name associated with this instance of QBem', () => {
const qb = new QBem('block')
expect(qb.blockWithModifier('active')).toEqual('block--active')
})
})
describe('QBem.elementWithModifier', () => {
it('should return a BEM style string using the block name associated with this instance of QBem', () => {
const qb = new QBem('block')
expect(qb.elementWithModifier('element', 'active')).toEqual(
'block__element--active'
)
})
})
describe('QBem.block', () => {
it('should return the block name if modifiers undefined', () => {
const nameBlock = 'block'
const qb = new QBem(nameBlock)
const classname = qb.block()
expect(classname).toEqual(nameBlock)
})
it('should return the block name and modifiers (in order) if modifiers supplied', () => {
const nameBlock = 'block'
const qb = new QBem(nameBlock)
const modifierActive = 'active'
const modifierDarkMode = 'dark-mode'
const classname = qb.block([modifierActive, modifierDarkMode])
expect(classname).toEqual(
`${nameBlock} ${nameBlock}--${modifierActive} ${nameBlock}--${modifierDarkMode}`
)
})
it('should add correct conditional modifiers (in order) if modifiers supplied', () => {
const nameBlock = 'block'
const qb = new QBem(nameBlock)
const modifierActive = 'active'
const modifierDarkMode = 'dark-mode'
const modifierUltraAwesome = 'ultra-awesome'
const classname = qb.block([
modifierActive,
{
[modifierDarkMode]: true,
},
{
[modifierUltraAwesome]: true,
disabled: false,
},
])
expect(classname).toEqual(
`${nameBlock} ${nameBlock}--${modifierActive} ${nameBlock}--${modifierDarkMode} ${nameBlock}--${modifierUltraAwesome}`
)
})
})
describe('QBem.element', () => {
it('should return the element with block name attached in BEM format if modifiers undefined', () => {
const nameBlock = 'block'
const nameElement = 'element'
const qb = new QBem(nameBlock)
const classname = qb.element(nameElement)
expect(classname).toEqual(`${nameBlock}__${nameElement}`)
})
it('should return the element with block name attached in BEM format with modifiers (in order) if defined', () => {
const nameBlock = 'block'
const nameElement = 'element'
const qb = new QBem(nameBlock)
const modifierActive = 'active'
const modifierDarkMode = 'dark-mode'
const classname = qb.element(nameElement, [
modifierActive,
modifierDarkMode,
])
const element = `${nameBlock}__${nameElement}`
expect(classname).toEqual(
`${element} ${element}--${modifierActive} ${element}--${modifierDarkMode}`
)
})
it('should return the element with block name attached in BEM format with conditional modifiers (in order) if defined', () => {
const nameBlock = 'block'
const nameElement = 'element'
const qb = new QBem(nameBlock)
const modifierActive = 'active'
const modifierDarkMode = 'dark-mode'
const modifierUltraAwesome = 'ultra-awesome'
const classname = qb.element(nameElement, [
{
[modifierActive]: true,
[modifierUltraAwesome]: true,
'other-modifier': undefined,
},
modifierDarkMode,
])
const element = `${nameBlock}__${nameElement}`
expect(classname).toEqual(
`${element} ${element}--${modifierActive} ${element}--${modifierUltraAwesome} ${element}--${modifierDarkMode}`
)
})
})
describe('QBem.elem', () => {
it('should return the element with block name attached in BEM format if modifiers undefined', () => {
const nameBlock = 'block'
const nameElement = 'element'
const qb = new QBem(nameBlock)
const classname = qb.elem(nameElement)
expect(classname).toEqual(`${nameBlock}__${nameElement}`)
})
it('should return the element with block name attached in BEM format with modifiers (in order) if defined', () => {
const nameBlock = 'block'
const nameElement = 'element'
const qb = new QBem(nameBlock)
const modifierActive = 'active'
const modifierDarkMode = 'dark-mode'
const classname = qb.elem(nameElement, [modifierActive, modifierDarkMode])
const element = `${nameBlock}__${nameElement}`
expect(classname).toEqual(
`${element} ${element}--${modifierActive} ${element}--${modifierDarkMode}`
)
})
it('should return the element with block name attached in BEM format with conditional modifiers (in order) if defined', () => {
const nameBlock = 'block'
const nameElement = 'element'
const qb = new QBem(nameBlock)
const modifierActive = 'active'
const modifierDarkMode = 'dark-mode'
const modifierUltraAwesome = 'ultra-awesome'
const classname = qb.elem(nameElement, [
{
[modifierActive]: true,
[modifierUltraAwesome]: true,
'other-modifier': undefined,
},
modifierDarkMode,
])
const element = `${nameBlock}__${nameElement}`
expect(classname).toEqual(
`${element} ${element}--${modifierActive} ${element}--${modifierUltraAwesome} ${element}--${modifierDarkMode}`
)
})
})
})