-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
browser_collection.spec.js
347 lines (297 loc) · 11.3 KB
/
browser_collection.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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
'use strict'
describe('BrowserCollection', () => {
let emitter
const e = require('../../lib/events')
const Collection = require('../../lib/browser_collection')
const Browser = require('../../lib/browser')
let collection = emitter = null
beforeEach(() => {
emitter = new e.EventEmitter()
collection = new Collection(emitter)
})
describe('add', () => {
it('should add browser', () => {
expect(collection.length).to.equal(0)
collection.add(new Browser('id'))
expect(collection.length).to.equal(1)
})
it('should fire "browsers_change" event', () => {
const spy = sinon.spy()
emitter.on('browsers_change', spy)
collection.add({})
expect(spy).to.have.been.called
})
})
describe('remove', () => {
it('should remove given browser', () => {
const browser = new Browser('id')
collection.add(browser)
expect(collection.length).to.equal(1)
expect(collection.remove(browser)).to.equal(true)
expect(collection.length).to.equal(0)
})
it('should fire "browsers_change" event', () => {
const spy = sinon.spy()
const browser = new Browser('id')
collection.add(browser)
emitter.on('browsers_change', spy)
collection.remove(browser)
expect(spy).to.have.been.called
})
it('should return false if given browser does not exist within the collection', () => {
const spy = sinon.spy()
emitter.on('browsers_change', spy)
expect(collection.remove({})).to.equal(false)
expect(spy).not.to.have.been.called
})
})
describe('getById', () => {
it('should find the browser by id', () => {
const browser = new Browser(123)
collection.add(browser)
expect(collection.getById(123)).to.equal(browser)
})
it('should return null if no browser with given id', () => {
expect(collection.getById(123)).to.equal(null)
collection.add(new Browser(456))
expect(collection.getById(123)).to.equal(null)
})
})
describe('areAllReady', () => {
let browsers = null
beforeEach(() => {
browsers = [new Browser(), new Browser(), new Browser()]
browsers.forEach((browser) => {
browser.state = Browser.STATE_CONNECTED
collection.add(browser)
})
})
it('should return true if all browsers are ready', () => {
expect(collection.areAllReady()).to.equal(true)
})
it('should return false if at least one browser is not ready', () => {
browsers[1].state = Browser.STATE_EXECUTING
expect(collection.areAllReady()).to.equal(false)
})
it('should add all non-ready browsers into given array', () => {
browsers[0].state = Browser.STATE_EXECUTING
browsers[1].state = Browser.STATE_EXECUTING_DISCONNECTED
const nonReady = []
collection.areAllReady(nonReady)
expect(nonReady).to.deep.equal([browsers[0], browsers[1]])
})
})
describe('serialize', () => {
it('should return plain array with serialized browsers', () => {
const browsers = [new Browser('1'), new Browser('2')]
browsers[0].name = 'B 1.0'
browsers[1].name = 'B 2.0'
collection.add(browsers[0])
collection.add(browsers[1])
expect(collection.serialize()).to.deep.equal([
{id: '1', name: 'B 1.0', isConnected: true},
{id: '2', name: 'B 2.0', isConnected: true}
])
})
})
describe('getResults', () => {
it('should return sum of all browser results', () => {
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])
browsers[0].lastResult.success = 2
browsers[0].lastResult.failed = 3
browsers[1].lastResult.success = 4
browsers[1].lastResult.failed = 5
const results = collection.getResults()
expect(results.success).to.equal(6)
expect(results.failed).to.equal(8)
})
it('should compute disconnected true if any browser got disconnected', () => {
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])
let results = collection.getResults()
expect(results.disconnected).to.equal(false)
browsers[0].lastResult.disconnected = true
results = collection.getResults()
expect(results.disconnected).to.equal(true)
browsers[1].lastResult.disconnected = true
results = collection.getResults()
expect(results.disconnected).to.equal(true)
browsers[0].lastResult.disconnected = false
results = collection.getResults()
expect(results.disconnected).to.equal(true)
})
it('should compute error true if any browser had error', () => {
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])
let results = collection.getResults()
expect(results.error).to.equal(false)
browsers[0].lastResult.error = true
results = collection.getResults()
expect(results.error).to.equal(true)
browsers[1].lastResult.error = true
results = collection.getResults()
expect(results.error).to.equal(true)
browsers[0].lastResult.error = false
results = collection.getResults()
expect(results.error).to.equal(true)
})
it('should compute exitCode', () => {
const browsers = [new Browser(), new Browser()]
browsers.forEach((browser) => collection.add(browser))
browsers[0].lastResult.success = 2
let results = collection.getResults()
expect(results.exitCode).to.equal(0)
browsers[0].lastResult.failed = 2
results = collection.getResults()
expect(results.exitCode).to.equal(1)
browsers[0].lastResult.failed = 0
browsers[1].lastResult.error = true
results = collection.getResults()
expect(results.exitCode).to.equal(1)
browsers[0].lastResult.disconnected = true
browsers[1].lastResult.error = false
results = collection.getResults()
expect(results.exitCode).to.equal(1)
browsers[0].lastResult.disconnected = false
results = collection.getResults()
expect(results.exitCode).to.equal(0)
})
})
describe('clearResults', () => {
it('should clear all results', () => {
// Date.now.returns 112233
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])
browsers[0].lastResult.sucess++
browsers[0].lastResult.error = true
browsers[1].lastResult.failed++
browsers[1].lastResult.skipped++
browsers[1].lastResult.disconnected = true
collection.clearResults()
browsers.forEach((browser) => {
expect(browser.lastResult.success).to.equal(0)
expect(browser.lastResult.failed).to.equal(0)
expect(browser.lastResult.skipped).to.equal(0)
expect(browser.lastResult.error).to.equal(false)
expect(browser.lastResult.disconnected).to.equal(false)
})
})
})
describe('clone', () => {
it('should create a shallow copy of the collection', () => {
const browsers = [new Browser(), new Browser(), new Browser()]
browsers.forEach((browser) => collection.add(browser))
const clone = collection.clone()
expect(clone.length).to.equal(3)
clone.remove(browsers[0])
expect(clone.length).to.equal(2)
expect(collection.length).to.equal(3)
})
})
describe('map', () => {
it('should have map()', () => {
const browsers = [new Browser(1), new Browser(2), new Browser(3)]
browsers.forEach((browser) => collection.add(browser))
const mappedIds = collection.map((browser) => browser.id)
expect(mappedIds).to.deep.equal([1, 2, 3])
})
})
describe('forEach', () => {
it('should have forEach()', () => {
const browsers = [new Browser(0), new Browser(1), new Browser(2)]
browsers.forEach((browser) => collection.add(browser))
collection.forEach((browser, index) => {
expect(browser.id).to.equal(index)
})
})
})
// ============================================================================
// server.calculateExitCode
// ============================================================================
describe('calculateExitCode', () => {
const EXIT_CODE_ERROR = 1
const EXIT_CODE_SUCCESS = 0
describe('no tests', () => {
const results = {
success: 0,
failed: 0,
error: true
}
it('shall pass if failOnEmptyTestSuite not is set', () => {
const res = collection.calculateExitCode(results)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
it('shall pass if failOnEmptyTestSuite is false', () => {
const res = collection.calculateExitCode(results, false, false)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
it('shall fail if failOnEmptyTestSuite is true', () => {
const res = collection.calculateExitCode(results, false, true)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
it('shall fail if failOnFailingTestSuite is set', () => {
const res = collection.calculateExitCode(results, false, true, true)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
})
describe('all test passed, no errors', () => {
const results = {
success: 10,
failed: 0,
error: false
}
it('shall fail if singleRunBrowserNotCaptured is true', () => {
const res = collection.calculateExitCode(results, true)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
it('shall pass if failOnEmptyTestSuite not is set', () => {
const res = collection.calculateExitCode(results, false)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
it('shall pass if failOnEmptyTestSuite not is set', () => {
const res = collection.calculateExitCode(results, false, false)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
it('shall pass if failOnFailingTestSuite is true', () => {
const res = collection.calculateExitCode(results, false, true, true)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
it('shall pass if failOnFailingTestSuite is false', () => {
const res = collection.calculateExitCode(results, false, true, false)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
})
describe('all test passed, with error', () => {
const results = {
success: 10,
failed: 5,
error: false
}
it('shall fail if singleRunBrowserNotCaptured is true', () => {
const res = collection.calculateExitCode(results, true)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
it('shall fail if failOnEmptyTestSuite not is set', () => {
const res = collection.calculateExitCode(results, false)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
it('shall fail if failOnEmptyTestSuite not is set', () => {
const res = collection.calculateExitCode(results, false, false)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
it('shall fail if failOnFailingTestSuite is true', () => {
const res = collection.calculateExitCode(results, false, true, true)
expect(res).to.be.equal(EXIT_CODE_ERROR)
})
it('shall pass if failOnFailingTestSuite is false', () => {
const res = collection.calculateExitCode(results, false, true, false)
expect(res).to.be.equal(EXIT_CODE_SUCCESS)
})
})
})
})