diff --git a/packages/core/src/2d/atlas/SpriteAtlas.ts b/packages/core/src/2d/atlas/SpriteAtlas.ts index 44adadbfdd..93828efe2e 100644 --- a/packages/core/src/2d/atlas/SpriteAtlas.ts +++ b/packages/core/src/2d/atlas/SpriteAtlas.ts @@ -45,7 +45,7 @@ export class SpriteAtlas extends ReferResource { sprite.name === name && outSprites.push(sprite); } } else { - console.warn("The name of the sprite you want to find is not exit in SpriteAtlas."); + console.warn("There is no sprite named " + name + " in the atlas."); } return outSprites; } diff --git a/tests/src/core/SpriteAtlas.test.ts b/tests/src/core/SpriteAtlas.test.ts new file mode 100644 index 0000000000..6417e8be17 --- /dev/null +++ b/tests/src/core/SpriteAtlas.test.ts @@ -0,0 +1,63 @@ +import { Sprite, SpriteAtlas, Texture2D } from "@galacean/engine-core"; +import { WebGLEngine } from "@galacean/engine"; +import { afterEach, describe, expect, it, vi } from "vitest"; + +describe("SpriteAtlas", async () => { + const canvas = document.createElement("canvas"); + const engine = await WebGLEngine.create({ canvas: canvas }); + + afterEach(() => { + vi.restoreAllMocks(); + }); + + it("getSprite returns the sprite when it exists", () => { + const atlas = new SpriteAtlas(engine); + const sprite = new Sprite(engine, new Texture2D(engine, 100, 100)); + sprite.name = "test"; + // @ts-ignore + atlas._addSprite(sprite); + + const result = atlas.getSprite("test"); + expect(result).to.eq(sprite); + }); + + it("getSprite warns when sprite not found", () => { + const atlas = new SpriteAtlas(engine); + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const result = atlas.getSprite("nonexistent"); + expect(result).to.be.undefined; + expect(warnSpy).toHaveBeenCalledWith("There is no sprite named nonexistent in the atlas."); + }); + + it("getSprites returns matching sprites", () => { + const atlas = new SpriteAtlas(engine); + const sprite1 = new Sprite(engine, new Texture2D(engine, 100, 100)); + const sprite2 = new Sprite(engine, new Texture2D(engine, 200, 200)); + sprite1.name = "shared"; + sprite2.name = "shared"; + // @ts-ignore + atlas._addSprite(sprite1); + // @ts-ignore + atlas._addSprite(sprite2); + + const out: Sprite[] = []; + const result = atlas.getSprites("shared", out); + expect(result).to.eq(out); + expect(out.length).to.be.greaterThan(0); + for (const s of out) { + expect(s.name).to.eq("shared"); + } + }); + + it("getSprites warns when sprite not found", () => { + const atlas = new SpriteAtlas(engine); + const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {}); + + const out: Sprite[] = []; + const result = atlas.getSprites("nonexistent", out); + expect(result).to.eq(out); + expect(out.length).to.eq(0); + expect(warnSpy).toHaveBeenCalledWith("There is no sprite named nonexistent in the atlas."); + }); +});