-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.js
More file actions
82 lines (73 loc) · 2.24 KB
/
index.test.js
File metadata and controls
82 lines (73 loc) · 2.24 KB
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// @flow
import {
buildUrl,
parseRawParams,
buildReqUrl,
callApi
} from './index';
const todoParams = { todo: { description: 'this is a todo.' } };
describe('libs/utils/api', () => {
describe('{ buildUrl }', () => {
it('combines a path and an object of key values into a url with a query', () => {
const path = 'example.com';
const query = { page: 1 };
const actual = buildUrl(path, query);
const expected = 'example.com?page=1';
expect(actual).toBe(expected);
});
it('removes keys that have null values', () => {
const path = 'example.com';
const query = { page: null };
const actual = buildUrl(path, query);
const expected = 'example.com?';
expect(actual).toBe(expected);
});
});
describe('{ parseRawParams }', () => {
it('accepts todo params', () => {
const url = '/todos';
const actual = parseRawParams('GET', { url, data: todoParams });
const expected = {
method: 'GET',
url: '/todos',
data: {
todo: {
description: 'this is a todo.'
}}};
expect(actual).toMatchObject(expected);
});
it('accepts remote URLs', () => {
const url = 'http://localhost:3000/api/v1/todos';
const actual = parseRawParams(
'GET', { remote: true, url, data: todoParams });
const expected = {
method: 'GET',
url: 'http://localhost:3000/api/v1/todos',
data: {
todo: {
description: 'this is a todo.'
}}};
expect(actual).toMatchObject(expected);
});
});
describe('callApi', () => {
it('handles 400 error with an exception', async () => {
const url = 'http://fakeurl';
fetch.mockResponseOnce(JSON.stringify([{description: "add a todo"}]), {status: 400});
try {
const resp = await callApi('GET', { url });
} catch (error) {
expect.anything();
}
});
it('handles 500 error with an exception', async () => {
const url = 'http://fakeurl';
fetch.mockResponseOnce(JSON.stringify([{description: "add a todo"}]), {status: 500});
try {
const resp = await callApi('GET', { url });
} catch (error) {
expect.anything();
}
});
});
});