Skip to content

Commit 81a7f7f

Browse files
alistairjcbrownTheLarkInn
authored andcommitted
Add tests for LibraryTemplatePlugin (webpack#3835)
1 parent ff30099 commit 81a7f7f

2 files changed

Lines changed: 325 additions & 2 deletions

File tree

‎test/LibraryTemplatePlugin.test.js‎

Lines changed: 323 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,323 @@
1+
var should = require("should");
2+
var sinon = require("sinon");
3+
var LibraryTemplatePlugin = require("../lib/LibraryTemplatePlugin");
4+
var applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
5+
6+
describe("LibraryTemplatePlugin", function() {
7+
var env;
8+
9+
beforeEach(function() {
10+
env = {
11+
compilation: sinon.spy()
12+
};
13+
});
14+
15+
it("has apply function", function() {
16+
(new LibraryTemplatePlugin()).apply.should.be.a.Function();
17+
});
18+
19+
describe("when applied", function() {
20+
beforeEach(function() {
21+
env.eventBindings = applyPluginWithOptions(LibraryTemplatePlugin);
22+
});
23+
24+
it("binds two event handlers", function() {
25+
env.eventBindings.length.should.be.exactly(1);
26+
});
27+
28+
describe("this-compilation handler", function() {
29+
beforeEach(function() {
30+
env.eventBinding = env.eventBindings[0];
31+
});
32+
33+
describe("event handler", function() {
34+
it("binds to this-compilation event", function() {
35+
env.eventBinding.name.should.be.exactly("this-compilation");
36+
});
37+
});
38+
39+
describe("when target is unknown", function() {
40+
beforeEach(function() {
41+
var unknownTarget = "unknownTarget";
42+
env.eventBindings = applyPluginWithOptions(LibraryTemplatePlugin, "foo", unknownTarget, "bar", "baz");
43+
env.eventBinding = env.eventBindings[0];
44+
});
45+
46+
it("throws an error", function() {
47+
should(function() {
48+
env.eventBinding.handler(env.compilation);
49+
}).throw("unknownTarget is not a valid Library target");
50+
});
51+
});
52+
53+
describe("name is a string", function() {
54+
[{
55+
type: "var",
56+
assertion: function(compilationContext) {
57+
compilationContext.varExpression.should.be.exactly("var foo");
58+
should(compilationContext.copyObject).be.undefined();
59+
}
60+
},
61+
{
62+
type: "assign",
63+
assertion: function(compilationContext) {
64+
compilationContext.varExpression.should.be.exactly("foo");
65+
should(compilationContext.copyObject).be.undefined();
66+
}
67+
},
68+
{
69+
type: "this",
70+
assertion: function(compilationContext) {
71+
compilationContext.varExpression.should.be.exactly('this["foo"]');
72+
should(compilationContext.copyObject).be.undefined();
73+
}
74+
},
75+
{
76+
type: "window",
77+
assertion: function(compilationContext) {
78+
compilationContext.varExpression.should.be.exactly('window["foo"]');
79+
should(compilationContext.copyObject).be.undefined();
80+
}
81+
},
82+
{
83+
type: "global",
84+
assertion: function(compilationContext) {
85+
compilationContext.varExpression.should.be.exactly('global["foo"]');
86+
should(compilationContext.copyObject).be.undefined();
87+
}
88+
},
89+
{
90+
type: "commonjs",
91+
assertion: function(compilationContext) {
92+
compilationContext.varExpression.should.be.exactly('exports["foo"]');
93+
should(compilationContext.copyObject).be.undefined();
94+
}
95+
},
96+
{
97+
type: "commonjs2",
98+
assertion: function(compilationContext) {
99+
compilationContext.varExpression.should.be.exactly("module.exports");
100+
should(compilationContext.copyObject).be.undefined();
101+
}
102+
},
103+
{
104+
type: "commonjs-module",
105+
assertion: function(compilationContext) {
106+
compilationContext.varExpression.should.be.exactly("module.exports");
107+
should(compilationContext.copyObject).be.undefined();
108+
}
109+
},
110+
{
111+
type: "amd",
112+
assertion: function(compilationContext) {
113+
compilationContext.name.should.be.exactly("foo");
114+
}
115+
},
116+
{
117+
type: "umd",
118+
assertion: function(compilationContext) {
119+
compilationContext.name.should.be.exactly("foo");
120+
compilationContext.optionalAmdExternalAsGlobal.should.be.false();
121+
compilationContext.namedDefine.should.be.exactly("bar");
122+
compilationContext.auxiliaryComment.should.be.exactly("baz");
123+
}
124+
},
125+
{
126+
type: "umd2",
127+
assertion: function(compilationContext) {
128+
compilationContext.name.should.be.exactly("foo");
129+
compilationContext.optionalAmdExternalAsGlobal.should.be.true();
130+
compilationContext.namedDefine.should.be.exactly("bar");
131+
compilationContext.auxiliaryComment.should.be.exactly("baz");
132+
}
133+
},
134+
{
135+
type: "jsonp",
136+
assertion: function(compilationContext) {
137+
compilationContext.name.should.be.exactly("foo");
138+
}
139+
}
140+
].forEach(function(targetTypeAndAssertion) {
141+
var type = targetTypeAndAssertion.type;
142+
143+
describe("when target is " + type, function() {
144+
beforeEach(function() {
145+
env.eventBindings = applyPluginWithOptions(LibraryTemplatePlugin, "foo", type, "bar", "baz");
146+
env.eventBinding = env.eventBindings[0];
147+
env.eventBinding.handler(env.compilation);
148+
});
149+
150+
it("compilation callback is called", function() {
151+
env.compilation.callCount.should.be.exactly(1);
152+
});
153+
154+
it("compilation callback context is set up", function() {
155+
var compilationContext = env.compilation.firstCall.thisValue;
156+
targetTypeAndAssertion.assertion(compilationContext);
157+
});
158+
});
159+
});
160+
});
161+
162+
describe("name is an array of strings", function() {
163+
[{
164+
type: "var",
165+
assertion: function(compilationContext) {
166+
compilationContext.varExpression.should.be.exactly('var foo = foo || {}; foo["bar"] = foo["bar"] || {}; foo["bar"]["baz"]');
167+
should(compilationContext.copyObject).be.undefined();
168+
}
169+
},
170+
{
171+
type: "assign",
172+
assertion: function(compilationContext) {
173+
compilationContext.varExpression.should.be.exactly('foo = typeof foo === "object" ? foo : {}; foo["bar"] = foo["bar"] || {}; foo["bar"]["baz"]');
174+
should(compilationContext.copyObject).be.undefined();
175+
}
176+
},
177+
{
178+
type: "this",
179+
assertion: function(compilationContext) {
180+
compilationContext.varExpression.should.be.exactly('this["foo"] = this["foo"] || {}; this["foo"]["bar"] = this["foo"]["bar"] || {}; this["foo"]["bar"]["baz"]');
181+
should(compilationContext.copyObject).be.undefined();
182+
}
183+
},
184+
{
185+
type: "window",
186+
assertion: function(compilationContext) {
187+
compilationContext.varExpression.should.be.exactly('window["foo"] = window["foo"] || {}; window["foo"]["bar"] = window["foo"]["bar"] || {}; window["foo"]["bar"]["baz"]');
188+
should(compilationContext.copyObject).be.undefined();
189+
}
190+
},
191+
{
192+
type: "global",
193+
assertion: function(compilationContext) {
194+
compilationContext.varExpression.should.be.exactly('global["foo"] = global["foo"] || {}; global["foo"]["bar"] = global["foo"]["bar"] || {}; global["foo"]["bar"]["baz"]');
195+
should(compilationContext.copyObject).be.undefined();
196+
}
197+
},
198+
{
199+
type: "commonjs",
200+
assertion: function(compilationContext) {
201+
compilationContext.varExpression.should.be.exactly('exports["foo"] = exports["foo"] || {}; exports["foo"]["bar"] = exports["foo"]["bar"] || {}; exports["foo"]["bar"]["baz"]');
202+
should(compilationContext.copyObject).be.undefined();
203+
}
204+
},
205+
{
206+
type: "commonjs2",
207+
assertion: function(compilationContext) {
208+
compilationContext.varExpression.should.be.exactly("module.exports");
209+
should(compilationContext.copyObject).be.undefined();
210+
}
211+
},
212+
{
213+
type: "commonjs-module",
214+
assertion: function(compilationContext) {
215+
compilationContext.varExpression.should.be.exactly("module.exports");
216+
should(compilationContext.copyObject).be.undefined();
217+
}
218+
},
219+
{
220+
type: "amd",
221+
assertion: function(compilationContext) {
222+
compilationContext.name.should.deepEqual(["foo", "bar", "baz"]);
223+
}
224+
},
225+
{
226+
type: "umd",
227+
assertion: function(compilationContext) {
228+
compilationContext.name.should.deepEqual(["foo", "bar", "baz"]);
229+
compilationContext.optionalAmdExternalAsGlobal.should.be.false();
230+
compilationContext.namedDefine.should.be.exactly("bar");
231+
compilationContext.auxiliaryComment.should.be.exactly("baz");
232+
}
233+
},
234+
{
235+
type: "umd2",
236+
assertion: function(compilationContext) {
237+
compilationContext.name.should.deepEqual(["foo", "bar", "baz"]);
238+
compilationContext.optionalAmdExternalAsGlobal.should.be.true();
239+
compilationContext.namedDefine.should.be.exactly("bar");
240+
compilationContext.auxiliaryComment.should.be.exactly("baz");
241+
}
242+
},
243+
{
244+
type: "jsonp",
245+
assertion: function(compilationContext) {
246+
compilationContext.name.should.deepEqual(["foo", "bar", "baz"]);
247+
}
248+
}
249+
].forEach(function(targetTypeAndAssertion) {
250+
var type = targetTypeAndAssertion.type;
251+
252+
describe("when target is " + type, function() {
253+
beforeEach(function() {
254+
env.eventBindings = applyPluginWithOptions(LibraryTemplatePlugin, ["foo", "bar", "baz"], type, "bar", "baz");
255+
env.eventBinding = env.eventBindings[0];
256+
env.eventBinding.handler(env.compilation);
257+
});
258+
259+
it("compilation callback is called", function() {
260+
env.compilation.callCount.should.be.exactly(1);
261+
});
262+
263+
it("compilation callback context is set up", function() {
264+
var compilationContext = env.compilation.firstCall.thisValue;
265+
targetTypeAndAssertion.assertion(compilationContext);
266+
});
267+
});
268+
});
269+
});
270+
271+
describe("name not provided", function() {
272+
[{
273+
type: "this",
274+
assertion: function(compilationContext) {
275+
compilationContext.varExpression.should.be.exactly("this");
276+
should(compilationContext.copyObject).be.true();
277+
}
278+
},
279+
{
280+
type: "window",
281+
assertion: function(compilationContext) {
282+
compilationContext.varExpression.should.be.exactly("window");
283+
should(compilationContext.copyObject).be.true();
284+
}
285+
},
286+
{
287+
type: "global",
288+
assertion: function(compilationContext) {
289+
compilationContext.varExpression.should.be.exactly("global");
290+
should(compilationContext.copyObject).be.true();
291+
}
292+
},
293+
{
294+
type: "commonjs",
295+
assertion: function(compilationContext) {
296+
compilationContext.varExpression.should.be.exactly("exports");
297+
should(compilationContext.copyObject).be.true();
298+
}
299+
}
300+
].forEach(function(targetTypeAndAssertion) {
301+
var type = targetTypeAndAssertion.type;
302+
303+
describe("when target is " + type, function() {
304+
beforeEach(function() {
305+
env.eventBindings = applyPluginWithOptions(LibraryTemplatePlugin, undefined, type, "bar", "baz");
306+
env.eventBinding = env.eventBindings[0];
307+
env.eventBinding.handler(env.compilation);
308+
});
309+
310+
it("compilation callback is called", function() {
311+
env.compilation.callCount.should.be.exactly(1);
312+
});
313+
314+
it("compilation callback context is set up", function() {
315+
var compilationContext = env.compilation.firstCall.thisValue;
316+
targetTypeAndAssertion.assertion(compilationContext);
317+
});
318+
});
319+
});
320+
});
321+
});
322+
});
323+
});

‎test/helpers/applyPluginWithOptions.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var PluginEnvironment = require('./PluginEnvironment');
22

3-
module.exports = function applyPluginWithOptions(Plugin, options) {
4-
var plugin = new Plugin(options);
3+
module.exports = function applyPluginWithOptions(Plugin) {
4+
var plugin = new (Function.prototype.bind.apply(Plugin, arguments));
55
var pluginEnvironment = new PluginEnvironment();
66
plugin.apply(pluginEnvironment.getEnvironmentStub());
77
return pluginEnvironment.getEventBindings();

0 commit comments

Comments
 (0)