UNPKG

@transferwise/approve-api-action-helpers

Version:

An http client that handles SCA protected requests gracefully

62 lines (52 loc) 1.52 kB
import { http } from './http'; const defaultHeaders = { 'Content-Type': 'application/json', }; const resJson = jest.fn(() => 'parsed json'); // eslint-disable-next-line jest/prefer-lowercase-title describe('HTTP Client', () => { beforeEach(() => { fetch.resetMocks(); fetch.mockResolvedValue({ json: resJson, ok: true, }); // eslint-disable-next-line jest/no-standalone-expect expect(fetch).not.toHaveBeenCalled(); }); describe('http method', () => { it('calls fetch with whatever parameters were passed to it', async () => { await http('IM A URL', { isParam: true, method: 'GET' }); expect(fetch).toHaveBeenCalledWith('IM A URL', { isParam: true, headers: defaultHeaders, method: 'GET', }); }); it('passes params.headers through the headers object', async () => { await http('IM A URL', { headers: { 'X-Header-Key': 'this is a value', }, method: 'UPDATE', }); expect(fetch).toHaveBeenCalledWith('IM A URL', { headers: { ...defaultHeaders, 'X-Header-Key': 'this is a value', }, method: 'UPDATE', }); }); it('throws an http error if res.ok is not true', async () => { fetch.mockResolvedValue({ ok: false, }); await expect( http('IM A URL', { method: 'POST', }), ).rejects.toThrow(); // eslint-disable-line jest/require-to-throw-message }); }); });