|
1 | | -import { afterAll, beforeAll, describe, expect, it } from "vitest"; |
| 1 | +import { afterAll, beforeAll, describe, expect, it, vi } from "vitest"; |
2 | 2 | import { Application, boot, event, video } from "../src/index.js"; |
3 | 3 | import DefaultLoadingScreen from "../src/loader/loadingscreen.js"; |
4 | 4 |
|
@@ -37,16 +37,82 @@ describe("DefaultLoadingScreen", () => { |
37 | 37 | return screen; |
38 | 38 | }; |
39 | 39 |
|
40 | | - it("puts a progress bar in the world", () => { |
| 40 | + /** |
| 41 | + * Wait until `test()` holds, a few frames at most. |
| 42 | + * @param {Function} test - the condition |
| 43 | + * @returns {Promise<boolean>} whether it came true in time |
| 44 | + */ |
| 45 | + const until = async (test) => { |
| 46 | + for (let i = 0; i < 100 && !test(); i++) { |
| 47 | + await new Promise((resolve) => { |
| 48 | + setTimeout(resolve, 10); |
| 49 | + }); |
| 50 | + } |
| 51 | + return test(); |
| 52 | + }; |
| 53 | + |
| 54 | + it("shows the bar and the logo together, once the logo is decoded", async () => { |
| 55 | + // The logo decodes a frame or so after the screen comes up. A bar put |
| 56 | + // on screen at once made the logo pop in after it; now neither shows |
| 57 | + // until both can. |
41 | 58 | const screen = mount(); |
42 | 59 | try { |
43 | | - expect(screen.progressBar).toBeDefined(); |
44 | | - expect(app.world.hasChild(screen.progressBar)).toBe(true); |
| 60 | + const bar = screen.progressBar; |
| 61 | + expect(bar).toBeDefined(); |
| 62 | + expect(app.world.hasChild(bar)).toBe(false); |
| 63 | + expect(screen.logoSprite).toBe(null); |
| 64 | + |
| 65 | + expect( |
| 66 | + await until(() => { |
| 67 | + return screen.logoSprite !== null; |
| 68 | + }), |
| 69 | + ).toBe(true); |
| 70 | + expect(app.world.hasChild(screen.logoSprite)).toBe(true); |
| 71 | + expect(app.world.hasChild(bar)).toBe(true); |
45 | 72 | } finally { |
46 | 73 | screen.destroy(app); |
47 | 74 | } |
48 | 75 | }); |
49 | 76 |
|
| 77 | + it("shows the bar alone when the logo cannot be decoded", async () => { |
| 78 | + const decode = vi |
| 79 | + .spyOn(globalThis, "createImageBitmap") |
| 80 | + .mockRejectedValue(new Error("cannot decode")); |
| 81 | + const screen = mount(); |
| 82 | + try { |
| 83 | + const bar = screen.progressBar; |
| 84 | + expect( |
| 85 | + await until(() => { |
| 86 | + return app.world.hasChild(bar); |
| 87 | + }), |
| 88 | + ).toBe(true); |
| 89 | + expect(screen.logoSprite).toBe(null); |
| 90 | + } finally { |
| 91 | + decode.mockRestore(); |
| 92 | + screen.destroy(app); |
| 93 | + } |
| 94 | + }); |
| 95 | + |
| 96 | + it("tracks the load before the bar is on screen", () => { |
| 97 | + const screen = mount(); |
| 98 | + try { |
| 99 | + event.emit(event.LOADER_PROGRESS, 0.4); |
| 100 | + expect(screen.progressBar.value).toBe(0.4); |
| 101 | + } finally { |
| 102 | + screen.destroy(app); |
| 103 | + } |
| 104 | + }); |
| 105 | + |
| 106 | + it("destroys a bar that was never shown, and its subscription with it", () => { |
| 107 | + // torn down before the logo or the assets came in: the bar never |
| 108 | + // reached the world, so the world will not destroy it |
| 109 | + const screen = mount(); |
| 110 | + const bar = screen.progressBar; |
| 111 | + screen.destroy(app); |
| 112 | + event.emit(event.LOADER_PROGRESS, 0.7); |
| 113 | + expect(bar.value).toBe(0); |
| 114 | + }); |
| 115 | + |
50 | 116 | it("stays on screen after the preloader reports done", () => { |
51 | 117 | // The regression. `LOADER_COMPLETE` fires when the ASSETS are in — |
52 | 118 | // which is before the game has finished its own post-preload setup and |
|
0 commit comments