Skip to content

Commit acc9410

Browse files
authored
Merge pull request #477 from lttb/feature/fix-add-rule-for-fn
Fix addRule for functional values, fix #475
2 parents 56cfb58 + b9e3da2 commit acc9410

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

src/utils/toCss.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,20 @@ export default function toCss(selector: string, style: JssStyle, options: Option
4747
}
4848
}
4949

50+
let hasFunctionValue = false
51+
5052
for (const prop in style) {
5153
let value = style[prop]
5254
if (typeof value === 'function') {
5355
value = style[`$${prop}`]
56+
hasFunctionValue = true
5457
}
5558
if (value != null && prop !== 'fallbacks') {
5659
result += `\n${indentStr(`${prop}: ${toCssValue(value)};`, indent)}`
5760
}
5861
}
5962

60-
if (!result) return result
63+
if (!result && !hasFunctionValue) return result
6164

6265
indent--
6366
result = indentStr(`${selector} {${result}\n`, indent) + indentStr('}', indent)

tests/functional/sheet.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,34 @@ describe('Functional: sheet', () => {
247247
})
248248
})
249249

250+
describe('.addRule() with just function values and attached sheet', () => {
251+
let style
252+
let sheet
253+
254+
beforeEach(() => {
255+
sheet = jss.createStyleSheet().attach().link()
256+
sheet.addRule('a', {color: ({color}) => color})
257+
style = getStyle()
258+
})
259+
260+
afterEach(() => {
261+
sheet.detach()
262+
})
263+
264+
it('should render an empty rule', () => {
265+
expect(getCss(style)).to.be(removeWhitespace(sheet.toString()))
266+
})
267+
268+
it('should render rule with updated color', () => {
269+
sheet.update({color: 'red'})
270+
expect(sheet.toString()).to.be(stripIndent`
271+
.a-id {
272+
color: red;
273+
}
274+
`)
275+
})
276+
})
277+
250278
describe('.addRule() with empty styles', () => {
251279
let sheet
252280
let style
@@ -449,7 +477,14 @@ describe('Functional: sheet', () => {
449477
})
450478

451479
it('should return correct .toString()', () => {
452-
expect(sheet.toString()).to.be('')
480+
expect(sheet.toString()).to.be(stripIndent`
481+
.a-id {
482+
}
483+
@media all {
484+
.b-id {
485+
}
486+
}
487+
`)
453488

454489
sheet.update({
455490
color: 'green'

0 commit comments

Comments
 (0)