Skip to content
This repository was archived by the owner on Apr 6, 2021. It is now read-only.

Add test for mutable state interactions #142

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions src/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ function nameClass(...args) {
* Clone generated and/or cached nodes to ensure no future
* postcss plugins can mutate the rules and mess up our cache
*
* NOTE: Only clone the nodes you pass to root.append()
*
* @param {import('postcss').Node[]} nodes
* */
function cloneNodes(nodes) {
Expand Down
3 changes: 3 additions & 0 deletions tests/11-mutable.test.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.bg-foo {
background-image: url("./foo.png");
}
13 changes: 13 additions & 0 deletions tests/11-mutable.test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Title</title>
<link rel="stylesheet" href="./tailwind.css" />
</head>
<body>
<div class="bg-foo"></div>
</body>
</html>
53 changes: 53 additions & 0 deletions tests/11-mutable.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
const postcss = require('postcss')
const tailwind = require('../src/index.js')
const fs = require('fs')
const path = require('path')

function pluginThatMutatesRules() {
return (root) => {
root.walkRules((rule) => {
rule.nodes.filter(node => node.prop === 'background-image').forEach(node => {
node.value = 'url("./bar.png")'
})

return rule
})
}
}

function run(input, config = {}) {
return postcss([tailwind(config)]).process(input, { from: path.resolve(__filename) })
}

test.only('plugins mutating rules after tailwind doesnt break it', async () => {
let config = {
purge: [path.resolve(__dirname, './11-mutable.test.html')],
theme: {
backgroundImage: {
'foo': 'url("./foo.png")',
}
},
plugins: [],
}

let css = `@tailwind utilities;`

function checkResult(result) {
let expectedPath = path.resolve(__dirname, './11-mutable.test.css')
let expected = fs.readFileSync(expectedPath, 'utf8')

expect(result.css).toMatchCss(expected)
}

// Verify the first run produces the expected result
let firstRun = await run(css, config)
checkResult(firstRun)

// Outside of the context of tailwind jit more postcss plugins may operate on the AST:
// In this case we have a plugin that mutates rules directly
await postcss([ pluginThatMutatesRules() ]).process(firstRun, { from: path.resolve(__filename) })

// Verify subsequent runs don't produce mutated rules
let secondRun = await run(css, config)
checkResult(secondRun)
})