Repository navigation
<
10BC8
div class="Box_2__2Iyod3">More file actions
Expand file tree
/
Copy pathbasic.spec.ts
More file actions
131 lines (126 loc) · 3.88 KB
/
Copy pathbasic.spec.ts
File metadata and controls
131 lines (126 loc) · 3.88 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import { describe, expect, it } from 'vitest'
import { defu } from 'defu'
import { join } from 'pathe'
import { loadNuxt } from '@nuxt/kit'
import type { NuxtConfig } from '@nuxt/schema'
import type * as tailwindcss from 'tailwindcss'
type TWConfig = tailwindcss.Config;
import type resolveConfig from 'tailwindcss/resolveConfig'
async function getTailwindCSSConfig (overrides: Partial<NuxtConfig> = {}) {
let tailwindConfig: ReturnType<typeof resolveConfig<TWConfig>>
const nuxt = await loadNuxt({
ready: true,
cwd: join(process.cwd(), 'fixtures', 'empty'),
dev: false,
overrides: defu(overrides, {
ssr: false,
modules: ['../../src/module'],
hooks: {
'tailwindcss:resolvedConfig' (config) {
tailwindConfig = config
}
}
} satisfies NuxtConfig) as NuxtConfig
})
const nuxtOptions = structuredClone({
plugins: nuxt.options.plugins.map(p => typeof p !== 'string' && ({ src: p.src, mode: p.mode })),
_requiredModules: nuxt.options._requiredModules,
appConfig: nuxt.options.appConfig
})
await nuxt.close()
return {
nuxtOptions,
tailwindConfig
}
}
describe('nuxt', () => {
it('should add plugins and modules to nuxt', async () => {
const { nuxtOptions } = await getTailwindCSSConfig()
expect(nuxtOptions.plugins).toContainEqual(
expect.objectContaining({
src: expect.stringContaining('plugins/colors'),
mode: 'all'
})
)
expect(nuxtOptions._requiredModules).toMatchObject({
'@nuxtjs/color-mode': true,
'@nuxtjs/tailwindcss': true
})
// default values in appConfig
expect(nuxtOptions.appConfig.ui).toMatchObject({
primary: 'green',
gray: 'cool'
})
})
})
describe('tailwindcss config', () => {
it.each([
/* format:
name,
tailwindcss config, safelistColors,
expected safelistPatterns (add "!" before a pattern to negate it)
*/
[
'default safelist',
{}, [],
['bg-(primary)-50', 'bg-(red)-500'] // these both should be in the safelist
],
[
'safelisting single new color',
{}, ['myColor'],
'bg-(myColor|primary)-50'
],
[
'reducing amount of theme colors',
{ theme: { colors: { plainBlue: '#00F' } } }, ['plainBlue'],
['bg-(plainBlue|primary)-50', '!', /orange/] // the word "orange" should _not_ be found in any safelist pattern
]
])('%s', async (_description, tailwindcss, safelistColors, safelistPatterns) => {
const { tailwindConfig } = await getTailwindCSSConfig({
ui: {
safelistColors
},
tailwindcss: {
config: tailwindcss
}
})
expect.extend({
toBeRegExp: (received, expected) => {
if (typeof expected === 'string' || expected instanceof String) {
return {
message: () => `expected ${received} to be exact regex ${expected}`,
pass: received.toString() === RegExp(expected as string).toString()
}
} else if (expected instanceof RegExp) {
return {
message: () => `expected ${received} to be a regex like ${expected.toString()}`,
pass: received.toString().match(expected)
}
}
return {
message: () => `expected ${received} to be a regex`,
pass: false
}
}
})
safelistPatterns = safelistPatterns instanceof Array ? safelistPatterns : [safelistPatterns]
let negate = false
for (const safelistPattern of safelistPatterns) {
if (safelistPattern === '!') {
// negate next!
negate = true
continue
}
if (negate) {
expect(tailwindConfig.safelist).not.toContainEqual({
pattern: expect.toBeRegExp(safelistPattern)
})
} else {
expect(tailwindConfig.safelist).toContainEqual({
pattern: expect.toBeRegExp(safelistPattern)
})
}
negate = false
}
})
})