|
| 1 | +// This config need to be set on initial evaluation to be effective |
| 2 | +__webpack_public_path__ = "https://test.cases/"; |
| 3 | + |
| 4 | +it("waits for a server-rendered stylesheet that has not applied yet", async () => { |
| 5 | + // A streamed render inserts its <link> after the shell, so it is not render |
| 6 | + // blocking: no `sheet` yet means the rules are still on their way. |
| 7 | + const printed = document.createElement("link"); |
| 8 | + printed.rel = "stylesheet"; |
| 9 | + printed.href = "https://test.cases/button.css"; |
| 10 | + printed.sheet = undefined; |
| 11 | + document.head.appendChild(printed); |
| 12 | + |
| 13 | + let loaded = false; |
| 14 | + const promise = import(/* webpackChunkName: "button" */ "./button.js").then( |
| 15 | + (module) => { |
| 16 | + loaded = true; |
| 17 | + return module; |
| 18 | + } |
| 19 | + ); |
| 20 | + |
| 21 | + // drive the <script> load for chunk "button" so only the stylesheet is pending |
| 22 | + const script = document.head._children[document.head._children.length - 1]; |
| 23 | + expect(script._type).toBe("script"); |
| 24 | + __non_webpack_require__("./button.js"); |
| 25 | + script.onload(); |
| 26 | + |
| 27 | + // the runtime adopted the printed link rather than creating its own |
| 28 | + expect(typeof printed.onload).toBe("function"); |
| 29 | + |
| 30 | + await new Promise((resolve) => setTimeout(resolve, 0)); |
| 31 | + expect(loaded).toBe(false); |
| 32 | + |
| 33 | + printed.sheet = {}; |
| 34 | + printed.onload({ type: "load", target: printed }); |
| 35 | + |
| 36 | + const { renderButton } = await promise; |
| 37 | + |
| 38 | + expect(renderButton("Buy")).toBe('<button class="button">Buy</button>'); |
| 39 | + |
| 40 | + const stylesheets = document.head._children.filter( |
| 41 | + (element) => element._type === "link" && element.rel === "stylesheet" |
| 42 | + ); |
| 43 | + |
| 44 | + expect(stylesheets).toHaveLength(1); |
| 45 | + expect(stylesheets[0]).toBe(printed); |
| 46 | +}); |
0 commit comments