Skip to content

Commit e28924f

Browse files
committed
test: requestInit のテストコード追加
1 parent c16e452 commit e28924f

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

tests/requestInit.test.ts

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
import { createClient } from '../src/createClient';
2+
import { resolveHeadersConstructor } from '../src/lib/fetch';
3+
4+
const fetchMock = jest.fn(() =>
5+
Promise.resolve({
6+
ok: true,
7+
json: () => Promise.resolve(),
8+
})
9+
);
10+
11+
const client = createClient({
12+
serviceDomain: 'serviceDomain',
13+
apiKey: 'apiKey',
14+
customFetch: fetchMock as any,
15+
});
16+
17+
const Headers = resolveHeadersConstructor();
18+
19+
beforeEach(() => {
20+
fetchMock.mockClear();
21+
});
22+
23+
describe('requestInit', () => {
24+
test('Default request init is passed for fetch in get request.', async () => {
25+
await client.get({ endpoint: 'object-type' });
26+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
27+
// @ts-expect-error
28+
expect(fetchMock.mock.calls[0][1]).toEqual({
29+
method: 'GET',
30+
headers: new Headers({
31+
'X-MICROCMS-API-KEY': 'apiKey',
32+
}),
33+
});
34+
});
35+
36+
test('Custom request init added cache parameter is passed for fetch in get request.', async () => {
37+
await client.get({
38+
endpoint: 'object-type',
39+
customRequestInit: {
40+
cache: 'no-store',
41+
},
42+
});
43+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
44+
// @ts-expect-error
45+
expect(fetchMock.mock.calls[0][1]).toEqual({
46+
method: 'GET',
47+
headers: new Headers({
48+
'X-MICROCMS-API-KEY': 'apiKey',
49+
}),
50+
cache: 'no-store',
51+
});
52+
});
53+
54+
test('Custom request init added for Next.js parameter is passed for fetch in get request.', async () => {
55+
await client.get({
56+
endpoint: 'object-type',
57+
customRequestInit: {
58+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
59+
// @ts-expect-error
60+
next: {
61+
revalidate: 10,
62+
},
63+
},
64+
});
65+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
66+
// @ts-expect-error
67+
expect(fetchMock.mock.calls[0][1]).toEqual({
68+
method: 'GET',
69+
headers: new Headers({
70+
'X-MICROCMS-API-KEY': 'apiKey',
71+
}),
72+
next: {
73+
revalidate: 10,
74+
},
75+
});
76+
});
77+
78+
test('Custom request init added method, headers and body parameters is not overwrited for fetch in create request.', async () => {
79+
await client.create({
80+
endpoint: 'list-type',
81+
content: { title: 'title' },
82+
customRequestInit: {
83+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
84+
// @ts-expect-error
85+
method: 'GET',
86+
headers: {
87+
'X-MICROCMS-API-KEY': 'OverwrittenApiKey',
88+
},
89+
body: { title: 'Overwritten Title' },
90+
},
91+
});
92+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
93+
// @ts-expect-error
94+
expect(fetchMock.mock.calls[0][1]).toEqual({
95+
method: 'POST',
96+
headers: new Headers({
97+
'Content-Type': 'application/json',
98+
'X-MICROCMS-API-KEY': 'apiKey',
99+
}),
100+
body: JSON.stringify({ title: 'title' }),
101+
});
102+
});
103+
104+
test('Custom request init added method, headers and body parameters is not overwrited for fetch in update request.', async () => {
105+
await client.update({
106+
endpoint: 'list-type',
107+
content: { title: 'title' },
108+
customRequestInit: {
109+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
110+
// @ts-expect-error
111+
method: 'GET',
112+
headers: {
113+
'X-MICROCMS-API-KEY': 'OverwrittenApiKey',
114+
},
115+
body: { title: 'Overwritten Title' },
116+
},
117+
});
118+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
119+
// @ts-expect-error
120+
expect(fetchMock.mock.calls[0][1]).toEqual({
121+
method: 'POST',
122+
headers: new Headers({
123+
'Content-Type': 'application/json',
124+
'X-MICROCMS-API-KEY': 'apiKey',
125+
}),
126+
body: JSON.stringify({ title: 'title' }),
127+
});
128+
});
129+
130+
test('Custom request init added method, headers and body parameters is not overwrited for fetch in delete request.', async () => {
131+
await client.delete({
132+
endpoint: 'list-type',
133+
content: { title: 'title' },
134+
customRequestInit: {
135+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
136+
// @ts-expect-error
137+
method: 'GET',
138+
headers: {
139+
'X-MICROCMS-API-KEY': 'OverwrittenApiKey',
140+
},
141+
body: { title: 'Overwritten Title' },
142+
},
143+
});
144+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
145+
// @ts-expect-error
146+
expect(fetchMock.mock.calls[0][1]).toEqual({
147+
method: 'POST',
148+
headers: new Headers({
149+
'Content-Type': 'application/json',
150+
'X-MICROCMS-API-KEY': 'apiKey',
151+
}),
152+
body: JSON.stringify({ title: 'title' }),
153+
});
154+
});
155+
});

0 commit comments

Comments
 (0)