/
RestResourcePipe.integration.spec.js
141 lines (126 loc) · 3.26 KB
/
RestResourcePipe.integration.spec.js
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
var RestResourceFactory = require('../RestResource');
var sinon = require('sinon');
var PromisePipe = require('promise-pipe')();
var MockId = {
id: 984,
foo: {
id: 123
}
};
var MockItem = {
id: 984,
name: "MockItem"
};
var MockFoo = {
id: 123,
name: "MockFoo"
};
var MockItems = [{
name: "MockItem1"
},{
name: "MockItem2"
},{
name: "MockItem3"
}];
describe('piped API', function(){
var API;
var sandbox;
var RestResource;
var agent;
var resp;
var pipe;
beforeEach(function(){
sandbox = sinon.sandbox.create()
resp = {
on: sandbox.spy(),
end: sandbox.stub(),
send: sandbox.stub()
}
agent = {
get: sandbox.stub(),
post: sandbox.stub(),
put: sandbox.stub(),
del: sandbox.stub(),
patch: sandbox.stub()
};
resp.end.callsArgWith(0, {body:"OK"});
agent.get.returns(resp);
agent.post.returns(resp);
agent.put.returns(resp);
agent.del.returns(resp);
agent.patch.returns(resp);
RestResource = RestResourceFactory(agent);
API = RestResource('user', '/api/users');
API.withUsers.createCRUD('item', '/items');
API.withUser.createCRUD('foo', '/foo');
PromisePipe.use('api', API);
});
afterEach(function(){
sandbox.restore();
})
describe('readUsers', function(){
beforeEach(function(done){
pipe = PromisePipe().api.readUsers();
pipe({},{}).then(function(){done()});
})
it('should get /api/users', function(){
sinon.assert.calledWith(agent.get, '/api/users')
})
})
describe('withUsers.readItems', function(){
beforeEach(function(done){
pipe = PromisePipe().api.withUsers.readItems();
pipe({},{}).then(function(){done()});
})
it('should get /api/users/items', function(){
sinon.assert.calledWith(agent.get, '/api/users/items')
})
})
describe('withUsers.readItem', function(){
beforeEach(function(done){
pipe = PromisePipe().api.withUsers.readItem();
pipe({item:{id:5}},{}).then(function(){done()});
})
it('should get /api/users/items', function(){
sinon.assert.calledWith(agent.get, '/api/users/items/5')
})
})
describe('withUsers.createItems', function(){
beforeEach(function(done){
pipe = PromisePipe().api.withUsers.createItem();
pipe(MockItem,{}).then(function(){done()});
})
it('should post /api/users/items', function(){
sinon.assert.calledWith(agent.post, '/api/users/items')
sinon.assert.calledWith(resp.send, MockItem)
})
})
describe('withUsers.updateItems', function(){
beforeEach(function(done){
pipe = PromisePipe().api.withUsers.updateItems();
pipe(MockItems,{}).then(function(){done()});
})
it('should put /api/users/items', function(){
sinon.assert.calledWith(agent.put, '/api/users/items')
sinon.assert.calledWith(resp.send, MockItems)
})
})
describe('deleteItems', function(){
beforeEach(function(done){
pipe = PromisePipe().api.deleteUsers();
pipe(MockItems,{}).then(function(){done()});
})
it('should del /api/users', function(){
sinon.assert.calledWith(agent.del, '/api/users')
})
})
describe('withUser.readItem', function(){
beforeEach(function(done){
pipe = PromisePipe().api.withUser.readFoo();
pipe(MockId,{}).then(function(){done()});
})
it('should del /api/user/:id/foo/:id', function(){
sinon.assert.calledWith(agent.get, '/api/users/' + MockId.id +'/foo/' + MockId.foo.id)
})
})
});