|
| 1 | +var should = require("should"); |
| 2 | +var sinon = require("sinon"); |
| 3 | +var WebWorkerMainTemplatePlugin = require("../lib/webworker/WebWorkerMainTemplatePlugin"); |
| 4 | +var applyPluginWithOptions = require("./helpers/applyPluginWithOptions"); |
| 5 | + |
| 6 | +describe("WebWorkerMainTemplatePlugin", function() { |
| 7 | + var env; |
| 8 | + |
| 9 | + beforeEach(function() { |
| 10 | + env = {}; |
| 11 | + }); |
| 12 | + |
| 13 | + it("has apply function", function() { |
| 14 | + (new WebWorkerMainTemplatePlugin()).apply.should.be.a.Function(); |
| 15 | + }); |
| 16 | + |
| 17 | + describe("when applied", function() { |
| 18 | + beforeEach(function() { |
| 19 | + env.eventBindings = applyPluginWithOptions(WebWorkerMainTemplatePlugin); |
| 20 | + env.handlerContext = { |
| 21 | + requireFn: 'requireFn', |
| 22 | + indent: (value) => typeof value === 'string' ? value : value.join("\n"), |
| 23 | + asString: (values) => values.join("\n"), |
| 24 | + renderCurrentHashCode: (value) => value, |
| 25 | + outputOptions: { |
| 26 | + chunkFilename: 'chunkFilename' |
| 27 | + }, |
| 28 | + applyPluginsWaterfall: (moduleName, fileName, data) => { |
| 29 | + return '"' + moduleName + data.hash + data.hashWithLength() + (data.chunk && data.chunk.id || '') + '"'; |
| 30 | + }, |
| 31 | + renderAddModule: () => 'renderAddModuleSource();', |
| 32 | + }; |
| 33 | + }); |
| 34 | + |
| 35 | + it("binds five event handlers", function() { |
| 36 | + env.eventBindings.length.should.be.exactly(5); |
| 37 | + }); |
| 38 | + |
| 39 | + describe("local-vars handler", function() { |
| 40 | + beforeEach(function() { |
| 41 | + env.eventBinding = env.eventBindings[0]; |
| 42 | + }); |
| 43 | + |
| 44 | + it("binds to local-vars event", function() { |
| 45 | + env.eventBinding.name.should.be.exactly("local-vars"); |
| 46 | + }); |
| 47 | + |
| 48 | + describe("when no chunks are provided", function() { |
| 49 | + beforeEach(function() { |
| 50 | + var chunk = { |
| 51 | + ids: [], |
| 52 | + chunks: [] |
| 53 | + }; |
| 54 | + env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk); |
| 55 | + }); |
| 56 | + |
| 57 | + it("returns the original source", function() { |
| 58 | + env.source.should.be.exactly("moduleSource()") |
| 59 | + }); |
| 60 | + }); |
| 61 | + |
| 62 | + describe("when chunks are provided", function() { |
| 63 | + beforeEach(function() { |
| 64 | + var chunk = { |
| 65 | + ids: [1, 2, 3], |
| 66 | + chunks: [ |
| 67 | + 'foo', |
| 68 | + 'bar', |
| 69 | + 'baz' |
| 70 | + ] |
| 71 | + }; |
| 72 | + env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk, 'abc123'); |
| 73 | + }); |
| 74 | + |
| 75 | + it("returns the original source with installed mapping", function() { |
| 76 | + env.source.should.be.exactly(` |
| 77 | +moduleSource() |
| 78 | +
|
| 79 | +// object to store loaded chunks |
| 80 | +// "1" means "already loaded" |
| 81 | +var installedChunks = { |
| 82 | +1: 1, |
| 83 | +2: 1, |
| 84 | +3: 1 |
| 85 | +}; |
| 86 | +`.trim()) |
| 87 | + }); |
| 88 | + }); |
| 89 | + }); |
| 90 | + |
| 91 | + describe("require-ensure handler", function() { |
| 92 | + beforeEach(function() { |
| 93 | + env.eventBinding = env.eventBindings[1]; |
| 94 | + }); |
| 95 | + |
| 96 | + it("binds to require-ensure event", function() { |
| 97 | + env.eventBinding.name.should.be.exactly("require-ensure"); |
| 98 | + }); |
| 99 | + |
| 100 | + describe("when called", function() { |
| 101 | + beforeEach(function() { |
| 102 | + var chunk = {}; |
| 103 | + env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk, 'abc123'); |
| 104 | + }); |
| 105 | + |
| 106 | + it("creates import scripts call and promise resolve", function() { |
| 107 | + env.source.should.be.exactly(` |
| 108 | +// "1" is the signal for "already loaded" |
| 109 | +if(!installedChunks[chunkId]) { |
| 110 | +importScripts("asset-path" + abc123 + "" + abc123 + "" + chunkId + ""); |
| 111 | +} |
| 112 | +return Promise.resolve(); |
| 113 | +`.trim()) |
| 114 | + }); |
| 115 | + }); |
| 116 | + }); |
| 117 | + |
| 118 | + describe("bootstrap handler", function() { |
| 119 | + beforeEach(function() { |
| 120 | + env.eventBinding = env.eventBindings[2]; |
| 121 | + }); |
| 122 | + |
| 123 | + it("binds to bootstrap event", function() { |
| 124 | + env.eventBinding.name.should.be.exactly("bootstrap"); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("when no chunks are provided", function() { |
| 128 | + beforeEach(function() { |
| 129 | + var chunk = { |
| 130 | + ids: [], |
| 131 | + chunks: [] |
| 132 | + }; |
| 133 | + env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk); |
| 134 | + }); |
| 135 | + |
| 136 | + it("returns the original source", function() { |
| 137 | + env.source.should.be.exactly("moduleSource()") |
| 138 | + }); |
| 139 | + }); |
| 140 | + |
| 141 | + describe("when chunks are provided", function() { |
| 142 | + beforeEach(function() { |
| 143 | + var chunk = { |
| 144 | + ids: [1, 2, 3], |
| 145 | + chunks: [ |
| 146 | + 'foo', |
| 147 | + 'bar', |
| 148 | + 'baz' |
| 149 | + ] |
| 150 | + }; |
| 151 | + env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk); |
| 152 | + }); |
| 153 | + |
| 154 | + it("returns the original source with chunk callback", function() { |
| 155 | + env.source.should.be.exactly(` |
| 156 | +moduleSource() |
| 157 | +this["webpackChunk"] = function webpackChunkCallback(chunkIds, moreModules) { |
| 158 | +for(var moduleId in moreModules) { |
| 159 | +renderAddModuleSource(); |
| 160 | +} |
| 161 | +while(chunkIds.length) |
| 162 | +installedChunks[chunkIds.pop()] = 1; |
| 163 | +}; |
| 164 | +`.trim()) |
| 165 | + }); |
| 166 | + }); |
| 167 | + }); |
| 168 | + |
| 169 | + describe("hot-bootstrap handler", function() { |
| 170 | + beforeEach(function() { |
| 171 | + env.eventBinding = env.eventBindings[3]; |
| 172 | + }); |
| 173 | + |
| 174 | + it("binds to hot-bootstrap event", function() { |
| 175 | + env.eventBinding.name.should.be.exactly("hot-bootstrap"); |
| 176 | + }); |
| 177 | + |
| 178 | + describe("when called", function() { |
| 179 | + beforeEach(function() { |
| 180 | + var chunk = {}; |
| 181 | + env.source = env.eventBinding.handler.call(env.handlerContext, "moduleSource()", chunk, 'abc123'); |
| 182 | + }); |
| 183 | + |
| 184 | + it("returns the original source with hot update callback", function() { |
| 185 | + env.source.should.be.exactly(` |
| 186 | +moduleSource() |
| 187 | +var parentHotUpdateCallback = this["webpackHotUpdate"]; |
| 188 | +this["webpackHotUpdate"] = function webpackHotUpdateCallback(chunkId, moreModules) { // eslint-disable-line no-unused-vars |
| 189 | + hotAddUpdateChunk(chunkId, moreModules); |
| 190 | + if(parentHotUpdateCallback) parentHotUpdateCallback(chunkId, moreModules); |
| 191 | +} ; |
| 192 | +
|
| 193 | +function hotDownloadUpdateChunk(chunkId) { // eslint-disable-line no-unused-vars |
| 194 | + importScripts(requireFn.p + "asset-path" + abc123 + "" + abc123 + "" + chunkId + ""); |
| 195 | +} |
| 196 | +
|
| 197 | +function hotDownloadManifest(callback) { // eslint-disable-line no-unused-vars |
| 198 | + return new Promise(function(resolve, reject) { |
| 199 | + if(typeof XMLHttpRequest === "undefined") |
| 200 | + return reject(new Error("No browser support")); |
| 201 | + try { |
| 202 | + var request = new XMLHttpRequest(); |
| 203 | + var requestPath = requireFn.p + "asset-path" + abc123 + "" + abc123 + ""; |
| 204 | + request.open("GET", requestPath, true); |
| 205 | + request.timeout = 10000; |
| 206 | + request.send(null); |
| 207 | + } catch(err) { |
| 208 | + return reject(err); |
| 209 | + } |
| 210 | + request.onreadystatechange = function() { |
| 211 | + if(request.readyState !== 4) return; |
| 212 | + if(request.status === 0) { |
| 213 | + // timeout |
| 214 | + reject(new Error("Manifest request to " + requestPath + " timed out.")); |
| 215 | + } else if(request.status === 404) { |
| 216 | + // no update available |
| 217 | + resolve(); |
| 218 | + } else if(request.status !== 200 && request.status !== 304) { |
| 219 | + // other failure |
| 220 | + reject(new Error("Manifest request to " + requestPath + " failed.")); |
| 221 | + } else { |
| 222 | + // success |
| 223 | + try { |
| 224 | + var update = JSON.parse(request.responseText); |
| 225 | + } catch(e) { |
| 226 | + reject(e); |
| 227 | + return; |
| 228 | + } |
| 229 | + resolve(update); |
| 230 | + } |
| 231 | + }; |
| 232 | + }); |
| 233 | +} |
| 234 | +
|
| 235 | +function hotDisposeChunk(chunkId) { //eslint-disable-line no-unused-vars |
| 236 | + delete installedChunks[chunkId]; |
| 237 | +} |
| 238 | +`.trim()) |
| 239 | + }); |
| 240 | + }); |
| 241 | + }); |
| 242 | + |
| 243 | + describe("hash handler", function() { |
| 244 | + beforeEach(function() { |
| 245 | + env.eventBinding = env.eventBindings[4]; |
| 246 | + env.handlerContext = { |
| 247 | + outputOptions: { |
| 248 | + publicPath: "Alpha", |
| 249 | + filename: "Bravo", |
| 250 | + chunkFilename: "Charlie", |
| 251 | + chunkCallbackName: "Delta", |
| 252 | + library: "Echo" |
| 253 | + } |
| 254 | + }; |
| 255 | + env.hashMock = { |
| 256 | + update: sinon.spy() |
| 257 | + }; |
| 258 | + env.eventBinding.handler.call(env.handlerContext, env.hashMock); |
| 259 | + }); |
| 260 | + |
| 261 | + it("binds to hash event", function() { |
| 262 | + env.eventBinding.name.should.be.exactly("hash"); |
| 263 | + }); |
| 264 | + |
| 265 | + it("updates hash object", function() { |
| 266 | + env.hashMock.update.callCount.should.be.exactly(7); |
| 267 | + sinon.assert.calledWith(env.hashMock.update, "webworker"); |
| 268 | + sinon.assert.calledWith(env.hashMock.update, "3"); |
| 269 | + sinon.assert.calledWith(env.hashMock.update, "Alpha"); |
| 270 | + sinon.assert.calledWith(env.hashMock.update, "Bravo"); |
| 271 | + sinon.assert.calledWith(env.hashMock.update, "Charlie"); |
| 272 | + sinon.assert.calledWith(env.hashMock.update, "Delta"); |
| 273 | + sinon.assert.calledWith(env.hashMock.update, "Echo"); |
| 274 | + }); |
| 275 | + }); |
| 276 | + }); |
| 277 | +}); |
0 commit comments