Skip to content

Commit ef05c31

Browse files
Add tests for SourceMapDevToolModuleOptionsPlugin
1 parent 11c2865 commit ef05c31

File tree

2 files changed

+162
-0
lines changed

2 files changed

+162
-0
lines changed
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
var should = require("should");
2+
var SourceMapDevToolModuleOptionsPlugin = require("../lib/SourceMapDevToolModuleOptionsPlugin");
3+
var applyPluginWithOptions = require('./helpers/applyPluginWithOptions');
4+
5+
describe("SourceMapDevToolModuleOptionsPlugin", function() {
6+
it("has apply function", function() {
7+
(new SourceMapDevToolModuleOptionsPlugin()).apply.should.be.a.Function();
8+
});
9+
10+
describe("when applied", function() {
11+
var eventBindings;
12+
13+
beforeEach(function() {
14+
eventBindings = undefined;
15+
});
16+
17+
describe("with module false and line-to-line false", function() {
18+
beforeEach(function() {
19+
eventBindings = applyPluginWithOptions(SourceMapDevToolModuleOptionsPlugin, {
20+
module: false,
21+
lineToLine: false
22+
});
23+
});
24+
25+
it("does not bind any event handlers", function() {
26+
eventBindings.length.should.be.exactly(0);
27+
});
28+
});
29+
30+
describe("with module true", function() {
31+
beforeEach(function() {
32+
eventBindings = applyPluginWithOptions(SourceMapDevToolModuleOptionsPlugin, {
33+
module: true,
34+
lineToLine: false
35+
});
36+
});
37+
38+
it("binds one event handler", function() {
39+
eventBindings.length.should.be.exactly(1);
40+
});
41+
42+
describe("event handler", function() {
43+
it("binds to build-module event", function() {
44+
eventBindings[0].name.should.be.exactly("build-module");
45+
});
46+
47+
it("sets source map flag", function() {
48+
var module = {};
49+
eventBindings[0].handler(module);
50+
module.should.deepEqual({
51+
useSourceMap: true
52+
});
53+
});
54+
});
55+
});
56+
57+
describe("with line-to-line true", function() {
58+
beforeEach(function() {
59+
eventBindings = applyPluginWithOptions(SourceMapDevToolModuleOptionsPlugin, {
60+
module: false,
61+
lineToLine: true
62+
});
63+
});
64+
65+
it("binds one event handler", function() {
66+
eventBindings.length.should.be.exactly(1);
67+
});
68+
69+
describe("event handler", function() {
70+
it("binds to build-module event", function() {
71+
eventBindings[0].name.should.be.exactly("build-module");
72+
});
73+
74+
it("sets line-to-line flag", function() {
75+
var module = {};
76+
eventBindings[0].handler(module);
77+
module.should.deepEqual({
78+
lineToLine: true
79+
});
80+
});
81+
});
82+
});
83+
84+
describe("with line-to-line object", function() {
85+
beforeEach(function() {
86+
eventBindings = applyPluginWithOptions(SourceMapDevToolModuleOptionsPlugin, {
87+
module: false,
88+
lineToLine: {}
89+
});
90+
});
91+
92+
it("binds one event handler", function() {
93+
eventBindings.length.should.be.exactly(1);
94+
});
95+
96+
describe("event handler", function() {
97+
it("binds to build-module event", function() {
98+
eventBindings[0].name.should.be.exactly("build-module");
99+
});
100+
101+
describe("when module has no resource", function() {
102+
it("makes no changes", function() {
103+
var module = {};
104+
eventBindings[0].handler(module);
105+
module.should.deepEqual({});
106+
});
107+
});
108+
109+
describe("when module has a resource", function() {
110+
it("sets line-to-line flag", function() {
111+
var module = {
112+
resource: "foo"
113+
};
114+
eventBindings[0].handler(module);
115+
module.should.deepEqual({
116+
lineToLine: true,
117+
resource: "foo"
118+
});
119+
});
120+
});
121+
122+
describe("when module has a resource with query", function() {
123+
it("sets line-to-line flag", function() {
124+
var module = {
125+
resource: "foo?bar"
126+
};
127+
eventBindings[0].handler(module);
128+
module.should.deepEqual({
129+
lineToLine: true,
130+
resource: "foo?bar"
131+
});
132+
});
133+
});
134+
});
135+
});
136+
});
137+
});
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
}
19+
20+
module.exports = function applyPluginWithOptions(Plugin, options) {
21+
var plugin = new Plugin(options);
22+
var pluginEnvironment = new PluginEnvironment();
23+
plugin.apply(pluginEnvironment.getCompilerStub());
24+
return pluginEnvironment.getEventBindings();
25+
};

0 commit comments

Comments
 (0)