mirror of
https://github.com/shlinkio/shlink-web-client.git
synced 2026-04-11 00:56:20 +00:00
Created test for HttpClient
This commit is contained in:
@@ -1,5 +1,3 @@
|
|||||||
export type MediaMatcher = (query: string) => MediaQueryList;
|
export type MediaMatcher = (query: string) => MediaQueryList;
|
||||||
|
|
||||||
export type Fetch = typeof window.fetch;
|
export type Fetch = typeof window.fetch;
|
||||||
|
|
||||||
export type JsonFetch = <T>(url: string, options?: RequestInit) => Promise<T>;
|
|
||||||
|
|||||||
37
test/common/services/HttpClient.test.ts
Normal file
37
test/common/services/HttpClient.test.ts
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
import { HttpClient } from '../../../src/common/services/HttpClient';
|
||||||
|
|
||||||
|
describe('HttpClient', () => {
|
||||||
|
const fetch = jest.fn();
|
||||||
|
const httpClient = new HttpClient(fetch);
|
||||||
|
|
||||||
|
beforeEach(jest.clearAllMocks);
|
||||||
|
|
||||||
|
describe('fetchJson', () => {
|
||||||
|
it('throws json when response is not ok', async () => {
|
||||||
|
const theError = { error: true, foo: 'bar' };
|
||||||
|
fetch.mockResolvedValue({ json: () => theError, ok: false });
|
||||||
|
|
||||||
|
await expect(httpClient.fetchJson('')).rejects.toEqual(theError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('return json when response is ok', async () => {
|
||||||
|
const theJson = { foo: 'bar' };
|
||||||
|
fetch.mockResolvedValue({ json: () => theJson, ok: true });
|
||||||
|
|
||||||
|
const result = await httpClient.fetchJson('');
|
||||||
|
|
||||||
|
expect(result).toEqual(theJson);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
describe('fetchBlob', () => {
|
||||||
|
it('returns response as blob', async () => {
|
||||||
|
const theBlob = new Blob();
|
||||||
|
fetch.mockResolvedValue({ blob: () => theBlob });
|
||||||
|
|
||||||
|
const result = await httpClient.fetchBlob('');
|
||||||
|
|
||||||
|
expect(result).toEqual(theBlob);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user