Skip to content

Commit 82c95e0

Browse files
committed
test: refactor tests
1 parent f739f0e commit 82c95e0

3 files changed

Lines changed: 188 additions & 140 deletions

File tree

src/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import plugin from 'tailwindcss/plugin'
1+
const plugin = require('tailwindcss/plugin')
22

3-
const examplePlugin = plugin.withOptions(
3+
module.exports = plugin.withOptions(
44
function (options) {
55
const className = options ? options.className : 'markdown'
66

@@ -90,5 +90,3 @@ const examplePlugin = plugin.withOptions(
9090
}
9191
}
9292
)
93-
94-
export default examplePlugin

src/index.test.js

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
import path from 'path'
2+
import postcss from 'postcss'
3+
import examplePlugin from '.'
4+
import { expect, test } from 'vitest'
5+
import tailwindcss from 'tailwindcss'
6+
7+
// Custom CSS matcher
8+
expect.extend({
9+
// Compare two CSS strings with all whitespace removed
10+
// This is probably naive but it's fast and works well enough.
11+
toMatchCss(received, argument) {
12+
function stripped(string_) {
13+
return string_.replaceAll(/\s/g, '').replaceAll(';', '')
14+
}
15+
16+
const pass = stripped(received) === stripped(argument)
17+
18+
return {
19+
pass,
20+
actual: received,
21+
expected: argument,
22+
message: () => pass ? 'All good!' : 'CSS does not match',
23+
}
24+
}
25+
})
26+
27+
// Function to run the plugin
28+
function run(config, css = '@tailwind utilities', plugin = tailwindcss) {
29+
let { currentTestName } = expect.getState()
30+
31+
config = {
32+
...{
33+
plugins: [examplePlugin],
34+
corePlugins: {
35+
preflight: false,
36+
}
37+
},
38+
...config,
39+
}
40+
41+
return postcss(plugin(config)).process(css, {
42+
from: `${path.resolve(__filename)}?test=${currentTestName}`,
43+
})
44+
}
45+
46+
test('addBase', () => {
47+
const config = {
48+
content: [
49+
{
50+
raw: String.raw`🫣`
51+
}
52+
],
53+
corePlugins: {
54+
preflight: true,
55+
},
56+
}
57+
58+
return run(config, '@tailwind base').then(result => {
59+
expect(result.css).toContain('*, ::before, ::after')
60+
})
61+
})
62+
63+
test('addUtilities', () => {
64+
const config = {
65+
content: [
66+
{
67+
raw: String.raw`
68+
<div class="content-hidden"></div>
69+
<div class="content-visible"></div>
70+
`
71+
}
72+
],
73+
}
74+
75+
return run(config).then(result => {
76+
expect(result.css).toMatchCss(String.raw`
77+
.content-hidden {
78+
content-visibility: hidden
79+
}
80+
.content-visible {
81+
content-visibility: visible
82+
}
83+
`)
84+
})
85+
})
86+
87+
test('matchUtilities', () => {
88+
const config = {
89+
content: [
90+
{
91+
raw: String.raw`<div class="tab-2"></div>`
92+
}
93+
],
94+
}
95+
96+
return run(config).then(result => {
97+
expect(result.css).toMatchCss(String.raw`
98+
.tab-2 {
99+
tab-size: 2
100+
}
101+
`)
102+
})
103+
})
104+
105+
test('addComponents', () => {
106+
const config = {
107+
content: [
108+
{
109+
raw: String.raw`<div class="btn"></div>`
110+
}
111+
],
112+
plugins: [
113+
examplePlugin({
114+
className: 'btn'
115+
})
116+
],
117+
}
118+
119+
return run(config, '@tailwind components').then(result => {
120+
expect(result.css).toMatchCss(String.raw`
121+
.btn {
122+
padding: .5rem 1rem;
123+
font-weight: 600
124+
}
125+
`)
126+
})
127+
})
128+
129+
test('addVariant', () => {
130+
const config = {
131+
content: [
132+
{
133+
raw: String.raw`<div class="optional:hidden"></div>`
134+
}
135+
],
136+
}
137+
138+
return run(config).then(result => {
139+
expect(result.css).toMatchCss(String.raw`
140+
.optional\:hidden:optional {
141+
display: none
142+
}
143+
`)
144+
})
145+
})
146+
147+
test('addVariant (array)', () => {
148+
const config = {
149+
content: [
150+
{
151+
raw: String.raw`<div class="hocus:opacity-0"></div>`
152+
}
153+
],
154+
}
155+
156+
return run(config).then(result => {
157+
expect(result.css).toMatchCss(String.raw`
158+
.hocus\:opacity-0:hover {
159+
opacity: 0
160+
}
161+
.hocus\:opacity-0:focus {
162+
opacity: 0
163+
}
164+
`)
165+
})
166+
})
167+
168+
test('addVariant (media)', () => {
169+
const config = {
170+
content: [
171+
{
172+
raw: String.raw`<div class="supports-grid:hidden"></div>`
173+
}
174+
],
175+
}
176+
177+
return run(config).then(result => {
178+
expect(result.css).toMatchCss(String.raw`
179+
@supports (display: grid) {
180+
.supports-grid\:hidden {
181+
display: none
182+
}
183+
}
184+
`)
185+
})
186+
})

test/index.test.js

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)