Merge pull request #115 from tailwindcss/config-default

Support default values in config function
This commit is contained in:
Jonathan Reinink 2017-11-06 10:36:32 -05:00 • committed by GitHub
commit 0b0cb96fa9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 84 additions and 2 deletions

View file

@ -0,0 +1,82 @@
import postcss from 'postcss'
import plugin from '../src/lib/evaluateTailwindFunctions'
function run(input, opts = {}) {
return postcss([plugin(() => opts)]).process(input)
}
test("it looks up values in the config using dot notation", () => {
const input = `
.banana { color: config('colors.yellow'); }
`
const output = `
.banana { color: #f7cc50; }
`
return run(input, {
colors: {
yellow: '#f7cc50',
}
}).then(result => {
expect(result.css).toEqual(output)
expect(result.warnings().length).toBe(0)
})
})
test("quotes are optional around the lookup path", () => {
const input = `
.banana { color: config(colors.yellow); }
`
const output = `
.banana { color: #f7cc50; }
`
return run(input, {
colors: {
yellow: '#f7cc50',
}
}).then(result => {
expect(result.css).toEqual(output)
expect(result.warnings().length).toBe(0)
})
})
test("a default value can be provided", () => {
const input = `
.cookieMonster { color: config('colors.blue', #0000ff); }
`
const output = `
.cookieMonster { color: #0000ff; }
`
return run(input, {
colors: {
yellow: '#f7cc50',
}
}).then(result => {
expect(result.css).toEqual(output)
expect(result.warnings().length).toBe(0)
})
})
test("quotes are preserved around default values", () => {
const input = `
.heading { font-family: config('fonts.sans', "Helvetica Neue"); }
`
const output = `
.heading { font-family: "Helvetica Neue"; }
`
return run(input, {
fonts: {
serif: "Constantia",
}
}).then(result => {
expect(result.css).toEqual(output)
expect(result.warnings().length).toBe(0)
})
})

View file

@ -6,8 +6,8 @@ export default function(config) {
return functions({
functions: {
config: function (path) {
return _.get(options, _.trim(path, `'"`))
config: function (path, defaultValue) {
return _.get(options, _.trim(path, `'"`), defaultValue)
}
}
})