-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
json.request.headers.test.ts
54 lines (42 loc) · 1.43 KB
/
json.request.headers.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import { allSettled, fork } from 'effector';
import { describe, test, expect, vi } from 'vitest';
import { fetchFx } from '../fetch';
import { createJsonApiRequest } from '../json';
describe('fetch/json.request.headers', () => {
// Does not matter
const request = {
url: 'https://api.salo.com',
credentials: 'same-origin' as const,
};
test('add Context-Type header for POST request', async () => {
const callJsonApiFx = createJsonApiRequest({
request: { ...request, method: 'POST' as const },
});
const fetchMock = vi.fn().mockResolvedValue(new Response('Ok'));
const scope = fork({ handlers: [[fetchFx, fetchMock]] });
await allSettled(callJsonApiFx, {
scope,
params: { body: { some: 'test' } },
});
expect(fetchMock.mock.calls[0][0].headers).toEqual(
new Headers({
Accept: 'application/json',
'content-type': 'application/json',
})
);
});
test('DO NOT add Context-Type header for GET request', async () => {
const callJsonApiFx = createJsonApiRequest({
request: { ...request, method: 'GET' as const },
});
const fetchMock = vi.fn().mockResolvedValue(new Response('Ok'));
const scope = fork({ handlers: [[fetchFx, fetchMock]] });
await allSettled(callJsonApiFx, {
scope,
params: {},
});
expect(fetchMock.mock.calls[0][0].headers).toEqual(
new Headers({ Accept: 'application/json' })
);
});
});