Skip to content

Commit 1092ff4

Browse files
authored
Merge pull request webpack#4319 from jerairrest/test-contextreplacementplugin
Increased coverage on ContextReplacementPlugin
2 parents 16b9f73 + 0f45242 commit 1092ff4

1 file changed

Lines changed: 243 additions & 0 deletions

File tree

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
"use strict";
2+
3+
const should = require("should");
4+
const sinon = require("sinon");
5+
const ContextReplacementPlugin = require("../lib/ContextReplacementPlugin");
6+
const applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
7+
const PluginEnvironment = require("./helpers/PluginEnvironment");
8+
9+
describe("ContextReplacementPlugin", () => {
10+
it("has apply function", () => (new ContextReplacementPlugin()).apply.should.be.a.Function());
11+
12+
it("should consume resourceRegExp as regular expression", () => {
13+
let instance = new ContextReplacementPlugin(/selector/, "mock", "mock", "mock");
14+
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
15+
});
16+
17+
it("should consume newContentResource as function", () => {
18+
let instance = new ContextReplacementPlugin(/selector/, () => {}, "mock", "mock");
19+
should(instance.newContentCallback).be.a.Function();
20+
});
21+
22+
it("should consume newContentResource as not an string or function", () => {
23+
let instance = new ContextReplacementPlugin(/selector/, 42, "newContentRecursive", "newContentRegExp");
24+
25+
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
26+
should(instance.newContentResource).be.exactly(undefined);
27+
should(instance.newContentRecursive).be.exactly(undefined);
28+
should(instance.newContentRegExp).be.exactly(42);
29+
});
30+
31+
it("should consume newContentResource as an object", () => {
32+
let instance = new ContextReplacementPlugin(/selector/, "newResource", {
33+
test: "obj"
34+
}, /selector/);
35+
36+
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
37+
should(instance.newContentResource).be.exactly("newResource");
38+
should(instance.newContentRecursive).be.exactly(undefined);
39+
should(instance.newContentRegExp).be.exactly(undefined);
40+
should(instance.newContentCreateContextMap).be.a.Function();
41+
42+
let x = (nothing, obj) => {
43+
should(obj.test).be.exactly("obj")
44+
};
45+
46+
let spy = sinon.spy(x);
47+
48+
instance.newContentCreateContextMap(undefined, spy);
49+
50+
should(spy.called).be.exactly(true)
51+
52+
});
53+
54+
it("should consume newContentResource as an object", () => {
55+
let instance = new ContextReplacementPlugin(/selector/, "newResource", () => {}, /selector/);
56+
57+
should(instance.resourceRegExp instanceof RegExp).be.exactly(true);
58+
should(instance.newContentResource).be.exactly("newResource");
59+
should(instance.newContentRecursive).be.exactly(undefined);
60+
should(instance.newContentRegExp).be.exactly(undefined);
61+
should(instance.newContentCreateContextMap).be.a.Function();
62+
});
63+
64+
describe("when applied", () => {
65+
66+
describe("when before resolve is called", () => {
67+
it("default call", () => {
68+
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
69+
70+
let x = (nothing, result) => {
71+
should(result.request).be.exactly('./folder')
72+
should(result.dependencies[0].critical).be.exactly(false)
73+
should(result.recursive).be.exactly(true)
74+
should(result.regExp instanceof RegExp).be.exactly(true)
75+
};
76+
77+
let spy = sinon.spy(x);
78+
79+
obj.beforeResolve.handler({
80+
request: "selector",
81+
dependencies: [{
82+
critical: true
83+
}]
84+
}, spy)
85+
86+
should(spy.called).be.exactly(true)
87+
});
88+
89+
it("default call with newContentCallback as a function", () => {
90+
let obj = buildPluginWithParams(/selector/, (result) => {
91+
should(result.request).be.exactly('selector')
92+
should(result.dependencies[0].critical).be.exactly(false)
93+
should(result.recursive).be.exactly(undefined)
94+
should(result.regExp).be.exactly(undefined)
95+
}, true, /filter/);
96+
97+
let x = (nothing, result) => {
98+
should(result.request).be.exactly('selector')
99+
should(result.dependencies[0].critical).be.exactly(false)
100+
should(result.recursive).be.exactly(undefined)
101+
should(result.regExp).be.exactly(undefined)
102+
};
103+
104+
let spy = sinon.spy(x);
105+
106+
obj.beforeResolve.handler({
107+
request: "selector",
108+
dependencies: [{
109+
critical: false
110+
}]
111+
}, spy)
112+
113+
should(spy.called).be.exactly(true)
114+
});
115+
116+
it("call when result is false", () => {
117+
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
118+
119+
let x = (nothing, result) => {
120+
should(result).be.Undefined();
121+
};
122+
123+
let spy = sinon.spy(x);
124+
125+
obj.beforeResolve.handler(false, spy);
126+
127+
should(spy.called).be.exactly(true)
128+
});
129+
});
130+
131+
describe("when after resolve is called", () => {
132+
it("default call where regex is correct", () => {
133+
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
134+
135+
let x = (nothing, result) => {
136+
result.resource.should.containEql('selector')
137+
result.resource.should.containEql('folder')
138+
};
139+
140+
let spy = sinon.spy(x);
141+
142+
obj.afterResolve.handler({
143+
resource: "selector",
144+
dependencies: [{
145+
critical: true
146+
}]
147+
}, spy);
148+
149+
should(spy.called).be.exactly(true)
150+
});
151+
152+
it("default call where regex is incorrect", () => {
153+
let obj = buildPluginWithParams(/selector/, "./folder", true, /filter/);
154+
155+
let x = (nothing, result) => {
156+
result.resource.should.containEql('importwontwork')
157+
};
158+
159+
let spy = sinon.spy(x);
160+
161+
obj.afterResolve.handler({
162+
resource: "importwontwork",
163+
dependencies: [{
164+
critical: true
165+
}]
166+
}, spy);
167+
168+
should(spy.called).be.exactly(true)
169+
});
170+
171+
it("default call where regex is correct", () => {
172+
let obj = buildPluginWithParams(/selector/, (result) => {
173+
//noop
174+
}, true, /filter/);
175+
176+
let x = (nothing, result) => {
177+
result.resource.should.equal('selector')
178+
};
179+
180+
let spy = sinon.spy(x);
181+
182+
obj.afterResolve.handler({
183+
resource: "selector",
184+
dependencies: [{
185+
critical: true
186+
}]
187+
}, spy);
188+
189+
should(spy.called).be.exactly(true)
190+
});
191+
192+
it("default call where regex is correct and using function as newContent Resource", () => {
193+
let obj = buildPluginWithParams(/selector/, (result) => {
194+
result.resource = "imadifferentselector"
195+
}, true, /filter/);
196+
197+
let x = (nothing, result) => {
198+
result.resource.should.containEql('selector')
199+
result.resource.should.containEql('imadifferentselector')
200+
};
201+
202+
let spy = sinon.spy(x);
203+
204+
obj.afterResolve.handler({
205+
resource: "selector",
206+
dependencies: [{
207+
critical: true
208+
}]
209+
}, spy);
210+
211+
should(spy.called).be.exactly(true)
212+
});
213+
214+
})
215+
216+
});
217+
});
218+
219+
let buildPluginWithParams = (resourceRegExp, newContentResource, newContentRecursive, newContentRegExp) => {
220+
let instance = new ContextReplacementPlugin(resourceRegExp, newContentResource, newContentRecursive, newContentRegExp);
221+
222+
let pluginEnvironment = new PluginEnvironment();
223+
instance.apply(pluginEnvironment.getEnvironmentStub());
224+
225+
let contextModuleFactory = pluginEnvironment.getEventBindings()[0];
226+
pluginEnvironment.getEventBindings().length.should.be.exactly(1)
227+
228+
let contextModuleFactoryPluginEnv = new PluginEnvironment();
229+
230+
contextModuleFactory.handler(contextModuleFactoryPluginEnv.getEnvironmentStub());
231+
232+
let contextModuleFactoryEventBindings = contextModuleFactoryPluginEnv.getEventBindings();
233+
contextModuleFactoryPluginEnv.getEventBindings().length.should.be.exactly(2);
234+
235+
let beforeResolve = contextModuleFactoryEventBindings[0];
236+
let afterResolve = contextModuleFactoryEventBindings[1];
237+
238+
return {
239+
contextModuleFactory,
240+
beforeResolve,
241+
afterResolve
242+
}
243+
};

0 commit comments

Comments
 (0)