Improve loadImage to use a placeholder and prevent game crash

This commit is contained in:
Schluffe
2026-08-08 19:15:47 +02:00
parent 9bdffd06e4
commit e0c22b6952
3 changed files with 23 additions and 5 deletions
+12 -4
View File
@@ -1,5 +1,6 @@
import { afterEach, beforeEach, expect, test } from "vitest";
import { loadImage } from "./loadImage.ts";
import missingTextureSrc from "./missingTexture.svg?url";
class MockImage {
static instances: MockImage[] = [];
@@ -47,9 +48,16 @@ test("loadImage error", async () => {
const promise = loadImage("missing.png");
const image = MockImage.instances[0];
image.onerror!("missing");
image.onerror!("boom");
// errors are logged, but the promise still resolves with the image
await expect(promise).resolves.toBe(image);
expect(errors).toEqual([["missing"]]);
const placeholder = MockImage.instances[1];
expect(errors).toEqual([["boom"]]);
expect(placeholder.src).toBe(missingTextureSrc);
placeholder.onload!();
// errors are logged, but the promise still resolves
// with a drawable placeholder texture
await expect(promise).resolves.toBe(placeholder);
});