Skip to content

Commit

Permalink
✅ Complete test for all inclusion and exclusion logic
Browse files Browse the repository at this point in the history
  • Loading branch information
kghugo committed Aug 5, 2020
1 parent 3706302 commit 23bdeab
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 12 deletions.
6 changes: 6 additions & 0 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@rollup/plugin-commonjs": "^14.0.0",
"@rollup/plugin-node-resolve": "^8.4.0",
"chai": "^4.2.0",
"chai-match": "^1.1.1",
"chalk": "^4.1.0",
"eslint": "^7.5.0",
"eslint-config-standard": "^14.1.1",
Expand Down
111 changes: 99 additions & 12 deletions test/inclusionLogic.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ const postcssRfsAutopilot = require('../index.js')

const chai = require('chai')
const expect = chai.expect
chai.use(require('chai-match'))

describe('Test shouldBeTransformed()', function () {
let css
let css, beforeTransformation, afterTransformation

beforeEach(function () {
css = `p #hello{
Expand All @@ -16,6 +17,19 @@ describe('Test shouldBeTransformed()', function () {
body{
padding: 5px;
}`

beforeTransformation = []
afterTransformation = []

postcss
.parse(css, { from: undefined })
.walkDecls((decl) => {
beforeTransformation.push({
selector: decl.parent.selector,
prop: decl.prop,
value: decl.value
})
})
})

describe('if a value is already wrapped in rfs()', function () {
Expand All @@ -26,31 +40,104 @@ describe('Test shouldBeTransformed()', function () {

await postcss([
postcssRfsAutopilot(options)
]).process(css, { from: undefined }).then(result => {
result.root.walkDecls((decl) => {
if (decl.prop === 'margin') {
expect(decl.value).to.equal('rfs(10rem)')
}
])
.process(css, { from: undefined }).then(result => {
result.root.walkDecls((decl) => {
afterTransformation.push({
selector: decl.parent.selector,
prop: decl.prop,
value: decl.value
})
})
})

beforeTransformation.forEach((decl, index) => {
if (/^rfs/g.test(decl.value)) {
expect(decl.value).to.equal(afterTransformation[index].value)
}
})
})
})

describe('if the unit of a value is not included in includedUnits', function () {
it('should not be transformed', function (done) {
done()
it('should not be transformed', async function () {
const options = {
includedUnits: ['rem']
}

await postcss([
postcssRfsAutopilot(options)
])
.process(css, { from: undefined }).then(result => {
result.root.walkDecls((decl) => {
afterTransformation.push({
selector: decl.parent.selector,
prop: decl.prop,
value: decl.value
})
})
})

beforeTransformation.forEach((decl, index) => {
if (!/rem/g.test(decl.value)) {
expect(afterTransformation[index].value).to.not.match(/^rfs/g)
}
})
})
})

describe('if the unit of a value is included in excludedUnits', function () {
it('should not be transformed', function (done) {
done()
it('should not be transformed', async function () {
const options = {
excludedUnits: ['px']
}

await postcss([
postcssRfsAutopilot(options)
])
.process(css, { from: undefined }).then(result => {
result.root.walkDecls((decl) => {
afterTransformation.push({
selector: decl.parent.selector,
prop: decl.prop,
value: decl.value
})
})
})

beforeTransformation.forEach((decl, index) => {
if (/px/g.test(decl.value)) {
expect(afterTransformation[index].value).to.not.match(/^rfs/g)
}
})
})
})

describe('if the unit of a value is included in includedUnits and excludedUnits', function () {
it('should not be transformed', function (done) {
done()
it('should not be transformed', async function () {
const options = {
includedUnits: ['px'],
excludedUnits: ['px']
}

await postcss([
postcssRfsAutopilot(options)
])
.process(css, { from: undefined }).then(result => {
result.root.walkDecls((decl) => {
afterTransformation.push({
selector: decl.parent.selector,
prop: decl.prop,
value: decl.value
})
})
})

beforeTransformation.forEach((decl, index) => {
if (/px/g.test(decl.value)) {
expect(afterTransformation[index].value).to.not.match(/^rfs/g)
}
})
})
})
})

0 comments on commit 23bdeab

Please sign in to comment.