Migrated ImageDownloader from axios to fetch

This commit is contained in:
Alejandro Celaya
2022-11-15 11:41:05 +01:00
parent a88ebc26a9
commit 34aa156d5f
9 changed files with 21 additions and 31 deletions

View File

@@ -3,12 +3,12 @@ import { ShlinkApiClient } from '../../../src/api/services/ShlinkApiClient';
import { OptionalString } from '../../../src/utils/utils';
import { ShlinkDomain, ShlinkVisits, ShlinkVisitsOverview } from '../../../src/api/types';
import { ShortUrl, ShortUrlsOrder } from '../../../src/short-urls/data';
import { Fetch } from '../../../src/utils/types';
import { JsonFetch } from '../../../src/utils/types';
describe('ShlinkApiClient', () => {
const buildFetch = (data: any) => jest.fn().mockResolvedValue(data);
const buildRejectedFetch = (error: any) => jest.fn().mockRejectedValueOnce(error);
const buildApiClient = (fetch: Fetch) => new ShlinkApiClient(fetch, '', '');
const buildApiClient = (fetch: JsonFetch) => new ShlinkApiClient(fetch, '', '');
const shortCodesWithDomainCombinations: [string, OptionalString][] = [
['abc123', null],
['abc123', undefined],

View File

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