|
1 | 1 | import 'whatwg-fetch';
|
| 2 | +import Headers from 'fetch-headers'; |
2 | 3 | import { generatorToPromise as toPromise } from '../../makeRequest';
|
3 | 4 | import { Antonio } from '../Antonio';
|
4 | 5 |
|
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', () => { |
6 | 19 | let api: Antonio;
|
| 20 | + const BASE_URL = 'https://jsonplaceholder.typicode.com/'; |
7 | 21 |
|
8 | 22 | beforeEach(() => {
|
9 | 23 | api = new Antonio({
|
10 |
| - baseURL: 'https://jsonplaceholder.typicode.com/', |
| 24 | + baseURL: BASE_URL, |
11 | 25 | });
|
12 | 26 | });
|
13 | 27 |
|
14 | 28 | afterEach(() => {
|
15 | 29 | api.destroy();
|
16 | 30 | });
|
17 | 31 |
|
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')); |
20 | 46 |
|
21 | 47 | expect(data).toBeInstanceOf(Array);
|
22 | 48 | expect(response).toBeInstanceOf(Response);
|
23 | 49 | expect(request).toBeInstanceOf(Request);
|
24 | 50 | });
|
25 | 51 |
|
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')); |
35 | 114 |
|
36 |
| - expect(data).toContain(user); |
| 115 | + expect(result.data).toBe(null); |
37 | 116 | });
|
38 | 117 | });
|
0 commit comments