-
-
Notifications
You must be signed in to change notification settings - Fork 412
/
index.test.ts
240 lines (213 loc) · 7.63 KB
/
index.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
import { promises as fs } from 'fs'
import * as path from 'path'
import { exec as execCb } from 'child_process'
import { promisify } from 'util'
// @ts-ignore
import del from 'del'
const exec = promisify(execCb)
const svgr = path.join(__dirname, '../bin/svgr')
const sortCliResult = (result: string) => {
return result
.split(/\n/)
.sort((a, b) => a.localeCompare(b))
.map((x) => x.toLowerCase())
.join('\n')
}
describe('cli', () => {
const cli = async (args: string) => {
const { stdout } = await exec(`${svgr} ${args}`)
return stdout
}
it('should work with a simple file', async () => {
const result = await cli('__fixtures__/simple/file.svg')
expect(result).toMatchSnapshot()
})
it('should not work with a directory without --out-dir option', async () => {
expect.assertions(1)
try {
await cli('__fixtures__/nesting')
} catch (error: any) {
expect(error.message).toMatch(
'Directory are not supported without `--out-dir` option instead',
)
}
})
it('should not work with several files without destination', async () => {
expect.assertions(1)
try {
await cli('__fixtures__/simple/file.svg __fixtures__/nesting/one.svg')
} catch (error: any) {
expect(error.message).toMatch(
'Please specify only one filename or use `--out-dir` option',
)
}
})
it('should work with stdin', async () => {
const result = await cli('< __fixtures__/simple/file.svg')
expect(result).toMatchSnapshot()
})
it('should support stdin filepath', async () => {
const result = await cli(
'--stdin-filepath __fixtures__/simple/file.svg < __fixtures__/simple/file.svg',
)
expect(result).toMatchSnapshot()
})
it('should transform a whole directory and output relative destination paths', async () => {
const result = await cli('--out-dir __fixtures_build__/whole __fixtures__')
expect(sortCliResult(result)).toMatchSnapshot()
})
it('should transform a whole directory with --typescript', async () => {
const result = await cli(
'--typescript --out-dir __fixtures_build__/whole __fixtures__',
)
expect(sortCliResult(result)).toMatchSnapshot()
})
it('should suppress output when transforming a directory with a --silent option', async () => {
const result = await cli(
'--silent --out-dir __fixtures_build__/whole __fixtures__',
)
expect(sortCliResult(result)).toMatchSnapshot()
})
it('should support --prettier-config as json', async () => {
const result = await cli(
`--prettier-config '{"tabWidth": 5}' __fixtures__/simple/file.svg`,
)
expect(result).toMatchSnapshot()
})
it('should support --prettier-config as file', async () => {
const result = await cli(
`--prettier-config __fixtures__/withPrettierRc/.prettierrc __fixtures__/simple/file.svg`,
)
expect(result).toMatchSnapshot()
})
it('should support --svgo-config as json', async () => {
const result = await cli(
`--svgo-config '{"plugins":[{"name":"preset-default","params":{"overrides":{"removeTitle":false}}}]}' __fixtures__/simple/file.svg`,
)
expect(result).toMatchSnapshot()
})
it('should support --svgo-config as file', async () => {
const result = await cli(
`--svgo-config __fixtures__/withSvgoConfig/svgo.config.js __fixtures__/simple/file.svg`,
)
expect(result).toMatchSnapshot()
})
it('should support --svgo-config as file with .cjs extension', async () => {
const result = await cli(
`--svgo-config __fixtures__/withSvgoConfig/svgo.config.cjs __fixtures__/simple/file.svg`,
)
expect(result).toMatchSnapshot()
})
it.each([
['--no-dimensions'],
['--jsx-runtime classic-preact'],
['--jsx-runtime automatic'],
['--expand-props none'],
['--expand-props start'],
['--icon'],
['--icon 24'],
['--icon 2em'],
['--native'],
['--native --icon'],
['--native --expand-props none'],
['--native --ref'],
['--ref'],
['--replace-attr-values "#063855=currentColor"'],
[`--svg-props "hidden={true},id=hello"`],
['--no-svgo'],
['--no-prettier'],
['--title-prop'],
['--desc-prop'],
['--typescript'],
['--typescript --ref'],
['--typescript --ref --title-prop'],
['--typescript --ref --desc-prop'],
])(
'should support various args',
async (args) => {
const result = await cli(`${args} -- __fixtures__/simple/file.svg`)
expect(result).toMatchSnapshot(args)
},
10000,
)
it.each([
[0, ''],
[1, '--filename-case=camel'],
[2, '--filename-case=pascal'],
[3, '--filename-case=kebab'],
[4, '--filename-case=snake'],
])(
'should support different filename cases with directory output',
async (index, args) => {
const inDir = '__fixtures__/cased'
const outDir = `__fixtures_build__/filename-case-${index}`
await del(outDir)
await cli(`${args} ${inDir} --out-dir=${outDir}`)
expect(await fs.readdir(outDir)).toMatchSnapshot(args)
},
10000,
)
it('should support custom file extension', async () => {
const inDir = '__fixtures__/simple'
const outDir = '__fixtures_build__/ext'
await del(outDir)
await cli(`--ext=ts ${inDir} --out-dir=${outDir}`)
expect(await fs.readdir(outDir)).toMatchSnapshot()
})
it('should support "--ignore-existing"', async () => {
const inDir = '__fixtures__/simple'
const outDir = '__fixtures__/simple-existing'
await cli(`${inDir} --out-dir=${outDir} --ignore-existing`)
const content = await fs.readFile(path.join(outDir, 'File.js'), 'utf-8')
expect(content).toBe('// nothing')
})
it('should not override config with cli defaults', async () => {
const result = await cli(
'__fixtures__/simple/file.svg --config-file=__fixtures__/overrides.config.js',
)
expect(result).toMatchSnapshot()
})
it('should add Svg prefix to index.js exports staring with number', async () => {
const inDir = '__fixtures__/numeric'
const outDir = `__fixtures_build__/prefix-exports`
await del(outDir)
await cli(`${inDir} --out-dir=${outDir}`)
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
})
it('should support custom index.js with directory output', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/custom-index`
await del(outDir)
await cli(
`${inDir} --out-dir=${outDir} --config-file=__fixtures__/custom-index.config.js`,
)
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
})
it('using typescript option, it creates index with `.ts` extension', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/ts-index`
await del(outDir)
await cli(`${inDir} --out-dir=${outDir} --typescript`)
const content = await fs.readFile(path.join(outDir, 'index.ts'), 'utf-8')
expect(content).toMatchSnapshot()
})
it('should support --index-template in cli', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/custom-index-arg`
await del(outDir)
await cli(
`${inDir} --out-dir=${outDir} --index-template=__fixtures__/custom-index-template.js`,
)
const content = await fs.readFile(path.join(outDir, 'index.js'), 'utf-8')
expect(content).toMatchSnapshot()
})
it('should support --no-index', async () => {
const inDir = '__fixtures__/simple'
const outDir = `__fixtures_build__/no-index-case`
await del(outDir)
await cli(`--no-index ${inDir} --out-dir=${outDir}`)
expect(await fs.readdir(outDir)).toMatchSnapshot()
})
})