-
-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathcss-query.test.ts
More file actions
76 lines (68 loc) · 3.03 KB
/
css-query.test.ts
File metadata and controls
76 lines (68 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, expect, test } from 'vitest'
/**
* Check if a CSS import ID has special queries that should be excluded from CSS collection.
* These queries transform CSS imports to return different data types rather than actual CSS to be linked.
*/
function hasSpecialCssQuery(id: string): boolean {
try {
const url = new URL(id, 'file://')
return (
url.searchParams.has('url') ||
url.searchParams.has('inline') ||
url.searchParams.has('raw')
)
} catch {
// If URL parsing fails, check with simple string matching as fallback
return id.includes('?url') || id.includes('?inline') || id.includes('?raw')
}
}
describe('hasSpecialCssQuery', () => {
test('should return true for CSS imports with ?url query', () => {
expect(hasSpecialCssQuery('/path/to/style.css?url')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?url&other=param')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?other=param&url')).toBe(true)
})
test('should return true for CSS imports with ?inline query', () => {
expect(hasSpecialCssQuery('/path/to/style.css?inline')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?inline&other=param')).toBe(
true,
)
expect(hasSpecialCssQuery('/path/to/style.css?other=param&inline')).toBe(
true,
)
})
test('should return true for CSS imports with ?raw query', () => {
expect(hasSpecialCssQuery('/path/to/style.css?raw')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?raw&other=param')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?other=param&raw')).toBe(true)
})
test('should return false for normal CSS imports', () => {
expect(hasSpecialCssQuery('/path/to/style.css')).toBe(false)
expect(hasSpecialCssQuery('/path/to/style.css?t=123456')).toBe(false)
expect(hasSpecialCssQuery('/path/to/style.css?other=param')).toBe(false)
})
test('should handle complex URLs with multiple parameters', () => {
expect(hasSpecialCssQuery('/path/to/style.css?t=123&url&v=1')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?t=123&inline&v=1')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?t=123&raw&v=1')).toBe(true)
expect(hasSpecialCssQuery('/path/to/style.css?t=123&other=param&v=1')).toBe(
false,
)
})
test('should handle absolute URLs', () => {
expect(hasSpecialCssQuery('http://localhost:3000/style.css?url')).toBe(true)
expect(hasSpecialCssQuery('http://localhost:3000/style.css?inline')).toBe(
true,
)
expect(hasSpecialCssQuery('http://localhost:3000/style.css?raw')).toBe(true)
expect(hasSpecialCssQuery('http://localhost:3000/style.css?t=123')).toBe(
false,
)
})
test('should handle file URLs', () => {
expect(hasSpecialCssQuery('file:///path/to/style.css?url')).toBe(true)
expect(hasSpecialCssQuery('file:///path/to/style.css?inline')).toBe(true)
expect(hasSpecialCssQuery('file:///path/to/style.css?raw')).toBe(true)
expect(hasSpecialCssQuery('file:///path/to/style.css?t=123')).toBe(false)
})
})