Skip to content

Commit

Permalink
chore(asset): clean up AssetManager tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fallenoak committed Dec 27, 2023
1 parent 195df34 commit 6763970
Showing 1 changed file with 12 additions and 11 deletions.
23 changes: 12 additions & 11 deletions src/spec/AssetManager.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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);
});
});
});

0 comments on commit 6763970

Please sign in to comment.