From c6ff8ddae926f6fe98cc767cb3d6806c1f80a3ba Mon Sep 17 00:00:00 2001 From: lttb Date: Wed, 26 Apr 2017 00:50:55 +0300 Subject: [PATCH 1/2] Fix addRule for functional values, fix #475 --- src/utils/toCss.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/utils/toCss.js b/src/utils/toCss.js index 4c57a57de..6bfed2e5e 100644 --- a/src/utils/toCss.js +++ b/src/utils/toCss.js @@ -47,17 +47,20 @@ export default function toCss(selector: string, style: JssStyle, options: Option } } + let hasFunctionValue = false + for (const prop in style) { let value = style[prop] if (typeof value === 'function') { value = style[`$${prop}`] + hasFunctionValue = true } if (value != null && prop !== 'fallbacks') { result += `\n${indentStr(`${prop}: ${toCssValue(value)};`, indent)}` } } - if (!result) return result + if (!result && !hasFunctionValue) return result indent-- result = indentStr(`${selector} {${result}\n`, indent) + indentStr('}', indent) From b9e3da2ea4cf8e926b6b1556a677d458bf2d0393 Mon Sep 17 00:00:00 2001 From: lttb Date: Wed, 26 Apr 2017 00:51:36 +0300 Subject: [PATCH 2/2] Fix sheet.update() test, add test for .addRule with function value --- tests/functional/sheet.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/tests/functional/sheet.js b/tests/functional/sheet.js index 20fc896ec..4bd4a446b 100644 --- a/tests/functional/sheet.js +++ b/tests/functional/sheet.js @@ -247,6 +247,34 @@ describe('Functional: sheet', () => { }) }) + describe('.addRule() with just function values and attached sheet', () => { + let style + let sheet + + beforeEach(() => { + sheet = jss.createStyleSheet().attach().link() + sheet.addRule('a', {color: ({color}) => color}) + style = getStyle() + }) + + afterEach(() => { + sheet.detach() + }) + + it('should render an empty rule', () => { + expect(getCss(style)).to.be(removeWhitespace(sheet.toString())) + }) + + it('should render rule with updated color', () => { + sheet.update({color: 'red'}) + expect(sheet.toString()).to.be(stripIndent` + .a-id { + color: red; + } + `) + }) + }) + describe('.addRule() with empty styles', () => { let sheet let style @@ -449,7 +477,14 @@ describe('Functional: sheet', () => { }) it('should return correct .toString()', () => { - expect(sheet.toString()).to.be('') + expect(sheet.toString()).to.be(stripIndent` + .a-id { + } + @media all { + .b-id { + } + } + `) sheet.update({ color: 'green'