Skip to content
This repository has been archived by the owner on Dec 10, 2021. It is now read-only.

fix: broken build due to failing unit tests #141

Merged
merged 4 commits into from
Apr 26, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 28 additions & 34 deletions packages/superset-ui-connection/test/callApi/callApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -314,27 +314,24 @@ describe('callApi()', () => {
);
}));

it('works when the Cache API is disabled', () => {
it('works when the Cache API is disabled', async () => {
Object.defineProperty(constants, 'CACHE_AVAILABLE', { value: false });

return callApi({ url: mockCacheUrl, method: 'GET' }).then(firstResponse => {
const calls = fetchMock.calls(mockCacheUrl);
expect(calls).toHaveLength(1);
expect(firstResponse.body).toEqual('BODY');

return callApi({ url: mockCacheUrl, method: 'GET' }).then(secondResponse => {
const fetchParams = calls[1][1];
expect(calls).toHaveLength(2);

// second call should not have If-None-Match header
expect(fetchParams.headers).toBeUndefined();
expect(secondResponse.body).toEqual('BODY');

Object.defineProperty(constants, 'CACHE_AVAILABLE', { value: true });

return Promise.resolve();
});
});
const firstResponse = await callApi({ url: mockCacheUrl, method: 'GET' });
const calls = fetchMock.calls(mockCacheUrl);
expect(calls).toHaveLength(1);
const firstBody = await firstResponse.text();
expect(firstBody).toEqual('BODY');

const secondResponse = await callApi({ url: mockCacheUrl, method: 'GET' });
const fetchParams = calls[1][1];
expect(calls).toHaveLength(2);
// second call should not have If-None-Match header
expect(fetchParams.headers).toBeUndefined();
const secondBody = await secondResponse.text();
expect(secondBody).toEqual('BODY');

Object.defineProperty(constants, 'CACHE_AVAILABLE', { value: true });
});

it('sends known ETags in the If-None-Match header', () =>
Expand All @@ -354,23 +351,20 @@ describe('callApi()', () => {
});
}));

it('reuses cached responses on 304 status', () =>
it('reuses cached responses on 304 status', async () => {
// first call sets the cache
callApi({ url: mockCacheUrl, method: 'GET' }).then(() => {
const calls = fetchMock.calls(mockCacheUrl);
expect(calls).toHaveLength(1);

// second call reuses the cached payload on a 304
const mockCachedPayload = { status: 304 };
fetchMock.get(mockCacheUrl, mockCachedPayload, { overwriteRoutes: true });

return callApi({ url: mockCacheUrl, method: 'GET' }).then(response => {
expect(calls).toHaveLength(2);
expect(response.body).toEqual('BODY');
await callApi({ url: mockCacheUrl, method: 'GET' });
const calls = fetchMock.calls(mockCacheUrl);
expect(calls).toHaveLength(1);
// second call reuses the cached payload on a 304
const mockCachedPayload = { status: 304 };
fetchMock.get(mockCacheUrl, mockCachedPayload, { overwriteRoutes: true });

return Promise.resolve();
});
}));
const secondResponse = await callApi({ url: mockCacheUrl, method: 'GET' });
expect(calls).toHaveLength(2);
const secondBody = await secondResponse.text();
expect(secondBody).toEqual('BODY');
});

it('throws error when cache fails on 304', () => {
// this should never happen, since a 304 is only returned if we have
Expand Down