Improve loadImage to use a placeholder and prevent game crash
This commit is contained in:
@@ -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);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user