|
| 1 | +/** |
| 2 | + * @jest-environment jsdom |
| 3 | + * |
| 4 | + * WebGL Support Tests |
| 5 | + * |
| 6 | + * jsdom has no WebGL, so the unsupported path is the natural one here: |
| 7 | + * load() must throw WebGLUnsupportedError (not silently succeed), and |
| 8 | + * resize() must be a no-op before the renderer exists. |
| 9 | + */ |
| 10 | + |
| 11 | +import { Canvas } from "@canvas/shotstack-canvas"; |
| 12 | +import { checkWebGLSupport, WebGLUnsupportedError } from "@core/webgl-support"; |
| 13 | + |
| 14 | +import type { Edit } from "@core/edit-session"; |
| 15 | + |
| 16 | +// pixi.js ships untransformed ESM that jest can't parse; the paths under test |
| 17 | +// (constructor + the WebGL guard at the top of load/resize) never reach a real renderer. |
| 18 | +jest.mock("pixi.js", () => ({ |
| 19 | + Application: jest.fn(), |
| 20 | + Container: jest.fn(), |
| 21 | + Graphics: jest.fn(), |
| 22 | + Rectangle: jest.fn() |
| 23 | +})); |
| 24 | +jest.mock("pixi.js/app", () => ({})); |
| 25 | +jest.mock("pixi.js/events", () => ({})); |
| 26 | +jest.mock("pixi.js/graphics", () => ({})); |
| 27 | +jest.mock("pixi.js/text", () => ({})); |
| 28 | +jest.mock("pixi.js/text-html", () => ({})); |
| 29 | +jest.mock("pixi.js/sprite-tiling", () => ({})); |
| 30 | +jest.mock("pixi.js/filters", () => ({})); |
| 31 | +jest.mock("pixi.js/mesh", () => ({})); |
| 32 | + |
| 33 | +const makeEditStub = (): Edit => |
| 34 | + ({ |
| 35 | + setCanvas: () => {}, |
| 36 | + size: { width: 1280, height: 720 } |
| 37 | + }) as unknown as Edit; |
| 38 | + |
| 39 | +describe("WebGL support", () => { |
| 40 | + beforeEach(() => { |
| 41 | + document.body.innerHTML = `<div data-shotstack-studio></div>`; |
| 42 | + }); |
| 43 | + |
| 44 | + it("reports unsupported in jsdom", () => { |
| 45 | + expect(checkWebGLSupport().supported).toBe(false); |
| 46 | + }); |
| 47 | + |
| 48 | + it("load() throws WebGLUnsupportedError when WebGL is unavailable", async () => { |
| 49 | + const canvas = new Canvas(makeEditStub()); |
| 50 | + await expect(canvas.load()).rejects.toBeInstanceOf(WebGLUnsupportedError); |
| 51 | + }); |
| 52 | + |
| 53 | + it("still renders the error overlay for the user", async () => { |
| 54 | + const canvas = new Canvas(makeEditStub()); |
| 55 | + await canvas.load().catch(() => {}); |
| 56 | + const root = document.querySelector("[data-shotstack-studio]"); |
| 57 | + expect(root?.childElementCount).toBeGreaterThan(0); |
| 58 | + }); |
| 59 | + |
| 60 | + it("resize() is a no-op before the renderer is initialised", () => { |
| 61 | + const canvas = new Canvas(makeEditStub()); |
| 62 | + expect(() => canvas.resize()).not.toThrow(); |
| 63 | + }); |
| 64 | +}); |
0 commit comments