Skip to content

Commit 405a4ae

Browse files
committed
add test to contextreplacemntplugin
1 parent ecc2adb commit 405a4ae

1 file changed

Lines changed: 239 additions & 0 deletions

File tree

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

0 commit comments

Comments
 (0)