|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +const should = require("should"); |
| 4 | +const sinon = require("sinon"); |
| 5 | +const ConcatSource = require("webpack-sources").ConcatSource; |
| 6 | +const WebWorkerChunkTemplatePlugin = require("../lib/webworker/WebWorkerChunkTemplatePlugin"); |
| 7 | +const applyPluginWithOptions = require("./helpers/applyPluginWithOptions"); |
| 8 | + |
| 9 | +describe("WebWorkerChunkTemplatePlugin", () => { |
| 10 | + let handlerContext; |
| 11 | + |
| 12 | + beforeEach(() => { |
| 13 | + handlerContext = { |
| 14 | + outputOptions: { |
| 15 | + chunkCallbackName: "Foo", |
| 16 | + library: "Bar" |
| 17 | + } |
| 18 | + }; |
| 19 | + }); |
| 20 | + |
| 21 | + it("has apply function", () => { |
| 22 | + (new WebWorkerChunkTemplatePlugin()).apply.should.be.a.Function(); |
| 23 | + }); |
| 24 | + |
| 25 | + describe("when applied", () => { |
| 26 | + let eventBindings, eventBinding; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + eventBindings = applyPluginWithOptions(WebWorkerChunkTemplatePlugin); |
| 30 | + }); |
| 31 | + |
| 32 | + it("binds two event handlers", () => { |
| 33 | + eventBindings.length.should.be.exactly(2); |
| 34 | + }); |
| 35 | + |
| 36 | + describe("render handler", () => { |
| 37 | + beforeEach(() => { |
| 38 | + eventBinding = eventBindings[0]; |
| 39 | + }); |
| 40 | + |
| 41 | + it("binds to render event", () => { |
| 42 | + eventBinding.name.should.be.exactly("render"); |
| 43 | + }); |
| 44 | + |
| 45 | + describe("with chunk call back name set", () => { |
| 46 | + it("creates source wrapper with function name", () => { |
| 47 | + const source = eventBinding.handler.call(handlerContext, "modules()", { |
| 48 | + ids: 100, |
| 49 | + }); |
| 50 | + source.should.be.instanceof(ConcatSource); |
| 51 | + source.source().should.be.exactly("Foo(100,modules())"); |
| 52 | + }); |
| 53 | + }); |
| 54 | + |
| 55 | + describe("without chunk call back name set", () => { |
| 56 | + it("creates source wrapper with library name", () => { |
| 57 | + delete handlerContext.outputOptions.chunkCallbackName; |
| 58 | + const source = eventBinding.handler.call(handlerContext, "modules()", { |
| 59 | + ids: 100, |
| 60 | + }); |
| 61 | + source.should.be.instanceof(ConcatSource); |
| 62 | + source.source().should.be.exactly("webpackChunkBar(100,modules())"); |
| 63 | + }); |
| 64 | + }); |
| 65 | + }); |
| 66 | + |
| 67 | + describe("hash handler", () => { |
| 68 | + var hashMock; |
| 69 | + |
| 70 | + beforeEach(() => { |
| 71 | + eventBinding = eventBindings[1]; |
| 72 | + hashMock = { |
| 73 | + update: sinon.spy() |
| 74 | + }; |
| 75 | + }); |
| 76 | + |
| 77 | + it("binds to hash event", () => { |
| 78 | + eventBinding.name.should.be.exactly("hash"); |
| 79 | + }); |
| 80 | + |
| 81 | + it("updates hash object", () => { |
| 82 | + eventBinding.handler.call(handlerContext, hashMock); |
| 83 | + hashMock.update.callCount.should.be.exactly(4); |
| 84 | + sinon.assert.calledWith(hashMock.update, "webworker"); |
| 85 | + sinon.assert.calledWith(hashMock.update, "3"); |
| 86 | + sinon.assert.calledWith(hashMock.update, "Foo"); |
| 87 | + sinon.assert.calledWith(hashMock.update, "Bar"); |
| 88 | + }); |
| 89 | + }); |
| 90 | + }); |
| 91 | +}); |
0 commit comments