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