2018-04-16 23:43:45 +08:00
|
|
|
const FakeDocument = require("../../../helpers/FakeDocument");
|
|
|
|
|
|
|
|
let oldNonce;
|
|
|
|
let oldPublicPath;
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
oldNonce = __webpack_nonce__;
|
|
|
|
oldPublicPath = __webpack_public_path__;
|
2018-04-19 21:54:09 +08:00
|
|
|
global.document = new FakeDocument(undefined, false);
|
2018-04-19 03:10:21 +08:00
|
|
|
global.window = {};
|
2018-04-16 23:43:45 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
delete global.document;
|
2018-04-19 03:10:21 +08:00
|
|
|
delete global.window;
|
2018-04-16 23:43:45 +08:00
|
|
|
__webpack_nonce__ = oldNonce;
|
|
|
|
__webpack_public_path__ = oldPublicPath;
|
|
|
|
})
|
|
|
|
|
2018-04-19 21:54:09 +08:00
|
|
|
it("should prefetch and preload child chunks on chunk load", (done) => {
|
2018-04-16 23:43:45 +08:00
|
|
|
__webpack_nonce__ = "nonce";
|
|
|
|
__webpack_public_path__ = "/public/path/";
|
|
|
|
|
2018-05-31 01:17:16 +08:00
|
|
|
const promise = import(/* webpackChunkName: "chunk1" */ "./chunk1");
|
2018-04-19 01:54:24 +08:00
|
|
|
expect(document.head._children).toHaveLength(2);
|
2018-04-16 23:43:45 +08:00
|
|
|
const script = document.head._children[0];
|
2018-04-19 01:54:24 +08:00
|
|
|
expect(script._type).toBe("script");
|
|
|
|
expect(script.src).toBe("/public/path/chunk1.js")
|
|
|
|
expect(script.getAttribute("nonce")).toBe("nonce")
|
|
|
|
expect(script.crossOrigin).toBe("anonymous");
|
2018-04-19 03:10:21 +08:00
|
|
|
expect(script.onload).toBeTypeOf("function");
|
2018-04-16 23:43:45 +08:00
|
|
|
|
2018-04-17 01:35:02 +08:00
|
|
|
let link = document.head._children[1];
|
2018-04-19 01:54:24 +08:00
|
|
|
expect(link._type).toBe("link");
|
|
|
|
expect(link.rel).toBe("preload");
|
|
|
|
expect(link.as).toBe("script");
|
|
|
|
expect(link.href).toBe("/public/path/chunk1-b.js");
|
|
|
|
expect(link.charset).toBe("utf-8");
|
|
|
|
expect(link.getAttribute("nonce")).toBe("nonce");
|
|
|
|
expect(link.crossOrigin).toBe("anonymous");
|
2018-04-17 01:35:02 +08:00
|
|
|
|
2018-04-16 23:43:45 +08:00
|
|
|
__non_webpack_require__("./chunk1.js");
|
|
|
|
script.onload();
|
|
|
|
|
|
|
|
return promise.then((ex) => {
|
2018-04-19 01:54:24 +08:00
|
|
|
expect(document.head._children).toHaveLength(4);
|
2018-04-16 23:43:45 +08:00
|
|
|
|
2018-04-17 01:35:02 +08:00
|
|
|
let link = document.head._children[2];
|
2018-04-19 01:54:24 +08:00
|
|
|
expect(link._type).toBe("link");
|
|
|
|
expect(link.rel).toBe("prefetch");
|
|
|
|
expect(link.href).toBe("/public/path/chunk1-c.js");
|
2018-04-16 23:43:45 +08:00
|
|
|
|
|
|
|
link = document.head._children[3];
|
2018-04-19 01:54:24 +08:00
|
|
|
expect(link._type).toBe("link");
|
|
|
|
expect(link.rel).toBe("prefetch");
|
|
|
|
expect(link.href).toBe("/public/path/chunk1-a.js");
|
2018-04-19 21:54:09 +08:00
|
|
|
done();
|
|
|
|
}, done);
|
2018-04-16 23:43:45 +08:00
|
|
|
})
|