Skip to content

Commit dc5a298

Browse files
author
Laurynas Grigutis
committed
Added tests for "tailwindCSS.experimental.classFunctions"
1 parent baee7b5 commit dc5a298

File tree

1 file changed

+233
-2
lines changed

1 file changed

+233
-2
lines changed

packages/tailwindcss-language-service/src/util/find.test.ts

Lines changed: 233 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import type { State } from './state'
1+
import type { DocumentClassList, State } from './state'
22
import { test } from 'vitest'
33
import { TextDocument } from 'vscode-languageserver-textdocument'
44
import { findClassListsInHtmlRange } from './find'
55

6-
test('test', async ({ expect }) => {
6+
test('test astro', async ({ expect }) => {
77
let content = [
88
//
99
'<a class=`p-4 sm:p-2 ${active ? "underline": "line-through"}`>',
@@ -79,3 +79,234 @@ test('test', async ({ expect }) => {
7979
]
8080
`)
8181
})
82+
83+
test('test simple classFunctions', async ({ expect }) => {
84+
const state: State = {
85+
blocklist: [],
86+
editor: {
87+
userLanguages: {},
88+
getConfiguration: async () => ({
89+
editor: {
90+
tabSize: 1,
91+
},
92+
tailwindCSS: {
93+
classAttributes: ['class'],
94+
experimental: {
95+
classFunctions: ['cva', 'cn'],
96+
},
97+
},
98+
}),
99+
},
100+
} as any
101+
102+
const classList = `'pointer-events-auto relative flex bg-red-500',
103+
'items-center justify-between overflow-hidden',
104+
'md:min-w-[20rem] md:max-w-[37.5rem] md:py-sm py-xs pl-md pr-xs gap-sm w-full',
105+
'data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)]',
106+
Date.now() > 15000 ? 'text-red-200' : 'text-red-700',
107+
'data-[swipe=move]:transition-none',
108+
`
109+
110+
const expectedResult: DocumentClassList[] = [
111+
{
112+
classList: 'pointer-events-auto relative flex bg-red-500',
113+
range: {
114+
start: { character: 7, line: 2 },
115+
end: { character: 51, line: 2 },
116+
},
117+
},
118+
{
119+
classList: 'items-center justify-between overflow-hidden',
120+
range: {
121+
start: { character: 7, line: 3 },
122+
end: { character: 51, line: 3 },
123+
},
124+
},
125+
{
126+
classList: 'md:min-w-[20rem] md:max-w-[37.5rem] md:py-sm py-xs pl-md pr-xs gap-sm w-full',
127+
range: {
128+
start: { character: 7, line: 4 },
129+
end: { character: 83, line: 4 },
130+
},
131+
},
132+
{
133+
classList: 'data-[swipe=end]:translate-x-[var(--radix-toast-swipe-end-x)]',
134+
range: {
135+
start: { line: 5, character: 7 },
136+
end: { line: 5, character: 68 },
137+
},
138+
},
139+
{
140+
classList: 'text-red-200',
141+
range: {
142+
start: { line: 6, character: 28 },
143+
end: { line: 6, character: 40 },
144+
},
145+
},
146+
{
147+
classList: 'text-red-700',
148+
range: {
149+
start: { line: 6, character: 45 },
150+
end: { line: 6, character: 57 },
151+
},
152+
},
153+
{
154+
classList: 'data-[swipe=move]:transition-none',
155+
range: {
156+
start: { line: 7, character: 7 },
157+
end: { line: 7, character: 40 },
158+
},
159+
},
160+
]
161+
162+
const cnContent = `
163+
const classes = cn(
164+
${classList}
165+
)
166+
`
167+
168+
const cnDoc = TextDocument.create('file://file.html', 'html', 1, cnContent)
169+
const cnClassLists = await findClassListsInHtmlRange(state, cnDoc, 'html')
170+
171+
expect(cnClassLists).toMatchObject(expectedResult)
172+
173+
const cvaContent = `
174+
const classes = cva(
175+
${classList}
176+
)
177+
`
178+
179+
const cvaDoc = TextDocument.create('file://file.html', 'html', 1, cvaContent)
180+
const cvaClassLists = await findClassListsInHtmlRange(state, cvaDoc, 'html')
181+
182+
expect(cvaClassLists).toMatchObject(expectedResult)
183+
184+
// Ensure another function name with the same layout doesn't match
185+
const cmaContent = `
186+
const classes = cma(
187+
${classList}
188+
)
189+
`
190+
191+
const cmaDoc = TextDocument.create('file://file.html', 'html', 1, cmaContent)
192+
const cmaClassLists = await findClassListsInHtmlRange(state, cmaDoc, 'html')
193+
194+
expect(cmaClassLists).not.toMatchObject(expectedResult)
195+
})
196+
197+
test('test nested classFunctions', async ({ expect }) => {
198+
const state: State = {
199+
blocklist: [],
200+
editor: {
201+
userLanguages: {},
202+
getConfiguration: async () => ({
203+
editor: {
204+
tabSize: 1,
205+
},
206+
tailwindCSS: {
207+
classAttributes: ['class'],
208+
experimental: {
209+
classFunctions: ['cva', 'cn'],
210+
},
211+
},
212+
}),
213+
},
214+
} as any
215+
216+
const expectedResult: DocumentClassList[] = [
217+
{
218+
classList: 'fixed flex',
219+
range: {
220+
start: { line: 3, character: 9 },
221+
end: { line: 3, character: 19 },
222+
},
223+
},
224+
{
225+
classList: 'md:h-[calc(100%-2rem)]',
226+
range: {
227+
start: { line: 4, character: 9 },
228+
end: { line: 4, character: 31 },
229+
},
230+
},
231+
{
232+
classList: 'bg-red-700',
233+
range: {
234+
start: { line: 5, character: 9 },
235+
end: { line: 5, character: 19 },
236+
},
237+
},
238+
{
239+
classList: 'bottom-0 left-0',
240+
range: {
241+
start: { line: 10, character: 22 },
242+
end: { line: 10, character: 37 },
243+
},
244+
},
245+
{
246+
classList:
247+
'inset-0\n md:h-[calc(100%-2rem)]\n rounded-none\n ',
248+
range: {
249+
start: { line: 12, character: 14 },
250+
end: { line: 14, character: 26 },
251+
},
252+
},
253+
{
254+
classList: 'default',
255+
range: {
256+
start: { line: 19, character: 19 },
257+
end: { line: 19, character: 26 },
258+
},
259+
},
260+
{
261+
classList: 'fixed flex',
262+
range: {
263+
start: { line: 3, character: 9 },
264+
end: { line: 3, character: 19 },
265+
},
266+
},
267+
{
268+
classList: 'md:h-[calc(100%-2rem)]',
269+
range: {
270+
start: { line: 4, character: 9 },
271+
end: { line: 4, character: 31 },
272+
},
273+
},
274+
{
275+
classList: 'bg-red-700',
276+
range: {
277+
start: { line: 5, character: 9 },
278+
end: { line: 5, character: 19 },
279+
},
280+
},
281+
]
282+
283+
const content = `
284+
const variants = cva(
285+
cn(
286+
'fixed flex',
287+
'md:h-[calc(100%-2rem)]',
288+
'bg-red-700',
289+
),
290+
{
291+
variants: {
292+
mobile: {
293+
default: 'bottom-0 left-0',
294+
fullScreen: \`
295+
inset-0
296+
md:h-[calc(100%-2rem)]
297+
rounded-none
298+
\`,
299+
},
300+
},
301+
defaultVariants: {
302+
mobile: 'default',
303+
},
304+
},
305+
)
306+
`
307+
308+
const cnDoc = TextDocument.create('file://file.html', 'html', 1, content)
309+
const classLists = await findClassListsInHtmlRange(state, cnDoc, 'html')
310+
311+
expect(classLists).toMatchObject(expectedResult)
312+
})

0 commit comments

Comments
 (0)