Skip to content

Commit

Permalink
Merge pull request #477 from lttb/feature/fix-add-rule-for-fn
Browse files Browse the repository at this point in the history
Fix addRule for functional values, fix #475
  • Loading branch information
kof committed Apr 26, 2017
2 parents 56cfb58 + b9e3da2 commit acc9410
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/utils/toCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
37 changes: 36 additions & 1 deletion tests/functional/sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit acc9410

Please sign in to comment.