Improved HttpClientTest

This commit is contained in:
Alejandro Celaya 2022-11-20 12:54:06 +01:00
parent 059fa37ca7
commit cb76c89a08

View file

@ -7,14 +7,14 @@ describe('HttpClient', () => {
beforeEach(jest.clearAllMocks);
describe('fetchJson', () => {
it('throws json when response is not ok', async () => {
it('throws json on success', 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 () => {
it('return json on failure', async () => {
const theJson = { foo: 'bar' };
fetch.mockResolvedValue({ json: () => theJson, ok: true });
@ -24,6 +24,23 @@ describe('HttpClient', () => {
});
});
describe('fetchEmpty', () => {
it('returns empty on success', async () => {
fetch.mockResolvedValue({ ok: true });
const result = await httpClient.fetchEmpty('');
expect(result).not.toBeDefined();
});
it('throws error on failure', async () => {
const theError = { error: true, foo: 'bar' };
fetch.mockResolvedValue({ json: () => theError, ok: false });
await expect(httpClient.fetchJson('')).rejects.toEqual(theError);
});
});
describe('fetchBlob', () => {
it('returns response as blob', async () => {
const theBlob = new Blob();