forked from markshapiro/webpack-merge-and-include-globally
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.test.js
executable file
·200 lines (189 loc) · 5.12 KB
/
index.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
jest.mock('fs');
jest.mock('glob');
const fs = require('fs');
const glob = require('glob');
const MergeIntoSingle = require('./index.node6-compatible.js');
// const MergeIntoSingle = require('./index.js');
describe('MergeIntoFile', () => {
const pathToFiles = {
'file1.js': ['1.js'],
'file2.js': ['2.js'],
'*.css': ['3.css', '4.css'],
};
const fileToContent = {
'1.js': 'FILE_1_TEXT',
'2.js': 'FILE_2_TEXT',
'3.css': 'FILE_3_TEXT',
'4.css': 'FILE_4_TEXT',
};
fs.readFile.mockImplementation((fileName, options, cb) => cb(null, fileToContent[fileName]));
glob.mockImplementation((path, options, cb) => cb(null, pathToFiles[path]));
it('should succeed merging using mock content', (done) => {
const instance = new MergeIntoSingle({
files: {
'script.js': [
'file1.js',
'file2.js',
],
'style.css': [
'*.css',
],
},
});
instance.apply({
plugin: (event, fun) => {
const obj = {
assets: {},
};
fun(obj, (err) => {
expect(err).toEqual(undefined);
expect(obj.assets['script.js'].source()).toEqual('FILE_1_TEXT\nFILE_2_TEXT');
expect(obj.assets['style.css'].source()).toEqual('FILE_3_TEXT\nFILE_4_TEXT');
done();
});
},
});
});
it('should succeed merging using mock content with a custom separator', (done) => {
const instance = new MergeIntoSingle({
separator: '\n;\n',
files: {
'script.js': [
'file1.js',
'file2.js',
],
},
});
instance.apply({
plugin: (event, fun) => {
const obj = {
assets: {},
};
fun(obj, (err) => {
expect(err).toEqual(undefined);
expect(obj.assets['script.js'].source()).toEqual('FILE_1_TEXT\n;\nFILE_2_TEXT');
done();
});
},
});
});
it('should succeed merging using mock content with transform', (done) => {
const instance = new MergeIntoSingle({
files: {
'script.js': [
'file1.js',
'file2.js',
],
'style.css': [
'*.css',
],
},
transform: {
'script.js': (val) => `${val.toLowerCase()}`,
},
});
instance.apply({
plugin: (event, fun) => {
const obj = {
assets: {},
};
fun(obj, (err) => {
expect(err).toEqual(undefined);
expect(obj.assets['script.js'].source()).toEqual('file_1_text\nfile_2_text');
expect(obj.assets['style.css'].source()).toEqual('FILE_3_TEXT\nFILE_4_TEXT');
done();
});
},
});
});
it('should succeed merging using mock content with async transform', (done) => {
const instance = new MergeIntoSingle({
files: {
'script.js': [
'file1.js',
'file2.js',
],
'style.css': [
'*.css',
],
},
transform: {
'script.js': async (val) => `${val.toLowerCase()}`,
},
});
instance.apply({
plugin: (event, fun) => {
const obj = {
assets: {},
};
fun(obj, (err) => {
expect(err).toEqual(undefined);
expect(obj.assets['script.js'].source()).toEqual('file_1_text\nfile_2_text');
expect(obj.assets['style.css'].source()).toEqual('FILE_3_TEXT\nFILE_4_TEXT');
done();
});
},
});
});
it('should succeed merging using mock content by using array instead of object', (done) => {
const instance = new MergeIntoSingle({
files: [
{
src: ['file1.js', 'file2.js'],
dest: (val) => ({
'script.js': `${val.toLowerCase()}`,
}),
},
{
src: ['*.css'],
dest: 'style.css',
},
],
});
instance.apply({
plugin: (event, fun) => {
const obj = {
assets: {},
};
fun(obj, (err) => {
expect(err).toEqual(undefined);
expect(obj.assets['script.js'].source()).toEqual('file_1_text\nfile_2_text');
expect(obj.assets['style.css'].source()).toEqual('FILE_3_TEXT\nFILE_4_TEXT');
done();
});
},
});
});
it('should succeed merging using transform file name function', (done) => {
const mockHash = 'xyz';
const instance = new MergeIntoSingle({
files: {
'script.js': [
'file1.js',
'file2.js',
],
'other.deps.js': [
'file1.js',
],
'style.css': [
'*.css',
],
},
transformFileName: (fileNameBase, extension) => `${fileNameBase}${extension}?hash=${mockHash}`,
});
instance.apply({
plugin: (event, fun) => {
const obj = {
assets: {},
};
fun(obj, (err) => {
expect(err).toEqual(undefined);
expect(obj.assets[`script.js?hash=${mockHash}`]).toBeDefined();
expect(obj.assets[`other.deps.js?hash=${mockHash}`]).toBeDefined();
expect(obj.assets[`style.css?hash=${mockHash}`]).toBeDefined();
done();
});
},
});
});
});