Skip to content

Commit

Permalink
fix: broken build due to failing unit tests (apache#141)
Browse files Browse the repository at this point in the history
* fix: callapi unit test

* test: fix async calls

* fix: remaining test

* refactor: do not declare unused var
  • Loading branch information
kristw authored and zhaoyongjie committed Nov 26, 2021
1 parent 6421d4b commit d6f3933
Showing 1 changed file with 28 additions and 34 deletions.
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

0 comments on commit d6f3933

Please sign in to comment.