forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextReplacementPlugin.unittest.js
243 lines (182 loc) · 7.12 KB
/
ContextReplacementPlugin.unittest.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
"use strict";
const should = require("should");
const sinon = require("sinon");
const ContextReplacementPlugin = require("../lib/ContextReplacementPlugin");
const applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
const PluginEnvironment = require("./helpers/PluginEnvironment");
describe("ContextReplacementPlugin", () => {
it("has apply function", () => (new ContextReplacementPlugin()).apply.should.be.a.Function());
it("should consume resourceRegExp as regular expression", () => {
let instance = new ContextReplacementPlugin(/selector/, "mock", "mock", "mock");
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
});
it("should consume newContentResource as function", () => {
let instance = new ContextReplacementPlugin(/selector/, () => {}, "mock", "mock");
should(instance.newContentCallback).be.a.Function();
});
it("should consume newContentResource as not an string or function", () => {
let instance = new ContextReplacementPlugin(/selector/, 42, "newContentRecursive", "newContentRegExp");
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
should(instance.newContentResource).be.exactly(undefined);
should(instance.newContentRecursive).be.exactly(undefined);
should(instance.newContentRegExp).be.exactly(42);
});
it("should consume newContentResource as an object", () => {
let instance = new ContextReplacementPlugin(/selector/, "newResource", {
test: "obj"
}, /selector/);
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
should(instance.newContentResource).be.exactly("newResource");
should(instance.newContentRecursive).be.exactly(undefined);
should(instance.newContentRegExp).be.exactly(undefined);
should(instance.newContentCreateContextMap).be.a.Function();
let x = (nothing, obj) => {
should(obj.test).be.exactly("obj")
};
let spy = sinon.spy(x);
instance.newContentCreateContextMap(undefined, spy);
should(spy.called).be.exactly(true)
});
it("should consume newContentResource as an object", () => {
let instance = new ContextReplacementPlugin(/selector/, "newResource", () => {}, /selector/);
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
should(instance.newContentResource).be.exactly("newResource");
should(instance.newContentRecursive).be.exactly(undefined);
should(instance.newContentRegExp).be.exactly(undefined);
should(instance.newContentCreateContextMap).be.a.Function();
});
describe("when applied", () => {
describe("when before resolve is called", () => {
it("default call", () => {
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
let x = (nothing, result) => {
should(result.request).be.exactly('./folder')
should(result.dependencies[0].critical).be.exactly(false)
should(result.recursive).be.exactly(true)
should(result.regExp instanceof RegExp).be.exactly(true)
};
let spy = sinon.spy(x);
obj.beforeResolve.handler({
request: "selector",
dependencies: [{
critical: true
}]
}, spy)
should(spy.called).be.exactly(true)
});
it("default call with newContentCallback as a function", () => {
let obj = buildPluginWithParams(/selector/, (result) => {
should(result.request).be.exactly('selector')
should(result.dependencies[0].critical).be.exactly(false)
should(result.recursive).be.exactly(undefined)
should(result.regExp).be.exactly(undefined)
}, true, /filter/);
let x = (nothing, result) => {
should(result.request).be.exactly('selector')
should(result.dependencies[0].critical).be.exactly(false)
should(result.recursive).be.exactly(undefined)
should(result.regExp).be.exactly(undefined)
};
let spy = sinon.spy(x);
obj.beforeResolve.handler({
request: "selector",
dependencies: [{
critical: false
}]
}, spy)
should(spy.called).be.exactly(true)
});
it("call when result is false", () => {
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
let x = (nothing, result) => {
should(result).be.Undefined();
};
let spy = sinon.spy(x);
obj.beforeResolve.handler(false, spy);
should(spy.called).be.exactly(true)
});
});
describe("when after resolve is called", () => {
it("default call where regex is correct", () => {
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
let x = (nothing, result) => {
result.resource.should.containEql('selector')
result.resource.should.containEql('folder')
};
let spy = sinon.spy(x);
obj.afterResolve.handler({
resource: "selector",
dependencies: [{
critical: true
}]
}, spy);
should(spy.called).be.exactly(true)
});
it("default call where regex is incorrect", () => {
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
let x = (nothing, result) => {
result.resource.should.containEql('importwontwork')
};
let spy = sinon.spy(x);
obj.afterResolve.handler({
resource: "importwontwork",
dependencies: [{
critical: true
}]
}, spy);
should(spy.called).be.exactly(true)
});
it("default call where regex is correct", () => {
let obj = buildPluginWithParams(/selector/, (result) => {
//noop
}, true, /filter/);
let x = (nothing, result) => {
result.resource.should.equal('selector')
};
let spy = sinon.spy(x);
obj.afterResolve.handler({
resource: "selector",
dependencies: [{
critical: true
}]
}, spy);
should(spy.called).be.exactly(true)
});
it("default call where regex is correct and using function as newContent Resource", () => {
let obj = buildPluginWithParams(/selector/, (result) => {
result.resource = "imadifferentselector"
}, true, /filter/);
let x = (nothing, result) => {
result.resource.should.containEql('selector')
result.resource.should.containEql('imadifferentselector')
};
let spy = sinon.spy(x);
obj.afterResolve.handler({
resource: "selector",
dependencies: [{
critical: true
}]
}, spy);
should(spy.called).be.exactly(true)
});
})
});
});
let buildPluginWithParams = (resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) => {
let instance = new ContextReplacementPlugin(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp);
let pluginEnvironment = new PluginEnvironment();
instance.apply(pluginEnvironment.getEnvironmentStub());
let contextModuleFactory = pluginEnvironment.getEventBindings()[0];
pluginEnvironment.getEventBindings().length.should.be.exactly(1)
let contextModuleFactoryPluginEnv = new PluginEnvironment();
contextModuleFactory.handler(contextModuleFactoryPluginEnv.getEnvironmentStub());
let contextModuleFactoryEventBindings = contextModuleFactoryPluginEnv.getEventBindings();
contextModuleFactoryPluginEnv.getEventBindings().length.should.be.exactly(2);
let beforeResolve = contextModuleFactoryEventBindings[0];
let afterResolve = contextModuleFactoryEventBindings[1];
return {
contextModuleFactory,
beforeResolve,
afterResolve
}
};