From 1427db4c32ca16ce167b61d05639bc1de99a5345 Mon Sep 17 00:00:00 2001 From: Justin Walsh Date: Thu, 22 Aug 2024 14:51:10 -0400 Subject: [PATCH] test: ensure tests do not capture data in the asset mocking actual pixi behavior --- test/unit/hooks/useAssets.test.tsx | 32 +++++++++++++++--------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/test/unit/hooks/useAssets.test.tsx b/test/unit/hooks/useAssets.test.tsx index 4aa24e46..0f4143be 100644 --- a/test/unit/hooks/useAssets.test.tsx +++ b/test/unit/hooks/useAssets.test.tsx @@ -9,8 +9,11 @@ describe('useAssets', async () => { const assets: UnresolvedAsset[] = [{ src: 'test.png' }, { src: 'test2.png' }]; - // Mock the Assets.load, Assets.get & Cache.has method + // Store the loaded assets and data state to verify the hook results let loaded: Record = {}; + let data: Record = {}; + + // Mock the Assets.load, Assets.get & Cache.has method const load = vi.spyOn(Assets, 'load'); const get = vi.spyOn(Assets, 'get'); const has = vi.spyOn(Cache, 'has'); @@ -19,14 +22,14 @@ describe('useAssets', async () => load.mockImplementation((urls) => { const assets = urls as UnresolvedAsset[]; - const result = assets.reduce((acc, val) => ({ ...acc, [val.src!.toString()]: Object.assign(Texture.EMPTY, { data: val.data }) }), {}); return new Promise((resolve) => { setTimeout(() => { - loaded = { ...loaded, ...result }; - resolve(result); + loaded = { ...loaded, ...assets.reduce((acc, val) => ({ ...acc, [val.src!.toString()]: Texture.EMPTY }), {}) }; + data = { ...data, ...assets.reduce((acc, val) => ({ ...acc, [val.src!.toString()]: val.data }), {}) }; + resolve(loaded); }, 1); }); }); @@ -44,6 +47,7 @@ describe('useAssets', async () => beforeEach(() => { loaded = {}; + data = {}; }); afterEach(() => @@ -69,10 +73,10 @@ describe('useAssets', async () => it('accepts data', async () => { - type Data = { data: { test: string } }; - const { result } = renderHook(() => useAssets([ - { src: 'test.png', data: { test: 'test' } }, - { src: 'test2.png', data: { test: 'test' } }, + // Explicitly type the T in the useAssets hook + const { result } = renderHook(() => useAssets([ + { src: 'test.png', data: { test: '7a1c8bee' } }, + { src: 'test2.png', data: { test: '230a3f41' } }, ])); expect(result.current.isPending).toBe(true); @@ -81,20 +85,19 @@ describe('useAssets', async () => const { assets: [texture], isSuccess } = result.current; expect(isSuccess).toBe(true); - expect(texture?.data.test).toBe('test'); + expect(data['test.png'].test).toBe('7a1c8bee'); + expect(data['test2.png'].test).toBe('230a3f41'); const isTexture = (texture?: Texture) => texture && texture instanceof Texture; - const getData = (texture?: Texture & Data) => texture?.data.test; expect(isTexture(texture)).toBe(true); - expect(getData(texture)).toBe('test'); }); it('is properly typed with data', async () => { - type Data = { data: { test: string } }; + // Do not provide a type for T in the useAssets hook const { result } = renderHook(() => useAssets([ - { src: 'test.png', data: { test: 'test' } }, + { src: 'test.png', data: { test: 'd460dbdd' } }, ])); expect(result.current.isPending).toBe(true); @@ -103,12 +106,9 @@ describe('useAssets', async () => const { assets: [texture], isSuccess } = result.current; expect(isSuccess).toBe(true); - expect(texture?.data.test).toBe('test'); const isTexture = (texture?: Texture) => texture && texture instanceof Texture; - const getData = (texture?: Texture & Data) => texture?.data.test; expect(isTexture(texture)).toBe(true); - expect(getData(texture)).toBe('test'); }); });