Skip to content

Commit

Permalink
build: add missing unit test for callApi (apache#218)
Browse files Browse the repository at this point in the history
* build: add missing unit test for callApi

* fix: test
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 17, 2021
1 parent 2b580e4 commit 685b25d
Showing 1 changed file with 20 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('callApi()', () => {
const mockPutUrl = '/mock/put/url';
const mockPatchUrl = '/mock/patch/url';
const mockCacheUrl = '/mock/cache/url';
const mockNotFound = '/mock/notfound';

const mockGetPayload = { get: 'payload' };
const mockPostPayload = { post: 'payload' };
Expand All @@ -35,6 +36,7 @@ describe('callApi()', () => {
fetchMock.put(mockPutUrl, mockPutPayload);
fetchMock.patch(mockPatchUrl, mockPatchPayload);
fetchMock.get(mockCacheUrl, mockCachePayload);
fetchMock.get(mockNotFound, { status: 404 });

afterEach(fetchMock.reset);

Expand Down Expand Up @@ -413,6 +415,24 @@ describe('callApi()', () => {
expect(error.message).toEqual('Received 304 but no content is cached!');
});
});

it('returns original response if no Etag', async () => {
const url = mockGetUrl;
const response = await callApi({ url, method: 'GET' });
const calls = fetchMock.calls(url);
expect(calls).toHaveLength(1);
expect(response.status).toEqual(200);
const body = await response.json();
expect(body).toEqual(mockGetPayload);
});

it('returns original response if status not 304 or 200', async () => {
const url = mockNotFound;
const response = await callApi({ url, method: 'GET' });
const calls = fetchMock.calls(url);
expect(calls).toHaveLength(1);
expect(response.status).toEqual(404);
});
});

it('rejects if the request throws', () => {
Expand Down

0 comments on commit 685b25d

Please sign in to comment.