From 685b25d5a0f54c1bc53190b586aa31fd975b6d3b Mon Sep 17 00:00:00 2001 From: Krist Wongsuphasawat Date: Tue, 3 Sep 2019 21:04:10 -0700 Subject: [PATCH] build: add missing unit test for callApi (#218) * build: add missing unit test for callApi * fix: test --- .../test/callApi/callApi.test.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-connection/test/callApi/callApi.test.ts b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-connection/test/callApi/callApi.test.ts index 761de30de6321..add87549a19b7 100644 --- a/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-connection/test/callApi/callApi.test.ts +++ b/superset-frontend/temporary_superset_ui/superset-ui/packages/superset-ui-connection/test/callApi/callApi.test.ts @@ -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' }; @@ -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); @@ -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', () => {