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

test/createAPI.spec.js   A

Complexity

Total Complexity 16
Complexity/F 1

Size

Lines of Code 105
Function Count 16

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 2
Metric Value
cc 0
wmc 16
c 3
b 1
f 2
nc 1
mnd 0
bc 8
fnc 16
dl 0
loc 105
rs 10
bpm 0.5
cpm 1
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
A createAPI.spec.js ➔ ??? 0 1 1
1
import test from 'ava';
2
import sinon from 'sinon';
3
import * as applyMiddleware from '../src/applyMiddleware';
4
import callAPIMethod from '../src/callAPIMethod';
5
import createAPI from '../src/createAPI';
6
7
let stubApplyMiddleware;
8
let stubCallAPIMethod;
9
const boundCallAPIMethod = _ => _;
10
test.beforeEach(t => {
0 ignored issues
show
Unused Code introduced by
The parameter t is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
11
    stubCallAPIMethod = sinon.stub(callAPIMethod, 'bind');
12
    stubCallAPIMethod.returns(boundCallAPIMethod);
13
    stubApplyMiddleware = sinon.stub(applyMiddleware, 'default');
14
});
15
test.afterEach.always(t => {
0 ignored issues
show
Unused Code introduced by
The parameter t is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
16
    stubApplyMiddleware.restore();
17
    stubCallAPIMethod.restore();
18
});
19
20
test('undefined resources', t => {
21
    const API = createAPI();
22
    t.deepEqual(API, {}, 'empty API object');
23
});
24
25
test('0 resources', t => {
26
    const API = createAPI({});
27
    t.deepEqual(API, {}, 'empty API object');
28
});
29
30
test('supports resource prefix insted of namespace', t => {
31
    const user = {
32
        prefix: 'user-endpoint',
33
        methods: {
34
            get: ({ id }) => ({ path: ['get', id] })
35
        }
36
    };
37
    const resources = { user };
38
    const API = createAPI(resources, [], 'https://example.com');
39
40
    API.user.get(1);
41
42
    t.true(stubCallAPIMethod.calledWithExactly(
43
        null, 'https://example.com', {}, 'user-endpoint'
44
    ), 'correct arguments passed to callAPIMethod');
45
});
46
47
test('general behaviour', t => {
48
    const userResource = {
49
        namespace: 'user',
50
        methods: {
51
            get: ({ id }) => ({ path: ['get', id] }),
52
            delete: ({ id }) => ({ path: ['delete', id], options: { method: 'DELETE'} })
53
        }
54
    };
55
    const projectResource = {
56
        namespace: 'project',
57
        methods: {
58
            get: ({ id }) => ({ path: ['get', id] }),
59
            delete: ({ id }) => ({ path: ['delete', id], options: { method: 'DELETE'} })
60
        }
61
    };
62
63
    const resources = {
64
        user: userResource,
65
        project: projectResource
66
    };
67
68
    const middleware = [ _ => _, _ => _];
69
70
    const API = createAPI(resources, middleware, 'https://example.com');
71
72
    t.deepEqual(Object.keys(API), ['user', 'project'], 'resources');
73
74
    Object.keys(API).forEach(resource => {
75
        t.deepEqual(Object.keys(API[resource]), ['get', 'delete'], 'methods');
76
        Object.keys(API[resource]).forEach(method => {
77
            t.true(API[resource][method] instanceof Function, 'methods are functions');
78
        });
79
    });
80
81
    const methodOptions = { specific: 'option' };
82
    API.user.delete({ id: 1 }, methodOptions);
83
84
    t.true(stubCallAPIMethod.calledOnce);
85
    t.true(stubApplyMiddleware.calledOnce);
86
    t.true(stubCallAPIMethod.calledBefore(stubApplyMiddleware));
87
    t.true(stubCallAPIMethod.calledWithExactly(
88
        null, 'https://example.com', {}, 'user'
89
    ));
90
91
    t.is(stubApplyMiddleware.lastCall.args[0], boundCallAPIMethod, 'stubApplyMiddleware called with boundCallAPIMethod');
92
    t.is(stubApplyMiddleware.lastCall.args[1], middleware, 'stubApplyMiddleware called with middleware');
93
    t.is(stubApplyMiddleware.lastCall.args[2], methodOptions, 'stubApplyMiddleware called with methodOptions');
94
    t.deepEqual(stubApplyMiddleware.lastCall.args[3], {
95
        path: ['delete', 1], options: { method: 'DELETE'}
96
    }, 'stubApplyMiddleware called with correct apiParams');
97
    t.is(stubApplyMiddleware.lastCall.args[4], 'user', 'stubApplyMiddleware called with correct resourceId');
98
    t.is(stubApplyMiddleware.lastCall.args[5], 'delete', 'stubApplyMiddleware called with correct method');
99
100
    API.user.delete({ id: 1 });
101
102
    t.true(stubCallAPIMethod.calledTwice);
103
    t.true(stubApplyMiddleware.calledTwice);
104
    t.true(stubCallAPIMethod.calledBefore(stubApplyMiddleware));
105
});