From c7061e922894ed8690c0401a66d325e59ab7d54a Mon Sep 17 00:00:00 2001 From: Adam Wathan Date: Mon, 6 Nov 2017 10:32:17 -0500 Subject: [PATCH] Support default values in config function Also add a bunch of tests, seems probably wise :) --- __tests__/configFunction.test.js | 82 ++++++++++++++++++++++++++++ src/lib/evaluateTailwindFunctions.js | 4 +- 2 files changed, 84 insertions(+), 2 deletions(-) create mode 100644 __tests__/configFunction.test.js diff --git a/__tests__/configFunction.test.js b/__tests__/configFunction.test.js new file mode 100644 index 000000000..11f93876a --- /dev/null +++ b/__tests__/configFunction.test.js @@ -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) + }) +}) diff --git a/src/lib/evaluateTailwindFunctions.js b/src/lib/evaluateTailwindFunctions.js index 3b2ab3486..2e00ed9c1 100644 --- a/src/lib/evaluateTailwindFunctions.js +++ b/src/lib/evaluateTailwindFunctions.js @@ -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) } } })