Skip to content

Commit 7b9fced

Browse files
committed
✅ Add test for each req. method
1 parent 5dcd611 commit 7b9fced

File tree

1 file changed

+93
-14
lines changed
  • packages/@ackee/antonio-core/src/modules/core/models/__tests__

1 file changed

+93
-14
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,117 @@
11
import 'whatwg-fetch';
2+
import Headers from 'fetch-headers';
23
import { generatorToPromise as toPromise } from '../../makeRequest';
34
import { Antonio } from '../Antonio';
45

5-
describe('Antonio class', () => {
6+
interface User {
7+
id: string;
8+
}
9+
10+
type Users = User[];
11+
12+
/**
13+
README:
14+
The API never actually update the database and returns stale data.
15+
That's why no test below checks if the returned data contains data from a request.
16+
*/
17+
18+
describe('Antonio', () => {
619
let api: Antonio;
20+
const BASE_URL = 'https://jsonplaceholder.typicode.com/';
721

822
beforeEach(() => {
923
api = new Antonio({
10-
baseURL: 'https://jsonplaceholder.typicode.com/',
24+
baseURL: BASE_URL,
1125
});
1226
});
1327

1428
afterEach(() => {
1529
api.destroy();
1630
});
1731

18-
it('fetch users', async () => {
19-
const { data, response, request } = await toPromise(api.get('/users'));
32+
it('has correct default config', () => {
33+
expect(api.defaults).toEqual({
34+
baseURL: BASE_URL,
35+
headers: new Headers(),
36+
});
37+
});
38+
39+
it('has interceptors interface setup', () => {
40+
expect(api.interceptors).toHaveProperty('request');
41+
expect(api.interceptors).toHaveProperty('response');
42+
});
43+
44+
it('fetches users', async () => {
45+
const { data, response, request } = await toPromise(api.get<Users>('/users'));
2046

2147
expect(data).toBeInstanceOf(Array);
2248
expect(response).toBeInstanceOf(Response);
2349
expect(request).toBeInstanceOf(Request);
2450
});
2551

26-
// TODO:
27-
it.skip('create a user', async () => {
28-
interface User {
29-
name: string;
30-
}
31-
const user: User = {
32-
name: 'Alois',
33-
};
34-
const { data } = await toPromise(api.post('/users', user));
52+
it('deletes an user', async () => {
53+
await toPromise(
54+
api.delete('/users/:id', {
55+
uriParams: { id: 1 },
56+
}),
57+
);
58+
});
59+
60+
it('updates an user', async () => {
61+
const { data } = await toPromise(
62+
api.put<User>(
63+
'/users/:id',
64+
{ user: 'Alois' },
65+
{
66+
uriParams: { id: 1 },
67+
},
68+
),
69+
);
70+
71+
expect(data).toEqual({ id: 1 });
72+
});
73+
74+
it('fetches an user', async () => {
75+
const { data } = await toPromise(
76+
api.get<User>('/users/:id', {
77+
uriParams: { id: 1 },
78+
}),
79+
);
80+
81+
expect(data).toHaveProperty('id');
82+
expect(data.id).toBe(1);
83+
});
84+
85+
it('does an patch update of an user', async () => {
86+
const { data } = await toPromise(
87+
api.get<User>('/users/:id', {
88+
uriParams: { id: 1 },
89+
}),
90+
);
91+
92+
expect(data).toHaveProperty('id');
93+
expect(data.id).toBe(1);
94+
});
95+
96+
it('creates a user', async () => {
97+
const { data } = await toPromise(
98+
api.post<User>('/users', {
99+
name: 'Alois',
100+
}),
101+
);
102+
103+
expect(data).toHaveProperty('id');
104+
});
105+
106+
it('makes an HEAD req.', async () => {
107+
const result = await toPromise(api.head('/users'));
108+
109+
expect(result.data).toBe(null);
110+
});
111+
112+
it('makes an OPTIONS req.', async () => {
113+
const result = await toPromise(api.options('/users'));
35114

36-
expect(data).toContain(user);
115+
expect(result.data).toBe(null);
37116
});
38117
});

0 commit comments

Comments
 (0)