Skip to content

Commit

Permalink
refactor: migrate BrowserCollection class to ES2015
Browse files Browse the repository at this point in the history
  • Loading branch information
devoto13 committed Mar 15, 2018
1 parent bb012e2 commit 5809653
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 82 deletions.
89 changes: 43 additions & 46 deletions lib/browser_collection.js
@@ -1,49 +1,48 @@
var EXECUTING = require('./browser').STATE_EXECUTING
var Result = require('./browser_result')
'use strict'

var Collection = function (emitter, browsers) {
browsers = browsers || []
const EXECUTING = require('./browser').STATE_EXECUTING
const Result = require('./browser_result')

this.add = function (browser) {
browsers.push(browser)
emitter.emit('browsers_change', this)
class BrowserCollection {
constructor (emitter, browsers) {
this.browsers = browsers || []
this.emitter = emitter
}

this.remove = function (browser) {
var index = browsers.indexOf(browser)
add (browser) {
this.browsers.push(browser)
this.emitter.emit('browsers_change', this)
}

remove (browser) {
const index = this.browsers.indexOf(browser)

if (index === -1) {
return false
}

browsers.splice(index, 1)
emitter.emit('browsers_change', this)
this.browsers.splice(index, 1)
this.emitter.emit('browsers_change', this)

return true
}

this.getById = function (browserId) {
for (var i = 0; i < browsers.length; i++) {
if (browsers[i].id === browserId) {
return browsers[i]
}
}

return null
getById (browserId) {
return this.browsers.find((browser) => browser.id === browserId) || null
}

this.setAllToExecuting = function () {
browsers.forEach(function (browser) {
setAllToExecuting () {
this.browsers.forEach((browser) => {
browser.state = EXECUTING
})

emitter.emit('browsers_change', this)
this.emitter.emit('browsers_change', this)
}

this.areAllReady = function (nonReadyList) {
areAllReady (nonReadyList) {
nonReadyList = nonReadyList || []

browsers.forEach(function (browser) {
this.browsers.forEach((browser) => {
if (!browser.isReady()) {
nonReadyList.push(browser)
}
Expand All @@ -52,14 +51,12 @@ var Collection = function (emitter, browsers) {
return nonReadyList.length === 0
}

this.serialize = function () {
return browsers.map(function (browser) {
return browser.serialize()
})
serialize () {
return this.browsers.map((browser) => browser.serialize())
}

this.getResults = function () {
var results = browsers.reduce(function (previous, current) {
getResults () {
const results = this.browsers.reduce((previous, current) => {
previous.success += current.lastResult.success
previous.failed += current.lastResult.failed
previous.error = previous.error || current.lastResult.error
Expand All @@ -74,32 +71,32 @@ var Collection = function (emitter, browsers) {
}

// TODO(vojta): can we remove this? (we clear the results per browser in onBrowserStart)
this.clearResults = function () {
browsers.forEach(function (browser) {
clearResults () {
this.browsers.forEach((browser) => {
browser.lastResult = new Result()
})
}

this.clone = function () {
return new Collection(emitter, browsers.slice())
clone () {
return new BrowserCollection(this.emitter, this.browsers.slice())
}

// Array APIs
this.map = function (callback, context) {
return browsers.map(callback, context)
map (callback, context) {
return this.browsers.map(callback, context)
}

this.forEach = function (callback, context) {
return browsers.forEach(callback, context)
forEach (callback, context) {
return this.browsers.forEach(callback, context)
}

// this.length
Object.defineProperty(this, 'length', {
get: function () {
return browsers.length
}
})
get length () {
return this.browsers.length
}
}

BrowserCollection.factory = function (emitter) {
return new BrowserCollection(emitter)
}
Collection.$inject = ['emitter']

module.exports = Collection
module.exports = BrowserCollection
2 changes: 1 addition & 1 deletion lib/server.js
Expand Up @@ -99,7 +99,7 @@ var Server = function (cliOptions, done) {
// TODO(vojta): remove, once karma-dart does not rely on it
customScriptTypes: ['value', []],
reporter: ['factory', reporter.createReporters],
capturedBrowsers: ['type', BrowserCollection],
capturedBrowsers: ['factory', BrowserCollection.factory],
args: ['value', {}],
timer: ['value', {
setTimeout: function () {
Expand Down
72 changes: 37 additions & 35 deletions test/unit/browser_collection.spec.js
@@ -1,9 +1,11 @@
'use strict'

describe('BrowserCollection', () => {
var emitter
var e = require('../../lib/events')
var Collection = require('../../lib/browser_collection')
var Browser = require('../../lib/browser')
var collection = emitter = null
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()
Expand All @@ -18,7 +20,7 @@ describe('BrowserCollection', () => {
})

it('should fire "browsers_change" event', () => {
var spy = sinon.spy()
const spy = sinon.spy()
emitter.on('browsers_change', spy)
collection.add({})
expect(spy).to.have.been.called
Expand All @@ -27,7 +29,7 @@ describe('BrowserCollection', () => {

describe('remove', () => {
it('should remove given browser', () => {
var browser = new Browser('id')
const browser = new Browser('id')
collection.add(browser)

expect(collection.length).to.equal(1)
Expand All @@ -36,8 +38,8 @@ describe('BrowserCollection', () => {
})

it('should fire "browsers_change" event', () => {
var spy = sinon.spy()
var browser = new Browser('id')
const spy = sinon.spy()
const browser = new Browser('id')
collection.add(browser)

emitter.on('browsers_change', spy)
Expand All @@ -46,7 +48,7 @@ describe('BrowserCollection', () => {
})

it('should return false if given browser does not exist within the collection', () => {
var spy = sinon.spy()
const spy = sinon.spy()
emitter.on('browsers_change', spy)
expect(collection.remove({})).to.equal(false)
expect(spy).not.to.have.been.called
Expand All @@ -55,7 +57,7 @@ describe('BrowserCollection', () => {

describe('getById', () => {
it('should find the browser by id', () => {
var browser = new Browser(123)
const browser = new Browser(123)
collection.add(browser)

expect(collection.getById(123)).to.equal(browser)
Expand All @@ -70,7 +72,7 @@ describe('BrowserCollection', () => {
})

describe('setAllToExecuting', () => {
var browsers = null
let browsers = null

beforeEach(() => {
browsers = [new Browser(), new Browser(), new Browser()]
Expand All @@ -88,15 +90,15 @@ describe('BrowserCollection', () => {
})

it('should fire "browsers_change" event', () => {
var spy = sinon.spy()
const spy = sinon.spy()
emitter.on('browsers_change', spy)
collection.setAllToExecuting()
expect(spy).to.have.been.called
})
})

describe('areAllReady', () => {
var browsers = null
let browsers = null

beforeEach(() => {
browsers = [new Browser(), new Browser(), new Browser()]
Expand All @@ -118,15 +120,15 @@ describe('BrowserCollection', () => {
it('should add all non-ready browsers into given array', () => {
browsers[0].state = Browser.STATE_EXECUTING
browsers[1].state = Browser.STATE_EXECUTING_DISCONNECTED
var nonReady = []
const nonReady = []
collection.areAllReady(nonReady)
expect(nonReady).to.deep.equal([browsers[0], browsers[1]])
})
})

describe('serialize', () => {
it('should return plain array with serialized browsers', () => {
var browsers = [new Browser('1'), new Browser('2')]
const browsers = [new Browser('1'), new Browser('2')]
browsers[0].name = 'B 1.0'
browsers[1].name = 'B 2.0'
collection.add(browsers[0])
Expand All @@ -141,25 +143,25 @@ describe('BrowserCollection', () => {

describe('getResults', () => {
it('should return sum of all browser results', () => {
var browsers = [new Browser(), new Browser()]
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

var results = collection.getResults()
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', () => {
var browsers = [new Browser(), new Browser()]
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])

var results = collection.getResults()
let results = collection.getResults()
expect(results.disconnected).to.equal(false)

browsers[0].lastResult.disconnected = true
Expand All @@ -176,11 +178,11 @@ describe('BrowserCollection', () => {
})

it('should compute error true if any browser had error', () => {
var browsers = [new Browser(), new Browser()]
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])

var results = collection.getResults()
let results = collection.getResults()
expect(results.error).to.equal(false)

browsers[0].lastResult.error = true
Expand All @@ -197,11 +199,11 @@ describe('BrowserCollection', () => {
})

it('should compute exitCode', () => {
var browsers = [new Browser(), new Browser()]
browsers.forEach(collection.add)
const browsers = [new Browser(), new Browser()]
browsers.forEach((browser) => collection.add(browser))

browsers[0].lastResult.success = 2
var results = collection.getResults()
let results = collection.getResults()
expect(results.exitCode).to.equal(0)

browsers[0].lastResult.failed = 2
Expand All @@ -227,7 +229,7 @@ describe('BrowserCollection', () => {
describe('clearResults', () => {
it('should clear all results', () => {
// Date.now.returns 112233
var browsers = [new Browser(), new Browser()]
const browsers = [new Browser(), new Browser()]
collection.add(browsers[0])
collection.add(browsers[1])
browsers[0].lastResult.sucess++
Expand All @@ -249,10 +251,10 @@ describe('BrowserCollection', () => {

describe('clone', () => {
it('should create a shallow copy of the collection', () => {
var browsers = [new Browser(), new Browser(), new Browser()]
browsers.forEach(collection.add)
const browsers = [new Browser(), new Browser(), new Browser()]
browsers.forEach((browser) => collection.add(browser))

var clone = collection.clone()
const clone = collection.clone()
expect(clone.length).to.equal(3)

clone.remove(browsers[0])
Expand All @@ -263,21 +265,21 @@ describe('BrowserCollection', () => {

describe('map', () => {
it('should have map()', () => {
var browsers = [new Browser(1), new Browser(2), new Browser(3)]
browsers.forEach(collection.add)
const browsers = [new Browser(1), new Browser(2), new Browser(3)]
browsers.forEach((browser) => collection.add(browser))

var mappedIds = collection.map((browser) => browser.id)
const mappedIds = collection.map((browser) => browser.id)

expect(mappedIds).to.deep.equal([1, 2, 3])
})
})

describe('forEach', () => {
it('should have forEach()', () => {
var browsers = [new Browser(0), new Browser(1), new Browser(2)]
browsers.forEach(collection.add)
const browsers = [new Browser(0), new Browser(1), new Browser(2)]
browsers.forEach((browser) => collection.add(browser))

collection.forEach(function (browser, index) {
collection.forEach((browser, index) => {
expect(browser.id).to.equal(index)
})
})
Expand Down

0 comments on commit 5809653

Please sign in to comment.