-
-
Notifications
You must be signed in to change notification settings - Fork 389
Expand file tree
/
Copy pathplugins.js
More file actions
449 lines (398 loc) · 10.6 KB
/
plugins.js
File metadata and controls
449 lines (398 loc) · 10.6 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
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import expect from 'expect.js'
import {stripIndent} from 'common-tags'
import jssNested from 'jss-nested'
import {create} from '../../src'
import StyleSheet from '../../src/StyleSheet'
import PluginsRegistry from '../../src/PluginsRegistry'
import {
createGenerateClassName,
getCssFromSheet,
removeWhitespace
} from '../utils'
describe('Integration: plugins', () => {
let jss
beforeEach(() => {
jss = create({createGenerateClassName})
})
describe('common', () => {
it('should not call hooks twice on the same rule', () => {
const styles = {a: {color: 'red'}}
let receivedRule
let processed = 0
jss.use({
onCreateRule: () => receivedRule,
onProcessRule: (rule) => {
receivedRule = rule
processed++
}
})
// Process rules once.
jss.createStyleSheet(styles)
jss.createStyleSheet(styles)
expect(processed).to.be(1)
})
it('should call hooks in the correct order', () => {
jss.use({
onProcessRule: (rule) => {
if (rule.key === 'a') {
rule.options.sheet.addRule('b', {color: 'green'}, {index: 1})
}
}
})
const selectors = []
jss.use({
onProcessRule: (rule) => {
selectors.push(rule.selector)
}
})
const sheet = jss.createStyleSheet({
a: {color: 'red'},
c: {color: 'blue'}
})
expect(sheet.indexOf(sheet.getRule('a'))).to.be(0)
expect(sheet.indexOf(sheet.getRule('b'))).to.be(1)
expect(sheet.indexOf(sheet.getRule('c'))).to.be(2)
expect(sheet.toString()).to.be(
'.a-id {\n' +
' color: red;\n' +
'}\n' +
'.b-id {\n' +
' color: green;\n' +
'}\n' +
'.c-id {\n' +
' color: blue;\n' +
'}'
)
expect(selectors).to.eql(['.b-id', '.a-id', '.c-id'])
})
it('should warn when unknown hook name is used', () => {
let receivedWarning
// eslint-disable-next-line no-underscore-dangle
PluginsRegistry.__Rewire__('warning', (flag, warning) => {
receivedWarning = warning
})
jss.use({
unknownHook: () => null
})
jss.createStyleSheet({a: {color: 'red'}})
expect(receivedWarning).to.be('[JSS] Unknown hook "%s".')
})
})
describe('onProcessRule', () => {
let sheet
let receivedRule
let receivedSheet
let executed = 0
beforeEach(() => {
jss.use({
onProcessRule: (rule, passedSheet) => {
receivedRule = rule
receivedSheet = passedSheet
executed++
}
})
sheet = jss.createStyleSheet({
a: {color: 'red'}
})
})
it('should be executed just once', () => {
expect(executed).to.be(1)
})
it('should pass right arguments', () => {
expect(sheet).to.be(receivedSheet)
expect(sheet.getRule('a')).to.be(receivedRule)
})
})
describe('onCreateRule', () => {
let receivedName
let receivedDecl
let receivedOptions
let executed = 0
beforeEach(() => {
jss.use({
onCreateRule: (name, decl, options) => {
receivedName = name
receivedDecl = decl
receivedOptions = options
executed++
}
})
jss.createStyleSheet({
a: {float: 'left'}
})
})
it('should be executed just once', () => {
expect(executed).to.be(1)
})
it('should pass right arguments', () => {
expect(receivedName).to.be('a')
expect(receivedDecl).to.eql({float: 'left'})
expect(receivedOptions).to.be.an(Object)
})
})
describe('onProcessSheet', () => {
let receivedSheet
let executed = 0
beforeEach(() => {
jss.use({
onProcessSheet: (sheet) => {
receivedSheet = sheet
executed++
}
})
jss.createStyleSheet({
a: {float: 'left'}
})
})
it('should be executed just once', () => {
expect(executed).to.be(1)
})
it('should pass right arguments', () => {
expect(receivedSheet).to.be.a(StyleSheet)
})
})
describe('.createRule() with onProcessRule and onCreateRule', () => {
let receivedRule
let executed = 0
beforeEach(() => {
executed = 0
jss.use({
onProcessRule: (rule) => {
receivedRule = rule
executed++
}
})
})
it('should pass rule correctly', () => {
const rule = jss.createRule()
expect(rule).to.be(receivedRule)
expect(executed).to.be(1)
})
it('should run plugins on @media rule', () => {
const rule = jss.createRule('@media', {
button: {float: 'left'}
})
expect(rule).to.be(receivedRule)
expect(executed).to.be(2)
})
it('should run plugins on @keyframes rule', () => {
const rule = jss.createRule('@keyframes', {
from: {top: 0},
to: {top: 10}
})
expect(rule).to.be(receivedRule)
expect(executed).to.be(3)
})
it('should accept multiple plugins', () => {
let receivedRule1
let receivedRule2
const plugin1 = {
onProcessRule: (rule) => {
receivedRule1 = rule
}
}
const plugin2 = {
onProcessRule: (rule) => {
receivedRule2 = rule
}
}
jss.use(plugin1, plugin2)
const rule = jss.createRule()
expect(receivedRule1).to.be(rule)
expect(receivedRule2).to.be(rule)
})
})
describe('onChangeValue', () => {
let receivedValue
let receivedProp
let receivedRule
let executed = 0
let sheet
beforeEach(() => {
jss.use({
onChangeValue: (value, prop, rule) => {
receivedValue = value
receivedProp = prop
receivedRule = rule
executed++
return value
}
})
sheet = jss.createStyleSheet({
a: {color: 'red'}
})
})
it('should be executed just once', () => {
sheet.getRule('a').prop('color', 'green')
expect(executed).to.be(1)
})
it('should receive correct arguments', () => {
const rule = sheet.getRule('a')
rule.prop('color', 'green')
expect(receivedValue).to.be('green')
expect(receivedProp).to.be('color')
expect(receivedRule).to.be(rule)
})
it('should compile to correct CSS string', () => {
expect(sheet.toString()).to.be(stripIndent`
.a-id {
color: red;
}
`)
sheet.getRule('a').prop('color', 'green')
expect(sheet.toString()).to.be(stripIndent`
.a-id {
color: green;
}
`)
})
it('should pass the new value to the next hook', () => {
jss.use({onChangeValue: value => `${value}-first`})
jss.use({onChangeValue: value => `${value}-second`})
sheet.getRule('a').prop('color', 'green')
expect(sheet.toString()).to.be(stripIndent`
.a-id {
color: green-first-second;
}
`)
})
})
describe('onProcessStyle', () => {
let receivedStyle
let receivedRule
let receivedSheet
let executed = 0
let sheet
const newStyle = {color: 'green'}
beforeEach(() => {
jss.use({
onProcessStyle: (style, rule, passedSheet) => {
receivedStyle = style
receivedRule = rule
receivedSheet = passedSheet
executed++
return newStyle
}
})
sheet = jss.createStyleSheet({
a: {color: 'red'}
})
})
it('should be executed just once', () => {
expect(executed).to.be(1)
})
it('should receive correct arguments', () => {
expect(receivedStyle).to.eql({color: 'red'})
expect(receivedRule).to.be(sheet.getRule('a'))
expect(receivedSheet).to.be(sheet)
})
it('should set the returned style', () => {
expect(sheet.getRule('a').style).to.be(newStyle)
})
it('should compile to correct CSS string', () => {
expect(sheet.toString()).to.be(stripIndent`
.a-id {
color: green;
}
`)
})
it('should not call the hook if rule has no .style', () => {
let localExecuted = 0
jss.use({
onProcessStyle: (style, rule, passedSheet) => {
receivedStyle = style
receivedRule = rule
receivedSheet = passedSheet
localExecuted++
return style
}
})
sheet = jss.createStyleSheet({
'@media all': {
a: {color: 'red'}
}
})
expect(receivedStyle).to.be(newStyle)
expect(receivedRule.type).to.be('style')
expect(receivedSheet).to.be(sheet)
expect(localExecuted).to.be(1)
})
it('should pass the style object to the next hook', () => {
let passedStyle
jss.use({
onProcessStyle: (style) => {
passedStyle = style
return style
}
})
sheet = jss.createStyleSheet({
a: {color: 'red'}
})
expect(sheet.getRule('a').style).to.be(newStyle)
expect(passedStyle).to.be(newStyle)
})
})
describe('onUpdate (internal)', () => {
let receivedData
let receivedRule
let receivedSheet
let executed = 0
let sheet
beforeEach(() => {
jss.use({
onUpdate: (data, rule, styleSheet) => {
receivedData = data
receivedRule = rule
receivedSheet = styleSheet
executed++
}
})
sheet = jss.createStyleSheet({
a: {
color: data => data.color
}
})
})
it('should be executed just once', () => {
sheet.update({})
expect(executed).to.be(1)
})
it('should receive correct arguments', () => {
const data = {color: 'green'}
const rule = sheet.getRule('a')
sheet.update(data)
expect(receivedData).to.be(data)
expect(receivedRule).to.be(rule)
expect(receivedSheet).to.be(sheet)
expect(rule.prop('color')).to.be('green')
})
it('should compile to correct CSS string', () => {
sheet.update({color: 'green'})
expect(sheet.toString()).to.be(stripIndent`
.a-id {
color: green;
}
`)
})
})
describe('jss-nested', () => {
let sheet
beforeEach(() => {
jss.use(jssNested())
sheet = jss.createStyleSheet({}, {
link: true,
}).attach()
sheet.addRule('b', {color: 'green'})
sheet.addRule('a', {
'&:hover': {
'& $b': {
color: 'red',
},
},
})
})
it('should save the added nested rules order', () => {
expect(getCssFromSheet(sheet)).to.be(removeWhitespace(sheet.toString()))
})
})
})