forked from webpack/webpack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWebWorkerChunkTemplatePlugin.unittest.js
91 lines (76 loc) · 2.44 KB
/
WebWorkerChunkTemplatePlugin.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
"use strict";
const should = require("should");
const sinon = require("sinon");
const ConcatSource = require("webpack-sources").ConcatSource;
const WebWorkerChunkTemplatePlugin = require("../lib/webworker/WebWorkerChunkTemplatePlugin");
const applyPluginWithOptions = require("./helpers/applyPluginWithOptions");
describe("WebWorkerChunkTemplatePlugin", () => {
let handlerContext;
beforeEach(() => {
handlerContext = {
outputOptions: {
chunkCallbackName: "Foo",
library: "Bar"
}
};
});
it("has apply function", () => {
(new WebWorkerChunkTemplatePlugin()).apply.should.be.a.Function();
});
describe("when applied", () => {
let eventBindings, eventBinding;
beforeEach(() => {
eventBindings = applyPluginWithOptions(WebWorkerChunkTemplatePlugin);
});
it("binds two event handlers", () => {
eventBindings.length.should.be.exactly(2);
});
describe("render handler", () => {
beforeEach(() => {
eventBinding = eventBindings[0];
});
it("binds to render event", () => {
eventBinding.name.should.be.exactly("render");
});
describe("with chunk call back name set", () => {
it("creates source wrapper with function name", () => {
const source = eventBinding.handler.call(handlerContext, "modules()", {
ids: 100,
});
source.should.be.instanceof(ConcatSource);
source.source().should.be.exactly("Foo(100,modules())");
});
});
describe("without chunk call back name set", () => {
it("creates source wrapper with library name", () => {
delete handlerContext.outputOptions.chunkCallbackName;
const source = eventBinding.handler.call(handlerContext, "modules()", {
ids: 100,
});
source.should.be.instanceof(ConcatSource);
source.source().should.be.exactly("webpackChunkBar(100,modules())");
});
});
});
describe("hash handler", () => {
var hashMock;
beforeEach(() => {
eventBinding = eventBindings[1];
hashMock = {
update: sinon.spy()
};
});
it("binds to hash event", () => {
eventBinding.name.should.be.exactly("hash");
});
it("updates hash object", () => {
eventBinding.handler.call(handlerContext, hashMock);
hashMock.update.callCount.should.be.exactly(4);
sinon.assert.calledWith(hashMock.update, "webworker");
sinon.assert.calledWith(hashMock.update, "3");
sinon.assert.calledWith(hashMock.update, "Foo");
sinon.assert.calledWith(hashMock.update, "Bar");
});
});
});
});