Wrapped logic to perform HTTP requests with fetch into an HttpClient class

This commit is contained in:
Alejandro Celaya
2022-11-15 20:31:35 +01:00
parent a0767417b3
commit 9b3bdebb28
13 changed files with 142 additions and 115 deletions

View File

@@ -1,22 +1,25 @@
import { Mock } from 'ts-mockery';
import { ImageDownloader } from '../../../src/common/services/ImageDownloader';
import { HttpClient } from '../../../src/common/services/HttpClient';
import { windowMock } from '../../__mocks__/Window.mock';
describe('ImageDownloader', () => {
const fetch = jest.fn();
const fetchBlob = jest.fn();
const httpClient = Mock.of<HttpClient>({ fetchBlob });
let imageDownloader: ImageDownloader;
beforeEach(() => {
jest.clearAllMocks();
(global as any).URL = { createObjectURL: () => '' };
imageDownloader = new ImageDownloader(fetch, windowMock);
imageDownloader = new ImageDownloader(httpClient, windowMock);
});
it('calls URL with response type blob', async () => {
fetch.mockResolvedValue({ blob: () => new Blob() });
fetchBlob.mockResolvedValue(new Blob());
await imageDownloader.saveImage('/foo/bar.png', 'my-image.png');
expect(fetch).toHaveBeenCalledWith('/foo/bar.png');
expect(fetchBlob).toHaveBeenCalledWith('/foo/bar.png');
});
});