generated from sonofmagic/monorepo-template
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcache.test.ts
107 lines (98 loc) · 2.9 KB
/
cache.test.ts
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
import { pkgName } from '@/constants'
import { CacheManager, TailwindcssPatcher } from '@/core'
import { getCacheOptions } from '@/core/cache'
import { isCI } from 'ci-info'
import fs from 'fs-extra'
import path from 'pathe'
import { getCss } from './utils'
describe('cache', () => {
let cm: CacheManager
beforeEach(() => {
cm = new CacheManager()
})
it('getCacheOptions', () => {
expect(cm.getOptions).toBeDefined()
expect(cm.getOptions().dir).toBe(path.resolve(process.cwd(), './node_modules/.cache', pkgName))
expect(getCacheOptions(false)).toEqual({
enable: false,
})
expect(getCacheOptions(true)).toEqual({
enable: true,
})
})
it('mkCacheDirectory', () => {
const dir = path.resolve(__dirname, './fixtures', pkgName)
fs.ensureDirSync(dir)
expect(fs.existsSync(dir)).toBe(true)
fs.rmdirSync(dir)
expect(fs.existsSync(dir)).toBe(false)
})
it('write and read cache default option', async () => {
// const opt = getCacheOptions()
const opt = {
dir: path.resolve(__dirname, 'fixtures/cache'),
file: 'raw-method.json',
}
cm = new CacheManager(opt)
let cache: Set<string> | undefined
cache = await cm.read()
// expect(cache).toBe(undefined)
await cm.write(new Set(['a', 'b', 'c']))
cache = await cm.read()
expect(cache).toBeDefined()
if (cache) {
expect(cache.size).toBe(3)
}
})
it('read broken cache', async () => {
// const opt = getCacheOptions()
const dir = path.resolve(__dirname, './fixtures', `${pkgName}-broken`)
const filepath = path.resolve(dir, 'index.json')
fs.outputFileSync(
filepath,
`{
[ '2',"fuck you",{s:'12}
}`,
'utf8',
)
cm = new CacheManager({
dir,
})
expect(fs.existsSync(filepath)).toBe(true)
const cache = await cm.read()
expect(cache).toBeInstanceOf(Set)
expect(fs.existsSync(filepath)).toBe(false)
})
it.skipIf(isCI)('multiple tw context merge cache', async () => {
const dir = path.resolve(__dirname, './fixtures/cache')
const twPatcher = new TailwindcssPatcher({
cache: {
dir,
file: 'merge-multiple-context.json',
},
patch: {
output: {
removeUniversalSelector: false,
},
},
})
await twPatcher.setCache(new Set())
await getCss(['text-[100px]'])
let ctxs = twPatcher.getContexts()
expect(ctxs.length).toBe(1)
let set = await twPatcher.getClassSet()
expect(set.size).toBeGreaterThan(0)
expect(set.size).toBe(2)
expect(set.has('text-[100px]')).toBe(true)
// 2 times
// 不累加
await getCss(['text-[99px]'])
ctxs = twPatcher.getContexts()
expect(ctxs.length).toBe(1)
set = await twPatcher.getClassSet()
expect(set.size).toBeGreaterThan(0)
expect(set.size).toBe(3)
expect(set.has('text-[99px]')).toBe(true)
expect(set.has('text-[100px]')).toBe(true)
})
})