-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.js
193 lines (155 loc) · 5 KB
/
test.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
'use strict';
var _ = require('underscore'),
expect = require('expect.js'),
sinon = require('sinon'),
plugin = require('./lib');
describe('Shields plugin', function() {
var projectHandler, buildHandler, projectRevHandler;
var app = {
lib: {
logger: function() {
return {log: _.noop, error: _.noop};
}
},
projects: {},
builds: {},
express: {
get: function(route, handler) {
switch (route) {
case '/shields/:projectName.svg':
projectHandler = handler;
break;
case '/shields/builds/:id(\\d+).svg':
buildHandler = handler;
break;
case '/shields/:projectName/revs/:rev.svg':
projectRevHandler = handler;
break;
default:
break;
}
}
}
};
var res = {
sendStatus: sinon.spy(),
set: sinon.spy(),
end: sinon.spy()
};
var getReq = function(params) {
return {
params: params
};
};
var checkSvg = function(text) {
expect(res.set.firstCall.args[0]['Content-Type'])
.to.eql('image/svg+xml');
expect(res.end.firstCall.args[0]).match(new RegExp(text));
};
var statusesHash = {
done: 'passing',
'in-progress': 'in progress',
error: 'failing'
};
before(function() {
plugin.register(app);
});
beforeEach(function() {
res.sendStatus.reset();
res.set.reset();
res.end.reset();
});
describe('project', function() {
it('should be ok with unexisting project', function() {
app.projects.get = sinon.stub().returns(null);
projectHandler(getReq({projectName: 'unexisted'}), res);
expect(res.sendStatus.firstCall.args[0]).to.eql(404);
});
it('should be ok with project without builds', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub().callsArgWith(1, null, []);
projectHandler(getReq({projectName: 'ok'}), res);
checkSvg('unknown');
});
it('should be ok with getRecent error', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub().callsArgWith(1, new Error());
projectHandler(getReq({projectName: 'ok'}), res);
checkSvg('unknown');
});
it('should be ok with getRecent non-Error instance error', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub().callsArgWith(1, 'error');
projectHandler(getReq({projectName: 'ok'}), res);
checkSvg('unknown');
});
_(statusesHash).each(function(text, status) {
it('should be ok with `' + status + '` build', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub()
.callsArgWith(1, null, [{status: status}]);
projectHandler(getReq({projectName: 'ok'}), res);
checkSvg(text);
});
});
});
describe('build', function() {
it('should be ok with unexisting build', function() {
app.builds.get = sinon.stub().callsArgWith(1, null, null);
buildHandler(getReq({id: 777}), res);
expect(res.sendStatus.firstCall.args[0]).to.eql(404);
});
it('should be ok with get error', function() {
app.builds.get = sinon.stub().callsArgWith(1, new Error());
buildHandler(getReq({id: 1}), res);
expect(res.sendStatus.firstCall.args[0]).to.eql(404);
});
it('should be ok with getRecent non-Error instance error', function() {
app.builds.get = sinon.stub().callsArgWith(1, 'error');
buildHandler(getReq({id: 1}), res);
expect(res.sendStatus.firstCall.args[0]).to.eql(404);
});
_(statusesHash).each(function(text, status) {
it('should be ok with `' + status + '` build', function() {
app.builds.get = sinon.stub()
.callsArgWith(1, null, {status: status});
buildHandler(getReq({id: 1}), res);
checkSvg(text);
});
});
});
describe('project rev', function() {
it('should be ok with unexisting project', function() {
app.projects.get = sinon.stub().returns(null);
projectRevHandler(getReq({projectName: 'unexisted'}), res);
expect(res.sendStatus.firstCall.args[0]).to.eql(404);
});
it('should be ok when no builds with such rev', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub().callsArgWith(1, null, []);
projectRevHandler(getReq({projectName: 'ok'}), res);
checkSvg('unknown');
});
it('should be ok with getRecent error', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub().callsArgWith(1, new Error());
projectRevHandler(getReq({projectName: 'ok'}), res);
checkSvg('unknown');
});
it('should be ok with getRecent non-Error instance error', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub().callsArgWith(1, 'error');
projectRevHandler(getReq({projectName: 'ok'}), res);
checkSvg('unknown');
});
_(statusesHash).each(function(text, status) {
it('should be ok with `' + status + '` build', function() {
app.projects.get = sinon.stub().returns({});
app.builds.getRecent = sinon.stub()
.callsArgWith(1, null, [{status: status}]);
projectRevHandler(getReq({projectName: 'ok'}), res);
checkSvg(text);
});
});
});
});