Completed
Push — master ( dc50e4...8acfd5 )
by
unknown
51s
created

test/callAPIMethod.spec.js   A

Complexity

Total Complexity 2
Complexity/F 1

Size

Lines of Code 19
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 2 Features 0
Metric Value
cc 0
wmc 2
c 2
b 2
f 0
nc 1
mnd 0
bc 2
fnc 2
dl 0
loc 19
rs 10
bpm 1
cpm 1
noi 0
1
import test from 'ava';
2
import sinon from 'sinon';
3
import fetchMock from 'fetch-mock';
4
import callAPIMethod from '../src/callAPIMethod';
5
import APIError from '../src/error';
6
7
const matcher = '*';
8
9
const ResponseGood = new Response(
10
    JSON.stringify({ sample: 'data'}),
11
    {
12
        status: 200
13
    }
14
);
15
16
const ResponseBad = new Response(null, {
17
    status: 500
18
});
19
20
test.serial('default params', async t => {
21
    fetchMock.get(matcher, {});
22
    await callAPIMethod();
23
24
    t.is(fetchMock.lastUrl(matcher), '/api', 'correct url');
25
    t.deepEqual(fetchMock.lastOptions(matcher), {
26
        cache: 'default',
27
        credentials: 'include',
28
        method: 'GET',
29
        mode: 'cors'
30
    }, 'correct options');
31
32
    fetchMock.restore();
33
});
34
35
test.serial('200 reponse', async t => {
36
    fetchMock.get(matcher, ResponseGood);
37
    const result = await callAPIMethod();
38
39
    t.deepEqual(result, { sample: 'data' }, 'correct response body');
40
41
    fetchMock.restore();
42
});
43
44
45
test.serial('500 reponse', async t => {
46
    fetchMock.get(matcher, ResponseBad);
47
48
    const result = await callAPIMethod();
49
50
    t.true(result instanceof Error, 'instance of Error');
51
    t.true(result instanceof APIError, 'instance of APIError');
52
53
    fetchMock.restore();
54
});
55
56
test.serial('fetch options', async t => {
57
    fetchMock.post(matcher, ResponseGood);
58
59
    const APINamespace = 'rest-api';
60
    const namespace = 'user';
61
    const fetchOptions = { method: 'POST'};
62
    const methodOptions = {
63
        path: 'path',
64
        query: { edit: true },
65
        options: {
66
            credentials: 'omit'
67
        },
68
        method: 'text'
69
    };
70
    const spyResponseGood = sinon.spy(ResponseGood, 'text');
71
72
    fetchMock.post(matcher, {});
73
74
    await callAPIMethod(APINamespace, fetchOptions, namespace, methodOptions);
75
76
    t.is(fetchMock.lastUrl(matcher), '/rest-api/user/path?edit=true', 'correct url');
77
    t.deepEqual(fetchMock.lastOptions(matcher), {
78
        cache: 'default',
79
        credentials: 'omit',
80
        method: 'POST',
81
        mode: 'cors'
82
    }), 'correct options';
83
    t.true(spyResponseGood.calledOnce);
84
85
    spyResponseGood.restore();
86
    fetchMock.restore();
87
});
88
89
test.serial('unsupported Response method', async t => {
90
    fetchMock.get(matcher, ResponseGood);
91
92
    const APINamespace = 'rest-api';
93
    const namespace = 'user';
94
    const fetchOptions = { method: 'POST'};
95
    const methodOptions = {
96
        path: 'path',
97
        options: {
98
            credentials: 'omit'
99
        },
100
        method: 'undefined'
101
    };
102
103
    const result = await callAPIMethod(APINamespace, namespace, fetchOptions, methodOptions);
104
105
    t.true(result instanceof Error);
106
    t.true(result instanceof TypeError);
107
    t.false(result instanceof APIError);
108
109
    fetchMock.restore();
110
});