|
| 1 | +const should = require("should"); |
| 2 | +const FakeDocument = require("../../../helpers/FakeDocument"); |
| 3 | + |
| 4 | +let oldNonce; |
| 5 | +let oldPublicPath; |
| 6 | + |
| 7 | +beforeEach(() => { |
| 8 | + oldNonce = __webpack_nonce__; |
| 9 | + oldPublicPath = __webpack_public_path__; |
| 10 | + global.document = new FakeDocument(); |
| 11 | +}); |
| 12 | + |
| 13 | +afterEach(() => { |
| 14 | + delete global.document; |
| 15 | + __webpack_nonce__ = oldNonce; |
| 16 | + __webpack_public_path__ = oldPublicPath; |
| 17 | +}) |
| 18 | + |
| 19 | +it("should prefetch and preload child chunks on chunk load", () => { |
| 20 | + __webpack_nonce__ = "nonce"; |
| 21 | + __webpack_public_path__ = "/public/path/"; |
| 22 | + |
| 23 | + const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1"); |
| 24 | + document.head._children.length.should.be.eql(1); |
| 25 | + const script = document.head._children[0]; |
| 26 | + script._type.should.be.eql("script"); |
| 27 | + should(script.src).be.eql("/public/path/chunk1.js") |
| 28 | + should(script.getAttribute("nonce")).be.eql("nonce") |
| 29 | + should(script.crossOrigin).be.eql("anonymous"); |
| 30 | + should(script.onload).be.type("function"); |
| 31 | + |
| 32 | + __non_webpack_require__("./chunk1.js"); |
| 33 | + script.onload(); |
| 34 | + |
| 35 | + return promise.then((ex) => { |
| 36 | + document.head._children.length.should.be.eql(4); |
| 37 | + |
| 38 | + let link = document.head._children[1]; |
| 39 | + link._type.should.be.eql("link"); |
| 40 | + should(link.rel).be.eql("preload"); |
| 41 | + should(link.as).be.eql("script"); |
| 42 | + should(link.href).be.eql("/public/path/chunk1-b.js"); |
| 43 | + should(link.charset).be.eql("utf-8"); |
| 44 | + should(link.getAttribute("nonce")).be.eql("nonce"); |
| 45 | + should(link.crossOrigin).be.eql("anonymous"); |
| 46 | + |
| 47 | + link = document.head._children[2]; |
| 48 | + link._type.should.be.eql("link"); |
| 49 | + should(link.rel).be.eql("prefetch"); |
| 50 | + should(link.href).be.eql("/public/path/chunk1-c.js"); |
| 51 | + |
| 52 | + link = document.head._children[3]; |
| 53 | + link._type.should.be.eql("link"); |
| 54 | + should(link.rel).be.eql("prefetch"); |
| 55 | + should(link.href).be.eql("/public/path/chunk1-a.js"); |
| 56 | + }); |
| 57 | +}) |
0 commit comments