Skip to content

Commit b25189a

Browse files
alistairjcbrownTheLarkInn
authored andcommitted
Add tests for RequireJsStuffPlugin (webpack#3702)
1 parent 2bccec7 commit b25189a

File tree

3 files changed

+229
-19
lines changed

3 files changed

+229
-19
lines changed

test/RequireJsStuffPlugin.test.js

Lines changed: 209 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,209 @@
1+
var should = require("should");
2+
var sinon = require("sinon");
3+
var RequireJsStuffPlugin = require("../lib/RequireJsStuffPlugin");
4+
var applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
5+
var PluginEnvironment = require("./helpers/PluginEnvironment");
6+
7+
describe("RequireJsStuffPlugin", function() {
8+
it("has apply function", function() {
9+
(new RequireJsStuffPlugin()).apply.should.be.a.Function();
10+
});
11+
12+
describe("when applied", function() {
13+
var eventBindings, eventBinding;
14+
15+
beforeEach(function() {
16+
eventBindings = applyPluginWithOptions(RequireJsStuffPlugin);
17+
});
18+
19+
it("binds one event handler", function() {
20+
eventBindings.length.should.be.exactly(1);
21+
});
22+
23+
describe("compilation handler", function() {
24+
beforeEach(function() {
25+
eventBinding = eventBindings[0];
26+
});
27+
28+
it("binds to compilation event", function() {
29+
eventBinding.name.should.be.exactly("compilation");
30+
});
31+
32+
describe('when called', function() {
33+
var pluginEnvironment, compilationEventBindings, compilation;
34+
35+
beforeEach(function() {
36+
pluginEnvironment = new PluginEnvironment();
37+
compilation = {
38+
dependencyFactories: {
39+
set: sinon.spy()
40+
},
41+
dependencyTemplates: {
42+
set: sinon.spy()
43+
}
44+
};
45+
var params = {
46+
normalModuleFactory: pluginEnvironment.getEnvironmentStub()
47+
};
48+
eventBinding.handler(compilation, params);
49+
compilationEventBindings = pluginEnvironment.getEventBindings();
50+
});
51+
52+
it('sets the dependency factory', function() {
53+
compilation.dependencyFactories.set.callCount.should.be.exactly(1);
54+
});
55+
56+
it('sets the dependency template', function() {
57+
compilation.dependencyTemplates.set.callCount.should.be.exactly(1);
58+
});
59+
60+
it("binds one event handler", function() {
61+
compilationEventBindings.length.should.be.exactly(1);
62+
});
63+
64+
describe("parser handler", function() {
65+
var parser, parserEventBindings;
66+
67+
beforeEach(function() {
68+
compilationEventBinding = compilationEventBindings[0];
69+
pluginEnvironment = new PluginEnvironment();
70+
parser = pluginEnvironment.getEnvironmentStub();
71+
});
72+
73+
it("binds to parser event", function() {
74+
compilationEventBinding.name.should.be.exactly("parser");
75+
});
76+
77+
describe('when called with parser options of requirejs as false', function() {
78+
beforeEach(function() {
79+
compilationEventBinding.handler(parser, {
80+
requireJs: false
81+
});
82+
parserEventBindings = pluginEnvironment.getEventBindings();
83+
});
84+
85+
it("binds no event handlers", function() {
86+
parserEventBindings.length.should.be.exactly(0);
87+
});
88+
});
89+
90+
describe('when called with empty parser options', function() {
91+
var parserEventBinding, parserEventContext, expressionMock;
92+
93+
beforeEach(function() {
94+
parserEventContext = {
95+
state: {
96+
current: {
97+
addDependency: sinon.spy()
98+
}
99+
}
100+
};
101+
expressionMock = {
102+
range: 10,
103+
loc: 5
104+
};
105+
compilationEventBinding.handler(parser, {});
106+
parserEventBindings = pluginEnvironment.getEventBindings();
107+
});
108+
109+
it("binds four event handlers", function() {
110+
parserEventBindings.length.should.be.exactly(4);
111+
});
112+
113+
describe("'call require.config' handler", function() {
114+
beforeEach(function() {
115+
parserEventBinding = parserEventBindings[0];
116+
});
117+
118+
it("binds to 'call require.config' event", function() {
119+
parserEventBinding.name.should.be.exactly("call require.config");
120+
});
121+
122+
describe('when called', function() {
123+
beforeEach(function() {
124+
parserEventBinding.handler.call(parserEventContext, expressionMock);
125+
});
126+
127+
it('adds dependency to current state', function() {
128+
var addDependencySpy = parserEventContext.state.current.addDependency;
129+
var addedDependency = JSON.stringify(addDependencySpy.getCall(0).args[0]);
130+
addDependencySpy.callCount.should.be.exactly(1);
131+
addedDependency.should.be.exactly('{"module":null,"expression":";","range":10,"loc":5}');
132+
});
133+
});
134+
});
135+
136+
describe("'call requirejs.config' handler", function() {
137+
beforeEach(function() {
138+
parserEventBinding = parserEventBindings[1];
139+
});
140+
141+
it("binds to 'call requirejs.config' event", function() {
142+
parserEventBinding.name.should.be.exactly("call requirejs.config");
143+
});
144+
145+
describe('when called', function() {
146+
beforeEach(function() {
147+
parserEventBinding.handler.call(parserEventContext, expressionMock);
148+
});
149+
150+
it('adds dependency to current state', function() {
151+
var addDependencySpy = parserEventContext.state.current.addDependency;
152+
var addedDependency = JSON.stringify(addDependencySpy.getCall(0).args[0]);
153+
addDependencySpy.callCount.should.be.exactly(1);
154+
addedDependency.should.be.exactly('{"module":null,"expression":";","range":10,"loc":5}');
155+
});
156+
});
157+
});
158+
159+
describe("'expression require.version' handler", function() {
160+
beforeEach(function() {
161+
parserEventBinding = parserEventBindings[2];
162+
});
163+
164+
it("binds to 'expression require.version' event", function() {
165+
parserEventBinding.name.should.be.exactly("expression require.version");
166+
});
167+
168+
describe('when called', function() {
169+
beforeEach(function() {
170+
parserEventBinding.handler.call(parserEventContext, expressionMock);
171+
});
172+
173+
it('adds dependency to current state', function() {
174+
var addDependencySpy = parserEventContext.state.current.addDependency;
175+
var addedDependency = JSON.stringify(addDependencySpy.getCall(0).args[0]);
176+
addDependencySpy.callCount.should.be.exactly(1);
177+
addedDependency.should.be.exactly('{"module":null,"expression":"\\"0.0.0\\"","range":10,"loc":5}');
178+
});
179+
});
180+
});
181+
182+
describe("'expression requirejs.onError' handler", function() {
183+
beforeEach(function() {
184+
parserEventBinding = parserEventBindings[3];
185+
});
186+
187+
it("binds to 'expression requirejs.onError' event", function() {
188+
parserEventBinding.name.should.be.exactly("expression requirejs.onError");
189+
});
190+
191+
describe('when called', function() {
192+
beforeEach(function() {
193+
parserEventBinding.handler.call(parserEventContext, expressionMock);
194+
});
195+
196+
it('adds dependency to current state', function() {
197+
var addDependencySpy = parserEventContext.state.current.addDependency;
198+
var addedDependency = JSON.stringify(addDependencySpy.getCall(0).args[0]);
199+
addDependencySpy.callCount.should.be.exactly(1);
200+
addedDependency.should.be.exactly('{"module":null,"expression":"\\"__webpack_require__.oe\\"","range":10,"loc":5}');
201+
});
202+
});
203+
});
204+
});
205+
});
206+
});
207+
});
208+
});
209+
});

test/helpers/PluginEnvironment.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module.exports = function PluginEnvironment() {
2+
var events = [];
3+
4+
this.getEnvironmentStub = function() {
5+
return {
6+
plugin: function(name, handler) {
7+
events.push({
8+
name,
9+
handler
10+
});
11+
}
12+
};
13+
};
14+
15+
this.getEventBindings = function() {
16+
return events;
17+
};
18+
};
Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,8 @@
1-
function PluginEnvironment() {
2-
var events = [];
3-
4-
this.getCompilerStub = function() {
5-
return {
6-
plugin: function(name, handler) {
7-
events.push({
8-
name,
9-
handler
10-
});
11-
}
12-
};
13-
};
14-
15-
this.getEventBindings = function() {
16-
return events;
17-
};
18-
}
1+
var PluginEnvironment = require('./PluginEnvironment');
192

203
module.exports = function applyPluginWithOptions(Plugin, options) {
214
var plugin = new Plugin(options);
225
var pluginEnvironment = new PluginEnvironment();
23-
plugin.apply(pluginEnvironment.getCompilerStub());
6+
plugin.apply(pluginEnvironment.getEnvironmentStub());
247
return pluginEnvironment.getEventBindings();
258
};

0 commit comments

Comments
 (0)