-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
loader.test.js
122 lines (102 loc) · 3.26 KB
/
loader.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
'use strict';
import loader from '@src/loader';
import svgoConfig from '../example/svgo.config';
import { optimize, loadConfig } from 'svgo';
import { validate } from 'schema-utils';
import schemaOptions from '@src/schemas/loader-options.json';
import { PACKAGE_NAME } from '@src/utils';
jest.mock('schema-utils');
jest.mock('@src/utils', () => ({
PACKAGE_NAME: 'svg-chunk-webpack-plugin',
esmResolve: jest.fn()
}));
jest.mock('svgo', () => {
const originalModule = jest.requireActual('svgo');
return {
...originalModule,
optimize: jest.fn(),
loadConfig: jest.fn()
};
});
let _this;
let callback;
beforeEach(() => {
callback = jest.fn();
_this = {
_compiler: {
options: {
plugins: [
{
PLUGIN_NAME: 'svg-chunk-webpack-plugin'
}
]
}
},
_module: {
buildInfo: {},
factoryMeta: {}
},
async: jest.fn().mockReturnValue(callback),
getOptions: jest.fn(),
context: './',
emitError: jest.fn()
};
});
describe('Loader', () => {
beforeEach(() => {
optimize.mockReturnValue({ data: 'svg minified' });
});
afterEach(() => {
expect(_this._module.factoryMeta.sideEffectFree).toBe(false);
expect(_this._module.buildInfo.SVG_CHUNK_WEBPACK_PLUGIN).toBe(true);
expect(_this.getOptions).toHaveBeenCalled();
});
it('Should call the loader function with the config file as string', async () => {
_this.getOptions.mockReturnValue({ configFile: 'svgo.config.js' });
loadConfig.mockReturnValue(svgoConfig);
callback.mockReturnValue('svg stringify');
const result = await loader.call(_this, '<svg></svg>');
expect(validate).toHaveBeenCalledWith(
schemaOptions,
{ configFile: 'svgo.config.js' },
{
name: 'SvgChunkWebpackPlugin Loader',
baseDataPath: 'options'
}
);
expect(loadConfig).toHaveBeenCalledWith('svgo.config.js', './');
expect(optimize).toHaveBeenCalledWith('<svg></svg>', svgoConfig);
expect(callback).toHaveBeenCalledWith(null, JSON.stringify('svg minified'));
expect(result).toStrictEqual('svg stringify');
});
it('Should call the loader function without the config file to load', async () => {
await loader.call(_this, '<svg></svg>');
expect(loadConfig).toHaveBeenCalledWith(null, './');
});
it('Should call the loader function without factoryMeta object data', async () => {
delete _this._module.factoryMeta;
await loader.call(_this, '<svg></svg>');
});
});
describe('Loader with errors', () => {
it('Should call the loader function with a wrong svgo config path', async () => {
_this.getOptions.mockReturnValue({ configFile: 'svgo.confi.js' });
loadConfig.mockRejectedValue('error');
await loader.call(_this, '<svg></svg>');
expect.assertions(1);
expect(_this.emitError).toHaveBeenCalledWith(new Error('Cannot find module svgo.confi.js'));
});
it('Should call the loader function with a wrong SVG', async () => {
await loader.call(_this, 'wrong svg');
expect.assertions(1);
expect(callback).toHaveBeenCalledWith(new Error(`${PACKAGE_NAME} exception. wrong svg`));
});
it('Should call the loader function without the plugin imported', async () => {
_this._compiler.options.plugins = [];
await loader.call(_this, '<svg></svg>');
expect.assertions(1);
expect(callback).toHaveBeenCalledWith(
new Error(`${PACKAGE_NAME} requires the corresponding plugin`)
);
});
});