2025-10-03 02:55:56 +08:00
|
|
|
function abortable(fn) {
|
|
|
|
return new Promise((resolve) => {
|
|
|
|
const timeoutId = setTimeout(() => {
|
|
|
|
fn = undefined;
|
|
|
|
resolve('Promise resolved after delay');
|
|
|
|
clearTimeout(timeoutId);
|
|
|
|
}, 1000);
|
|
|
|
|
|
|
|
return fn();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
it("should set fetchPriority", async () => {
|
|
|
|
abortable(() => import(/* webpackFetchPriority: "high" */ "./a"));
|
2023-05-24 10:45:33 +08:00
|
|
|
expect(document.head._children).toHaveLength(4);
|
2023-05-24 13:18:26 +08:00
|
|
|
const script1 = document.head._children[2];
|
|
|
|
expect(script1._attributes.fetchpriority).toBe("high");
|
2023-04-23 08:40:29 +08:00
|
|
|
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./b"));
|
2023-05-24 10:45:33 +08:00
|
|
|
expect(document.head._children).toHaveLength(5);
|
2023-05-24 13:18:26 +08:00
|
|
|
const script2 = document.head._children[4];
|
|
|
|
expect(script2._attributes.fetchpriority).toBe("low");
|
|
|
|
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable(() => import(/* webpackFetchPriority: "low" */ "./c"));
|
2023-05-24 13:18:26 +08:00
|
|
|
expect(document.head._children).toHaveLength(6);
|
|
|
|
const script3 = document.head._children[5];
|
2023-06-13 05:17:53 +08:00
|
|
|
expect(script3._attributes.fetchpriority).toBe("low");
|
2023-05-24 10:45:33 +08:00
|
|
|
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable(() => import(/* webpackPrefetch: 20, webpackFetchPriority: "auto" */ "./c"));
|
2023-05-24 13:18:26 +08:00
|
|
|
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable(() => import("./d"))
|
2023-05-24 13:18:26 +08:00
|
|
|
expect(document.head._children).toHaveLength(7);
|
|
|
|
const script4 = document.head._children[6];
|
|
|
|
expect(script4._attributes.fetchpriority).toBeUndefined();
|
|
|
|
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable(() => import(/* webpackPrefetch: -20 */ "./d3"));
|
2023-05-24 13:18:26 +08:00
|
|
|
expect(document.head._children).toHaveLength(8);
|
|
|
|
const script5 = document.head._children[7];
|
2023-06-13 05:17:53 +08:00
|
|
|
expect(script5._attributes.fetchpriority).toBeUndefined();
|
|
|
|
|
|
|
|
const condition = true;
|
|
|
|
|
|
|
|
if (!condition) {
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable( () => import(/* webpackFetchPriority: "high", webpackChunkName: "one" */ "./e"));
|
2023-06-13 05:17:53 +08:00
|
|
|
expect(document.head._children).toHaveLength(9);
|
|
|
|
const script6 = document.head._children[8];
|
|
|
|
expect(script6._attributes.fetchpriority).toBe("high");
|
|
|
|
} else {
|
2025-10-03 02:55:56 +08:00
|
|
|
abortable(() => import(/* webpackFetchPriority: "low", webpackChunkName: "two" */ "./e"));
|
2023-06-13 05:17:53 +08:00
|
|
|
expect(document.head._children).toHaveLength(9);
|
|
|
|
const script6 = document.head._children[8];
|
|
|
|
expect(script6._attributes.fetchpriority).toBe("low");
|
|
|
|
}
|
2023-04-23 08:40:29 +08:00
|
|
|
});
|