From dd0c7b00dc1567104da2b1bb9633a3634c040fd5 Mon Sep 17 00:00:00 2001 From: David Hemphill Date: Sat, 9 Sep 2017 13:26:21 -0500 Subject: [PATCH 1/4] Add findColor test --- __tests__/findColor.test.js | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 __tests__/findColor.test.js diff --git a/__tests__/findColor.test.js b/__tests__/findColor.test.js new file mode 100644 index 000000000..0117e6c87 --- /dev/null +++ b/__tests__/findColor.test.js @@ -0,0 +1,10 @@ +import postcss from 'postcss' +import findColor from '../src/util/findColor' + +/** + * Tests + */ +it('finds a color in an object', () => { + let color = findColor({red: '#FF0000', blue: '#0000FF'}, 'red') + expect(color).toEqual(`#FF0000`) +}) From ee1659ab760e7287675ba44d4d6cab06d7724c6f Mon Sep 17 00:00:00 2001 From: David Hemphill Date: Sat, 14 Oct 2017 00:11:19 -0500 Subject: [PATCH 2/4] prevent rules inside media queries from being used as a mixin --- __tests__/applyAtRule.test.js | 54 +++++++++++++++++++++++++++++++++++ src/util/findMixin.js | 2 +- 2 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 __tests__/applyAtRule.test.js diff --git a/__tests__/applyAtRule.test.js b/__tests__/applyAtRule.test.js new file mode 100644 index 000000000..693a412c9 --- /dev/null +++ b/__tests__/applyAtRule.test.js @@ -0,0 +1,54 @@ +var postcss = require('postcss') + +import plugin from '../src/lib/substituteClassApplyAtRules' + +function run(input, opts) { + return postcss([plugin(opts)]).process(input) +} + +test("it copies a class's declarations into itself", () => { + const output = '.a { color: red; } .b { color: red; }' + + return run('.a { color: red; } .b { @apply .a; }', {}).then(result => { + expect(result.css).toEqual(output) + expect(result.warnings().length).toBe(0) + }) +}) + +test("it doesn't copy a media query definition into itself", () => { + const output = `.a { + color: red; + } + + @media (min-width: 300px) { + .a { color: blue; } + } + + .b { + color: red; + }` + + return run( + `.a { + color: red; + } + + @media (min-width: 300px) { + .a { color: blue; } + } + + .b { + @apply .a; + }`, + {} + ).then(result => { + expect(result.css).toEqual(output) + expect(result.warnings().length).toBe(0) + }) +}) + +test('it fails if the class does not exist', () => { + run('.b { @apply .a; }', {}).catch(error => { + expect(error.reason).toEqual('No .a class found.') + }) +}) diff --git a/src/util/findMixin.js b/src/util/findMixin.js index d01aad233..b07372a4e 100644 --- a/src/util/findMixin.js +++ b/src/util/findMixin.js @@ -4,7 +4,7 @@ export default function findMixin(css, mixin, onError) { const matches = [] css.walkRules(rule => { - if (rule.selectors.includes(mixin)) { + if (rule.selectors.includes(mixin) && rule.parent.type == 'root') { matches.push(rule) } }) From 59d2f6661f6295822951dea1bca3cfa030c14e7a Mon Sep 17 00:00:00 2001 From: David Hemphill Date: Sat, 14 Oct 2017 00:25:08 -0500 Subject: [PATCH 3/4] Remove unused test --- __tests__/findColor.test.js | 10 ---------- 1 file changed, 10 deletions(-) delete mode 100644 __tests__/findColor.test.js diff --git a/__tests__/findColor.test.js b/__tests__/findColor.test.js deleted file mode 100644 index 0117e6c87..000000000 --- a/__tests__/findColor.test.js +++ /dev/null @@ -1,10 +0,0 @@ -import postcss from 'postcss' -import findColor from '../src/util/findColor' - -/** - * Tests - */ -it('finds a color in an object', () => { - let color = findColor({red: '#FF0000', blue: '#0000FF'}, 'red') - expect(color).toEqual(`#FF0000`) -}) From 4f2713fae6696b059a756a037fb88226ad99015a Mon Sep 17 00:00:00 2001 From: David Hemphill Date: Sat, 14 Oct 2017 12:12:56 -0500 Subject: [PATCH 4/4] change to es6 import --- __tests__/applyAtRule.test.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/__tests__/applyAtRule.test.js b/__tests__/applyAtRule.test.js index 693a412c9..652b44940 100644 --- a/__tests__/applyAtRule.test.js +++ b/__tests__/applyAtRule.test.js @@ -1,5 +1,4 @@ -var postcss = require('postcss') - +import postcss from 'postcss' import plugin from '../src/lib/substituteClassApplyAtRules' function run(input, opts) {