tailwindcss/tests/modify-selectors.test.js
Robin Malfait 1454190ea0
inline tests (#10362)
This will reduce the amount of different test "implementations" and
should further unify/normalise the tests. Some tests are very large and
can be split up more but this is a start in the right direction.

One of the biggest benefits is that changes in the tests file re-trigger
a jest run in watch mode. A change in the HTML/CSS file won't.
2023-01-19 11:42:52 +01:00

98 lines
2.5 KiB
JavaScript

import selectorParser from 'postcss-selector-parser'
import { run, html, css } from './util/run'
test('modify selectors', () => {
let config = {
darkMode: 'class',
content: [
{
raw: html`
<div class="font-bold"></div>
<div class="foo:font-bold"></div>
<div class="foo:hover:font-bold"></div>
<div class="sm:foo:font-bold"></div>
<div class="md:foo:focus:font-bold"></div>
<div class="markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="foo:markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="foo:visited:markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
<div class="lg:foo:disabled:markdown">
<p>Lorem ipsum dolor sit amet...</p>
</div>
`,
},
],
corePlugins: { preflight: false },
theme: {},
plugins: [
function ({ addVariant }) {
addVariant('foo', ({ modifySelectors, separator }) => {
modifySelectors(({ selector }) => {
return selectorParser((selectors) => {
selectors.walkClasses((classNode) => {
classNode.value = `foo${separator}${classNode.value}`
classNode.parent.insertBefore(classNode, selectorParser().astSync(`.foo `))
})
}).processSync(selector)
})
})
},
],
}
let input = css`
@tailwind components;
@tailwind utilities;
@layer components {
.markdown > p {
margin-top: 12px;
}
}
`
return run(input, config).then((result) => {
expect(result.css).toMatchFormattedCss(css`
.markdown > p {
margin-top: 12px;
}
.font-bold {
font-weight: 700;
}
.foo .foo\:markdown > p {
margin-top: 12px;
}
.foo .foo\:font-bold {
font-weight: 700;
}
.foo .foo\:visited\:markdown:visited > p {
margin-top: 12px;
}
.foo .foo\:hover\:font-bold:hover {
font-weight: 700;
}
@media (min-width: 640px) {
.foo .sm\:foo\:font-bold {
font-weight: 700;
}
}
@media (min-width: 768px) {
.foo .md\:foo\:focus\:font-bold:focus {
font-weight: 700;
}
}
@media (min-width: 1024px) {
.foo .lg\:foo\:disabled\:markdown:disabled > p {
margin-top: 12px;
}
}
`)
})
})