diff --git a/src/spec/AssetManager.spec.ts b/src/spec/AssetManager.spec.ts index f34b623..71b6a59 100644 --- a/src/spec/AssetManager.spec.ts +++ b/src/spec/AssetManager.spec.ts @@ -9,8 +9,8 @@ const createFetchResponse = (status: number, statusText: string, data: ArrayBuff }); describe('AssetManager', () => { - describe('getAsset', () => { - test('should return expected asset data when fetch succeeds', async () => { + describe('get', () => { + test('should return new asset data when not previously loaded', async () => { const assetManager = new AssetManager('http://example.local', true); const assetData = new ArrayBuffer(7); @@ -23,29 +23,30 @@ describe('AssetManager', () => { expect(returnedAssetData).toEqual(assetData); }); - test('should throw when fetch fails', async () => { + test('should return existing asset data when previously loaded', async () => { const assetManager = new AssetManager('http://example.local', true); const assetData = new ArrayBuffer(7); const mockFetch = vi.fn(); - mockFetch.mockResolvedValue(createFetchResponse(404, 'Not Found', assetData)); + mockFetch.mockResolvedValue(createFetchResponse(200, 'Okay', assetData)); globalThis.fetch = mockFetch; - await expect(assetManager.get('foo')).rejects.toBeInstanceOf(Error); + const returnedAssetData1 = await assetManager.get('foo'); + const returnedAssetData2 = await assetManager.get('foo'); + + expect(mockFetch).toHaveBeenCalledOnce(); + expect(returnedAssetData1).toBe(returnedAssetData2); }); - test('should only fetch once for a given asset path', async () => { + test('should throw when asset data cannot be fetched', async () => { const assetManager = new AssetManager('http://example.local', true); const assetData = new ArrayBuffer(7); const mockFetch = vi.fn(); - mockFetch.mockResolvedValue(createFetchResponse(200, 'Okay', assetData)); + mockFetch.mockResolvedValue(createFetchResponse(404, 'Not Found', assetData)); globalThis.fetch = mockFetch; - await assetManager.get('foo'); - await assetManager.get('foo'); - - expect(mockFetch).toHaveBeenCalledOnce(); + await expect(assetManager.get('foo')).rejects.toBeInstanceOf(Error); }); }); });