Skip to content

Commit 05a96f6

Browse files
committed
fix: test code
1 parent c3bf073 commit 05a96f6

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

tests/lib/fetch.test.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import { generateFetchClient } from '../../src/lib/fetch';
2-
import crossFetch from 'cross-fetch';
32

43
describe('generateFetchClient', () => {
5-
test('If no options is specified, cross-fetch is used', () => {
4+
test('If no options is specified, cross-fetch is used', async () => {
65
const client = generateFetchClient('apiKey');
7-
expect(typeof client === typeof crossFetch).toBe(true);
6+
const response = await client('http://example.com');
7+
expect(response.status).toBe(200);
88
});
99

10-
test('If custom fetch is specified, custom fetch is used', () => {
11-
const customFetch = () => Promise.resolve(new Response());
10+
test('If custom fetch is specified, custom fetch is used', async () => {
11+
const customFetch = jest.fn();
1212
const client = generateFetchClient('apiKey', customFetch);
13-
expect(typeof client === typeof customFetch).toBe(true);
13+
await client('http://example.com');
14+
expect(customFetch).toHaveBeenCalled();
1415
});
1516
});

tests/mocks/handlers.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ import { API_VERSION, BASE_DOMAIN } from '../../src/utils/constants';
55
const baseUrl = `https://serviceDomain.${BASE_DOMAIN}/api/${API_VERSION}`;
66

77
const hasValidApiKey = (req: RestRequest) => {
8-
if (req.headers.get('X-MICROCMS-API-KEY') !== 'apiKey') {
9-
return false;
10-
}
11-
return true;
8+
return req.headers.get('X-MICROCMS-API-KEY') === 'apiKey';
129
};
1310

1411
export const handlers = [
12+
rest.get('http://example.com', (req, res, ctx) => {
13+
if (!hasValidApiKey(req)) return res(ctx.status(401));
14+
return res(ctx.status(200));
15+
}),
1516
rest.get(`${baseUrl}/list-type`, (req, res, ctx) => {
1617
if (!hasValidApiKey(req)) return res(ctx.status(401));
1718

0 commit comments

Comments
 (0)