Skip to content

Commit

Permalink
0.8.4 fixed helper default args
Browse files Browse the repository at this point in the history
- added tests for helpers
  • Loading branch information
fuzetsu committed Dec 5, 2019
1 parent f067431 commit 260c530
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 9 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# Changelog

## 0.8.4

_2019-12-05_

Fixed bug with function helpers where empty string would be passed as first arg when no args were passed, causing default args not to work:

```js
z.helper({ test: (x = 5) => `margin ${x}` })

z`test` // this would result in no style being applied, since x would be '' instead of defaulting to 5
```

## 0.8.3

_2019-11-12_
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zaftig",
"version": "0.8.3",
"version": "0.8.4",
"description": "css for your js",
"source": "src/index.js",
"main": "dist/zaftig.es5.min.js",
Expand Down Expand Up @@ -28,6 +28,6 @@
"buble": "^0.19.8",
"jsdom": "^15.2.1",
"ospec": "^4.0.1",
"terser": "^4.4.0"
"terser": "^4.4.2"
}
}
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const makeZ = (conf = {}) => {
const runHelper = (key, value) => {
const helper = helpers[key]
return typeof helper === 'function'
? helper(...(value || '').split(' '))
? helper(...(value ? value.split(' ') : []))
: helper && helper + ' ' + value
}

Expand Down
50 changes: 48 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const singleRule = (input, output, z = zaf.new({ id: 'test' })) => {
o(z.getSheet().sheet.cssRules[0].cssText).equals('.test-1 {' + output + '}')
}

const fullSheet = str => {
const z = zaf.new({ id: 'test', debug: true })
const fullSheet = (str, conf) => {
const z = zaf.new({ ...conf, id: 'test', debug: true })
const [input, output] = str.split('===')
const style = z(input)
o('' + style).equals('.test-1')
Expand Down Expand Up @@ -165,5 +165,51 @@ h 200
o(z.getSheet().sheet.cssRules.length).equals(2)
})
o('flex does not generate px', () => o(zaf.style`flex 1`.trim()).equals('flex: 1;'))
o('helpers work', () => {
const helpers = {
'no-arg': 'padding 10',
basic: 'margin',
'basic-fn': (x = 1, y = 2, z = 3) => `content '${x} ${y} ${z}'`,
multi: 'height 50;width 50'
}
fullSheet(
css`
h1 { basic 5 }
h2 { basic 5 10 }
h3 { basic-fn; no-arg; multi }
h4 { basic-fn hello }
h5 { basic-fn hello world }
h6 { basic-fn hello world foo }
===
.test-1 h1 {
margin: 5px;
}
.test-1 h2 {
margin: 5px 10px;
}
.test-1 h3 {
content: '1 2 3';
padding: 10px;
height: 50px;
width: 50px;
}
.test-1 h4 {
content: 'hello 2 3';
}
.test-1 h5 {
content: 'hello world 3';
}
.test-1 h6 {
content: 'hello world foo';
}
`,
{ helpers }
)
})
// TODO: add tests for selector prefixing and better error handling (JSDOM doesn't seem to give syntax errors like browsers do)
})

0 comments on commit 260c530

Please sign in to comment.