/
_index.js
267 lines (226 loc) · 8.99 KB
/
_index.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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
'use strict';
const path = require('path');
const { expect } = require('chai');
const { loadConfig } = require('../../lib/svgo-node.js');
const {
resolvePluginConfig,
extendDefaultPlugins
} = require('../../lib/svgo/config.js');
describe('config', function() {
describe('extend config with object', function() {
var plugins = [
{ name: 'removeDoctype', active: false },
{ name: 'convertColors', params: { shorthex: false } },
{ name: 'removeRasterImages', params: { param: true } }
].map(plugin => resolvePluginConfig(plugin, {}));
const removeDoctype = getPlugin('removeDoctype', plugins);
const convertColors = getPlugin('convertColors', plugins);
const removeRasterImages = getPlugin('removeRasterImages', plugins);
it('removeDoctype plugin should be disabled', function() {
expect(removeDoctype.active).to.be.false;
});
describe('enable plugin with params object', function() {
it('removeRasterImages plugin should be enabled', function() {
expect(removeRasterImages.active).to.be.true;
});
it('removeRasterImages plugin should have property "params"', function() {
expect(removeRasterImages).to.have.property('params');
});
it('"params" should be an instance of Object', function() {
expect(removeRasterImages.params).to.be.an.instanceOf(Object);
});
it('"params" should have property "param" with value of true', function() {
expect(removeRasterImages.params).to.have.property('param', true);
});
});
describe('extend plugin params with object', function() {
it('convertColors plugin should have property "params"', function() {
expect(convertColors).to.have.property('params');
});
it('"params" should be an instance of Object', function() {
expect(convertColors.params).to.be.an.instanceOf(Object);
});
it('"params" should have property "shorthex" with value of false', function() {
expect(convertColors.params).to.have.property('shorthex', false);
});
it('"params" should have property "rgb2hex" with value of true', function() {
expect(convertColors.params).to.have.property('rgb2hex', true);
});
});
});
describe('replace default config with custom', function() {
const config = {
multipass: true,
floatPrecision: 2,
plugins: [
{ name: 'cleanupNumericValues' }
]
}
const plugins = config.plugins.map(plugin => resolvePluginConfig(plugin, config));
const cleanupNumericValues = getPlugin('cleanupNumericValues', plugins);
it('should have "multipass"', function() {
expect(config.multipass).to.be.true;
});
it('config.plugins should have length 1', function() {
expect(plugins).to.have.length(1);
});
it('cleanupNumericValues plugin should be enabled', function() {
expect(cleanupNumericValues.active).to.be.true;
});
it('cleanupNumericValues plugin should have floatPrecision set from parameters', function() {
expect(cleanupNumericValues.params.floatPrecision).to.be.equal(2);
});
});
describe('custom plugins', function() {
describe('extend config with custom plugin', function() {
const plugins = [
{
name: 'aCustomPlugin',
type: 'perItem',
fn: function() { }
}
].map(plugin => resolvePluginConfig(plugin, {}));
const customPlugin = getPlugin('aCustomPlugin', plugins);
it('custom plugin should be enabled', function() {
expect(customPlugin.active).to.be.true;
});
it('custom plugin should have been given a name', function() {
expect(customPlugin.name).to.equal('aCustomPlugin');
});
});
describe('replace default config with custom plugin', function() {
const plugins = [
{
name: 'aCustomPlugin',
type: 'perItem',
fn: function() { }
}
].map(plugin => resolvePluginConfig(plugin, {}));
const customPlugin = getPlugin('aCustomPlugin', plugins);
it('config.plugins should have length 1', function() {
expect(plugins).to.have.length(1);
});
it('custom plugin should be enabled', function() {
expect(customPlugin.active).to.be.true;
});
it('custom plugin should have been given a name', function() {
expect(customPlugin.name).to.equal('aCustomPlugin');
});
});
});
describe('allows to extend default plugins list', () => {
const extendedPlugins = extendDefaultPlugins([
{
name: 'customPlugin',
fn: () => {},
},
{
name: 'removeAttrs',
params: { atts: ['aria-label'] },
},
{
name: 'cleanupIDs',
params: { remove: false },
},
]);
const removeAttrsIndex = extendedPlugins.findIndex(item => item.name === 'removeAttrs');
const cleanupIDsIndex = extendedPlugins.findIndex(item => item.name === 'cleanupIDs');
it('should preserve internal plugins order', () => {
expect(removeAttrsIndex).to.equal(40);
expect(cleanupIDsIndex).to.equal(10);
});
it('should activate inactive by default plugins', () => {
const removeAttrsPlugin = resolvePluginConfig(extendedPlugins[removeAttrsIndex], {});
const cleanupIDsPlugin = resolvePluginConfig(extendedPlugins[cleanupIDsIndex], {});
expect(removeAttrsPlugin.active).to.equal(true);
expect(cleanupIDsPlugin.active).to.equal(true);
});
it('should leave not extended inactive plugins to be inactive', () => {
const inactivePlugin = resolvePluginConfig(
extendedPlugins.find(item => item.name === 'addClassesToSVGElement'),
{},
);
expect(inactivePlugin.active).to.equal(false);
});
it('should put custom plugins in the end', () => {
expect(extendedPlugins[extendedPlugins.length - 1].name).to.equal('customPlugin');
});
});
describe('config', () => {
it('is loaded by absolute path', async () => {
const config = await loadConfig(
path.join(process.cwd(), './test/config/fixtures/one/two/config.js'),
);
expect(config).to.deep.equal({ plugins: [] });
});
it('is loaded by relative path to cwd', async () => {
const config = await loadConfig(
'one/two/config.js',
path.join(process.cwd(), './test/config/fixtures'),
);
expect(config).to.deep.equal({ plugins: [] });
});
it('is searched in cwd and up', async () => {
const config = await loadConfig(
null,
path.join(process.cwd(), './test/config/fixtures/one/two'),
);
expect(config).to.deep.equal({ plugins: [] });
});
it('gives null module does not exist', async () => {
const absoluteConfig = await loadConfig(
path.join(process.cwd(), './test/config/fixtures/config.js'),
);
expect(absoluteConfig).to.equal(null);
const searchedConfig = await loadConfig(
null,
path.join(process.cwd(), './test/config'),
);
expect(searchedConfig).to.equal(null);
});
it('is failed to load when module exports not an object', async () => {
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/invalid-null.js'),
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).to.match(/Invalid config file/);
}
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/invalid-array.js'),
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).to.match(/Invalid config file/);
}
try {
await loadConfig(
path.join(process.cwd(), './test/config/fixtures/invalid-string.js'),
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).to.match(/Invalid config file/);
}
});
it('handles config errors properly', async () => {
try {
await loadConfig(
null,
path.join(process.cwd(), './test/config/fixtures/invalid/config'),
);
expect.fail('Config is loaded successfully');
} catch (error) {
expect(error.message).to.match(/plugins is not defined/);
}
});
});
});
function getPlugin(name, plugins) {
return plugins.find(function(plugin) {
if (plugin.name === name) {
return plugin;
}
});
}