// This config need to be set on initial evaluation to be effective
__webpack_nonce__ = "nonce";
__webpack_public_path__ = "https://example.com/public/path/";

it("should prefetch and preload child chunks on chunk load", async () => {
	let link, script;

	expect(document.head._children).toHaveLength(3);

	// Test prefetch from entry chunk
	link = document.head._children[0];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("prefetch");
	expect(link.as).toBe("script");
	expect(link.href).toBe("https://example.com/public/path/chunk1.js");

	link = document.head._children[1];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("prefetch");
	expect(link.as).toBe("style");
	expect(link.href).toBe("https://example.com/public/path/chunk2-css.css");

	link = document.head._children[2];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("prefetch");
	expect(link.as).toBe("script");
	expect(link.href).toBe("https://example.com/public/path/chunk2-css.js");

	const promise = import(
		/* webpackChunkName: "chunk1", webpackPrefetch: true */ "./chunk1.js"
	);

	expect(document.head._children).toHaveLength(7);

	// Test normal script loading
	script = document.head._children[3];
	expect(script._type).toBe("script");
	expect(script.src).toBe("https://example.com/public/path/chunk1.js");
	expect(script.getAttribute("nonce")).toBe("nonce");
	expect(script.crossOrigin).toBe("anonymous");
	expect(script.onload).toBeTypeOf("function");

	// Test preload of chunk1-b
	link = document.head._children[4];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("modulepreload");
	expect(link.href).toBe("https://example.com/public/path/chunk1-b.js");
	expect(link.charset).toBe("utf-8");
	expect(link.getAttribute("nonce")).toBe("nonce");
	expect(link.crossOrigin).toBe("anonymous");

	// Test preload of chunk1-a-css
	link = document.head._children[5];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("preload");
	expect(link.as).toBe("style");
	expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.css");

	link = document.head._children[6];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("modulepreload");
	expect(link.href).toBe("https://example.com/public/path/chunk1-a-css.js");
	expect(link.charset).toBe("utf-8");
	expect(link.getAttribute("nonce")).toBe("nonce");
	expect(link.crossOrigin).toBe("anonymous");

	// Run the script — `await` is required: on older V8 the host import
	// callback for `import(webpackIgnore)` is queued as a microtask, so
	// without awaiting, `script.onload()` would fire before the chunk's
	// `webpackChunk.push` runs and the JSONP runtime would reject with
	// "Loading chunk chunk1 failed".
	await import(/* webpackIgnore: true */ "./chunk1.js");

	script.onload();

	await promise;

	expect(document.head._children).toHaveLength(8);

	// Test prefetching for chunk1-c and chunk1-a in this order
	link = document.head._children[6];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("prefetch");
	expect(link.as).toBe("script");
	expect(link.href).toBe("https://example.com/public/path/chunk1-c.js");
	expect(link.crossOrigin).toBe("anonymous");

	link = document.head._children[7];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("prefetch");
	expect(link.as).toBe("script");
	expect(link.href).toBe("https://example.com/public/path/chunk1-a.js");
	expect(link.crossOrigin).toBe("anonymous");

	const promise2 = import(
		/* webpackChunkName: "chunk1", webpackPrefetch: true */ "./chunk1.js"
	);

	// Loading chunk1 again should not trigger prefetch/preload
	expect(document.head._children).toHaveLength(8);

	const promise3 = import(/* webpackChunkName: "chunk2" */ "./chunk2.js");

	expect(document.head._children).toHaveLength(9);

	// Test normal script loading
	script = document.head._children[8];
	expect(script._type).toBe("script");
	expect(script.src).toBe("https://example.com/public/path/chunk2.js");
	expect(script.getAttribute("nonce")).toBe("nonce");
	expect(script.crossOrigin).toBe("anonymous");
	expect(script.onload).toBeTypeOf("function");

	// Run the script — see note above about awaiting before script.onload().
	await import(/* webpackIgnore: true */ "./chunk2.js");

	script.onload();

	await promise3;

	// Loading chunk2 again should not trigger prefetch/preload as it's already prefetch/preloaded
	expect(document.head._children).toHaveLength(8);

	// chunk1-css / chunk2-css each install a <link rel="stylesheet"> AND a
	// <script> for the JS half. We never invoke script.onload() for those, so
	// the JSONP runtime arms a chunkLoadTimeout (120s) that would fire mid-way
	// through later tests on slow runners. Swallow the eventual rejection.
	const promise4 = import(/* webpackChunkName: "chunk1-css" */ "./chunk1.css");
	promise4.catch(() => {});

	expect(document.head._children).toHaveLength(10);

	link = document.head._children[8];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("stylesheet");
	expect(link.href).toBe("https://example.com/public/path/chunk1-css.css");
	expect(link.crossOrigin).toBe("anonymous");

	const promise5 = import(/* webpackChunkName: "chunk2-css", webpackPrefetch: true */ "./chunk2.css");
	promise5.catch(() => {});

	expect(document.head._children).toHaveLength(12);

	link = document.head._children[10];
	expect(link._type).toBe("link");
	expect(link.rel).toBe("stylesheet");
	expect(link.href).toBe("https://example.com/public/path/chunk2-css.css");
	expect(link.crossOrigin).toBe("anonymous");
});
