-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
svgo-node.test.js
184 lines (168 loc) · 5.25 KB
/
svgo-node.test.js
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
import os from 'os';
import path from 'path';
import { optimize, loadConfig } from './svgo-node.js';
/**
* @typedef {import('../lib/types.js').Plugin} Plugin
*/
const describeLF = os.EOL === '\r\n' ? describe.skip : describe;
const describeCRLF = os.EOL === '\r\n' ? describe : describe.skip;
describeLF('with LF line-endings', () => {
test('should work', () => {
const svg = `
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 120 120">
<desc>
Created with love
</desc>
<circle fill="#ff0000" cx="60" cy="60" r="50"/>
</svg>
`;
const { data } = optimize(svg);
expect(data).toBe(
'<svg viewBox="0 0 120 120"><circle cx="60" cy="60" r="50" fill="red"/></svg>',
);
});
test('should respect config', () => {
const svg = `
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 120 120">
<desc>
Created with love
</desc>
<circle fill="#ff0000" cx="60" cy="60" r="50"/>
</svg>
`;
const { data } = optimize(svg, {
js2svg: { pretty: true, indent: 2 },
});
expect(data).toBe(
'<svg viewBox="0 0 120 120">\n <circle cx="60" cy="60" r="50" fill="red"/>\n</svg>\n',
);
});
test('should respect line-ending config', () => {
const svg = `
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 120 120">
<desc>
Created with love
</desc>
<circle fill="#ff0000" cx="60" cy="60" r="50"/>
</svg>
`;
const { data } = optimize(svg, {
js2svg: { eol: 'crlf', pretty: true, indent: 2 },
});
expect(data).toBe(
'<svg viewBox="0 0 120 120">\r\n <circle cx="60" cy="60" r="50" fill="red"/>\r\n</svg>\r\n',
);
});
});
describeCRLF('with CRLF line-endings', () => {
test('should work', () => {
const svg = `
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 120 120">
<desc>
Created with love
</desc>
<circle fill="#ff0000" cx="60" cy="60" r="50"/>
</svg>
`;
const { data } = optimize(svg);
expect(data).toBe(
'<svg viewBox="0 0 120 120"><circle cx="60" cy="60" r="50" fill="red"/></svg>',
);
});
test('should respect config', () => {
const svg = `
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 120 120">
<desc>
Created with love
</desc>
<circle fill="#ff0000" cx="60" cy="60" r="50"/>
</svg>
`;
const { data } = optimize(svg, {
js2svg: { pretty: true, indent: 2 },
});
expect(data).toBe(
'<svg viewBox="0 0 120 120">\r\n <circle cx="60" cy="60" r="50" fill="red"/>\r\n</svg>\r\n',
);
});
test('should respect line-ending config', () => {
const svg = `
<?xml version="1.0" encoding="utf-8"?>
<svg viewBox="0 0 120 120">
<desc>
Created with love
</desc>
<circle fill="#ff0000" cx="60" cy="60" r="50"/>
</svg>
`;
const { data } = optimize(svg, {
js2svg: { eol: 'lf', pretty: true, indent: 2 },
});
expect(data).toBe(
'<svg viewBox="0 0 120 120">\n <circle cx="60" cy="60" r="50" fill="red"/>\n</svg>\n',
);
});
});
describe('loadConfig', () => {
const cwd = process.cwd();
const fixtures = path.join(cwd, './test/fixtures/config-loader');
test('loads by absolute path', async () => {
expect(
await loadConfig(path.join(fixtures, 'one/two/config.js')),
).toStrictEqual({
plugins: [],
});
});
test('loads by relative path to cwd', async () => {
const config = await loadConfig('one/two/config.js', fixtures);
expect(config).toStrictEqual({ plugins: [] });
});
test('searches in cwd and up', async () => {
expect(
await loadConfig(null, path.join(fixtures, 'one/two')),
).toStrictEqual({
plugins: [],
});
expect(
await loadConfig(null, path.join(cwd, './test/fixtures/missing')),
).toBeNull();
expect(await loadConfig(null, path.join(fixtures, 'mjs'))).toStrictEqual({
plugins: ['mjs'],
});
expect(await loadConfig(null, path.join(fixtures, 'cjs'))).toStrictEqual({
plugins: ['cjs'],
});
});
test('fails when specified config does not exist', async () => {
await expect(loadConfig('{}')).rejects.toThrow(/Cannot find module/);
});
test('fails when exported config not an object', async () => {
await expect(
loadConfig(path.join(fixtures, 'invalid-null.js')),
).rejects.toThrow(/Invalid config file/);
await expect(
loadConfig(path.join(fixtures, 'invalid-array.js')),
).rejects.toThrow(/Invalid config file/);
await expect(
loadConfig(path.join(fixtures, 'invalid-string.js')),
).rejects.toThrow(/Invalid config file/);
});
test('handles runtime errors properly', async () => {
await expect(
loadConfig(path.join(fixtures, 'invalid-runtime.js')),
).rejects.toThrow(/plugins is not defined/);
await expect(
loadConfig(path.join(fixtures, 'invalid-runtime.mjs')),
).rejects.toThrow(/plugins is not defined/);
});
test('handles MODULE_NOT_FOUND properly', async () => {
await expect(
loadConfig(path.join(fixtures, 'module-not-found.js')),
).rejects.toThrow(/Cannot find module 'unknown-module'/);
});
});