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