/
asset.spec.ts
157 lines (134 loc) · 4.09 KB
/
asset.spec.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { describe, test, expect } from 'vitest'
import { assetFileNamesToFileName, getAssetHash } from '../plugins/asset'
describe('getAssetHash', () => {
test('8-digit hex', () => {
const hash = getAssetHash(Buffer.alloc(0))
expect(hash).toMatch(/^[\da-f]{8}$/)
})
})
describe('assetFileNamesToFileName', () => {
// on Windows, both forward slashes and backslashes may appear in the input
const sourceFilepaths: readonly string[] =
process.platform === 'win32'
? ['C:/path/to/source/input.png', 'C:\\path\\to\\source\\input.png']
: ['/path/to/source/input.png']
for (const sourceFilepath of sourceFilepaths) {
const content = Buffer.alloc(0)
const contentHash = 'abcd1234'
// basic examples
test('a string with no placeholders', () => {
const fileName = assetFileNamesToFileName(
'output.png',
sourceFilepath,
contentHash,
content
)
expect(fileName).toBe('output.png')
})
test('a string with placeholders', () => {
const fileName = assetFileNamesToFileName(
'assets/[name]/[ext]/[extname]/[hash]',
sourceFilepath,
contentHash,
content
)
expect(fileName).toBe('assets/input/png/.png/abcd1234')
})
// function examples
test('a function that uses asset information', () => {
const fileName = assetFileNamesToFileName(
(options) =>
`assets/${options.name.replace(/^C:|[/\\]/g, '')}/${options.type}/${
options.source.length
}`,
sourceFilepath,
contentHash,
content
)
expect(fileName).toBe('assets/pathtosourceinput.png/asset/0')
})
test('a function that returns a string with no placeholders', () => {
const fileName = assetFileNamesToFileName(
() => 'output.png',
sourceFilepath,
contentHash,
content
)
expect(fileName).toBe('output.png')
})
test('a function that returns a string with placeholders', () => {
const fileName = assetFileNamesToFileName(
() => 'assets/[name]/[ext]/[extname]/[hash]',
sourceFilepath,
contentHash,
content
)
expect(fileName).toBe('assets/input/png/.png/abcd1234')
})
// invalid cases
test('a string with an invalid placeholder', () => {
expect(() => {
assetFileNamesToFileName(
'assets/[invalid]',
sourceFilepath,
contentHash,
content
)
}).toThrowError(
'invalid placeholder [invalid] in assetFileNames "assets/[invalid]"'
)
expect(() => {
assetFileNamesToFileName(
'assets/[name][invalid][extname]',
sourceFilepath,
contentHash,
content
)
}).toThrowError(
'invalid placeholder [invalid] in assetFileNames "assets/[name][invalid][extname]"'
)
})
test('a function that returns a string with an invalid placeholder', () => {
expect(() => {
assetFileNamesToFileName(
() => 'assets/[invalid]',
sourceFilepath,
contentHash,
content
)
}).toThrowError(
'invalid placeholder [invalid] in assetFileNames "assets/[invalid]"'
)
expect(() => {
assetFileNamesToFileName(
() => 'assets/[name][invalid][extname]',
sourceFilepath,
contentHash,
content
)
}).toThrowError(
'invalid placeholder [invalid] in assetFileNames "assets/[name][invalid][extname]"'
)
})
test('a number', () => {
expect(() => {
assetFileNamesToFileName(
9876 as unknown as string,
sourceFilepath,
contentHash,
content
)
}).toThrowError('assetFileNames must be a string or a function')
})
test('a function that returns a number', () => {
expect(() => {
assetFileNamesToFileName(
() => 9876 as unknown as string,
sourceFilepath,
contentHash,
content
)
}).toThrowError('assetFileNames must return a string')
})
}
})