Skip to content

Commit

Permalink
test: ensure tests do not capture data in the asset mocking actual pi…
Browse files Browse the repository at this point in the history
…xi behavior
  • Loading branch information
thejustinwalsh committed Aug 22, 2024
1 parent 6a00c77 commit 1427db4
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions test/unit/hooks/useAssets.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown> = {};
let data: Record<string, any> = {};

// 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');
Expand All @@ -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);
});
});
Expand All @@ -44,6 +47,7 @@ describe('useAssets', async () =>
beforeEach(() =>
{
loaded = {};
data = {};
});

afterEach(() =>
Expand All @@ -69,10 +73,10 @@ describe('useAssets', async () =>

it('accepts data', async () =>
{
type Data = { data: { test: string } };
const { result } = renderHook(() => useAssets<Texture & Data>([
{ src: 'test.png', data: { test: 'test' } },
{ src: 'test2.png', data: { test: 'test' } },
// Explicitly type the T in the useAssets hook
const { result } = renderHook(() => useAssets<Texture>([
{ src: 'test.png', data: { test: '7a1c8bee' } },
{ src: 'test2.png', data: { test: '230a3f41' } },
]));

expect(result.current.isPending).toBe(true);
Expand All @@ -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);
Expand All @@ -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');
});
});

0 comments on commit 1427db4

Please sign in to comment.