Skip to content

Commit

Permalink
test(client): Move to assert and add shims
Browse files Browse the repository at this point in the history
  • Loading branch information
dignifiedquire committed Jan 19, 2016
1 parent f15f211 commit d4c2286
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 66 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@
"grunt-mocha-test": "^0.12.7",
"grunt-npm": "0.0.2",
"jasmine-core": "^2.3.4",
"json3": "^3.3.2",
"karma-browserify": "^4.1.2",
"karma-browserstack-launcher": "0.1.7",
"karma-chrome-launcher": "*",
Expand Down
89 changes: 45 additions & 44 deletions test/client/karma.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
// shim all the things
require('core-js/es5')
global.JSON = require('json3')
var sinon = require('sinon')
var chai = require('chai')
chai.use(require('sinon-chai'))
var expect = chai.expect
var assert = require('assert')

var Karma = require('../../client/karma')
var MockSocket = require('./mocks').Socket
Expand Down Expand Up @@ -31,10 +32,10 @@ describe('Karma', function () {
}

socket.emit('execute', config)
expect(startSpy).to.not.have.been.called
assert(!startSpy.called)

k.loaded()
expect(startSpy).to.have.been.calledWith(config)
assert(startSpy.calledWith(config))
})

it('should open a new window when useIFrame is false', function () {
Expand All @@ -43,24 +44,24 @@ describe('Karma', function () {
}

socket.emit('execute', config)
expect(k.start).to.not.have.been.called
assert(!k.start.called)

k.loaded()
expect(startSpy).to.have.been.calledWith(config)
expect(windowStub).to.have.been.calledWith('about:blank')
assert(startSpy.calledWith(config))
assert(windowStub.calledWith('about:blank'))
})

it('should stop execution', function () {
sinon.spy(k, 'complete')
socket.emit('stop')
expect(k.complete).to.have.been.called
assert(k.complete.called)
})

it('should not start execution if any error during loading files', function () {
k.error('syntax error', '/some/file.js', 11)
k.loaded()
sinon.spy(k, 'start')
expect(startSpy).to.not.have.been.called
assert(!startSpy.called)
})

it('should remove reference to start even after syntax error', function () {
Expand All @@ -69,11 +70,11 @@ describe('Karma', function () {
k.start = ADAPTER_START_FN
k.error('syntax error', '/some/file.js', 11)
k.loaded()
expect(k.start).to.not.be.eql(ADAPTER_START_FN)
assert.notEqual(k.start, ADAPTER_START_FN)

k.start = ADAPTER_START_FN
k.loaded()
expect(k.start).to.not.be.eql(ADAPTER_START_FN)
assert.notEqual(k.start, ADAPTER_START_FN)
})

it('should not set up context if there was an error', function () {
Expand All @@ -88,9 +89,9 @@ describe('Karma', function () {
k.error('page reload')
k.setupContext(mockWindow)

expect(mockWindow.__karma__).to.not.exist
expect(mockWindow.onbeforeunload).to.not.exist
expect(mockWindow.onerror).to.not.exist
assert(mockWindow.__karma__ == null)
assert(mockWindow.onbeforeunloadK == null)
assert(mockWindow.onerror == null)
})

it('should setup context if there was error but clearContext config is false', function () {
Expand All @@ -105,22 +106,22 @@ describe('Karma', function () {
k.error('page reload')
k.setupContext(mockWindow)

expect(mockWindow.__karma__).to.exist
expect(mockWindow.onbeforeunload).to.exist
expect(mockWindow.onerror).to.exist
assert(mockWindow.__karma__ != null)
assert(mockWindow.onbeforeunload != null)
assert(mockWindow.onerror != null)
})

it('should report navigator name', function () {
var spyInfo = sinon.spy(function (info) {
expect(info.name).to.be.eql('Fake browser name')
assert(info.name === 'Fake browser name')
})

windowNavigator.userAgent = 'Fake browser name'
windowLocation.search = ''
socket.on('register', spyInfo)
socket.emit('connect')

expect(spyInfo).to.have.been.called
assert(spyInfo.called)
})

it('should report browser id', function () {
Expand All @@ -129,13 +130,13 @@ describe('Karma', function () {
k = new Karma(socket, {}, windowStub, windowNavigator, windowLocation)

var spyInfo = sinon.spy(function (info) {
expect(info.id).to.be.eql('567')
assert(info.id === '567')
})

socket.on('register', spyInfo)
socket.emit('connect')

expect(spyInfo).to.have.been.called
assert(spyInfo.called)
})

describe('result', function () {
Expand All @@ -150,11 +151,11 @@ describe('Karma', function () {
k.result({id: i})
}

expect(spyResult).to.not.have.been.called
assert(!spyResult.called)

k.result('result', {id: 50})
expect(spyResult).to.have.been.called
expect(spyResult.args[0][0].length).to.be.eql(50)
assert(spyResult.called)
assert(spyResult.args[0][0].length === 50)
})

it('should buffer results when polling', function () {
Expand All @@ -169,8 +170,8 @@ describe('Karma', function () {
}

k.complete()
expect(spyResult).to.have.been.called
expect(spyResult.args[0][0].length).to.be.eql(40)
assert(spyResult.called)
assert(spyResult.args[0][0].length === 40)
})

it('should emit "start" with total specs count first', function () {
Expand All @@ -188,7 +189,7 @@ describe('Karma', function () {

// adapter didn't call info({total: x})
k.result()
expect(log).to.be.eql(['start', 'result'])
assert.deepEqual(log, ['start', 'result'])
})

it('should not emit "start" if already done by the adapter', function () {
Expand All @@ -209,8 +210,8 @@ describe('Karma', function () {

k.info({total: 321})
k.result()
expect(log).to.be.eql(['start', 'result'])
expect(spyStart).to.have.been.calledWith({total: 321})
assert.deepEqual(log, ['start', 'result'])
assert(spyStart.calledWith({total: 321}))
})
})

Expand All @@ -226,7 +227,7 @@ describe('Karma', function () {

k.setupContext(mockWindow)
mockWindow.alert('What?')
expect(k.log).to.have.been.calledWith('alert', ['What?'])
assert(k.log.calledWith('alert', ['What?']))
})
})

Expand All @@ -235,16 +236,16 @@ describe('Karma', function () {
k.store('a', 10)
k.store('b', [1, 2, 3])

expect(k.store('a')).to.be.eql(10)
expect(k.store('b')).to.be.eql([1, 2, 3])
assert.equal(k.store('a'), 10)
assert.deepEqual(k.store('b'), [1, 2, 3])
})

it('should clone arrays to avoid memory leaks', function () {
var array = [1, 2, 3, 4, 5]

k.store('one.array', array)
expect(k.store('one.array')).to.be.eql(array)
expect(k.store('one.array')).to.be.eql(array)
assert.deepEqual(k.store('one.array'), array)
assert.deepEqual(k.store('one.array'), array)
})
})

Expand All @@ -270,10 +271,10 @@ describe('Karma', function () {
k.result({id: i})
}

expect(spyResult).to.not.have.been.called
assert(!spyResult.called)

k.complete()
expect(spyResult).to.have.been.called
assert(spyResult.called)
})

it('should navigate the client to return_url if specified', function (done) {
Expand All @@ -291,7 +292,7 @@ describe('Karma', function () {

clock.tick(500)
setTimeout(function () {
expect(windowLocation.href).to.be.eql('http://return.com')
assert(windowLocation.href === 'http://return.com')
done()
}, 5)
clock.tick(10)
Expand All @@ -309,8 +310,8 @@ describe('Karma', function () {

k.setupContext(mockWindow)
mockWindow.console.log('What?')
expect(k.log).to.have.been.calledWith('log')
expect(k.log.args[0][1][0]).to.be.eql('What?')
assert(k.log.calledWith('log'))
assert(k.log.args[0][1][0] === 'What?')
})

it('should not patch the console if captureConsole is false', function () {
Expand All @@ -325,7 +326,7 @@ describe('Karma', function () {

k.setupContext(mockWindow)
mockWindow.console.log('hello')
expect(k.log).to.not.have.been.called
assert(!k.log.called)
})

it('should clear context window upon complete when clearContext config is true', function () {
Expand All @@ -338,9 +339,9 @@ describe('Karma', function () {

k.complete()

clock.tick(1)
clock.tick(20)

expect(iframe.src).to.not.be.eql(CURRENT_URL)
assert.notEqual(iframe.src, CURRENT_URL)
})

it('should not clear context window upon complete when clearContext config is false', function () {
Expand All @@ -355,7 +356,7 @@ describe('Karma', function () {

clock.tick(1)

expect(iframe.src).to.be.eql(CURRENT_URL)
assert.equal(iframe.src, CURRENT_URL)
})
})
})
37 changes: 18 additions & 19 deletions test/client/stringify.spec.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
/* global __karma__ */
var chai = require('chai')
var expect = chai.expect
var assert = require('assert')

var stringify = require('../../client/stringify')

describe('stringify', function () {
it('should serialize string', function () {
expect(stringify('aaa')).to.be.eql("'aaa'")
assert.deepEqual(stringify('aaa'), "'aaa'")
})

it('should serialize booleans', function () {
expect(stringify(true)).to.be.eql('true')
expect(stringify(false)).to.be.eql('false')
assert.deepEqual(stringify(true), 'true')
assert.deepEqual(stringify(false), 'false')
})

it('should serialize null and undefined', function () {
expect(stringify(null)).to.be.eql('null')
expect(stringify()).to.be.eql('undefined')
assert.deepEqual(stringify(null), 'null')
assert.deepEqual(stringify(), 'undefined')
})

it('should serialize functions', function () {
Expand All @@ -28,59 +27,59 @@ describe('stringify', function () {
var partsDef = ['function', '(d, e, f)', '{ ... }']

partsAbc.forEach(function (part) {
expect(abcString).to.contain(part)
assert(abcString.indexOf(part) > -1)
})

var defString = stringify(def)
partsDef.forEach(function (part) {
expect(defString).to.contain(part)
assert(defString.indexOf(part) > -1)
})
})

it('should serialize arrays', function () {
expect(stringify(['a', 'b', null, true, false])).to.be.eql("['a', 'b', null, true, false]")
assert.deepEqual(stringify(['a', 'b', null, true, false]), "['a', 'b', null, true, false]")
})

it('should serialize objects', function () {
var obj

obj = {a: 'a', b: 'b', c: null, d: true, e: false}
expect(stringify(obj)).to.contain("{a: 'a', b: 'b', c: null, d: true, e: false}")
assert(stringify(obj).indexOf("{a: 'a', b: 'b', c: null, d: true, e: false}") > -1)

function MyObj () {
this.a = 'a'
}

obj = new MyObj()
expect(stringify(obj)).to.contain("{a: 'a'}")
assert(stringify(obj).indexOf("{a: 'a'}") > -1)

obj = {constructor: null}
expect(stringify(obj)).to.contain('{constructor: null}')
assert(stringify(obj).indexOf('{constructor: null}') > -1)

obj = Object.create(null)
obj.a = 'a'
expect(stringify(obj)).to.contain("{a: 'a'}")
assert(stringify(obj).indexOf("{a: 'a'}") > -1)
})

it('should serialize html', function () {
var div = document.createElement('div')

expect(stringify(div)).to.be.eql('<div></div>')
assert.deepEqual(stringify(div), '<div></div>')

div.innerHTML = 'some <span>text</span>'
expect(stringify(div)).to.be.eql('<div>some <span>text</span></div>')
assert.deepEqual(stringify(div), '<div>some <span>text</span></div>')
})

it('should serialize DOMParser objects', function () {
var parser = new DOMParser()
var doc = parser.parseFromString('<test></test>', 'application/xml')
expect(stringify(doc)).to.be.eql('<test></test>')
assert.deepEqual(stringify(doc), '<test></test>')
})

it('should serialize across iframes', function () {
var div = document.createElement('div')
expect(__karma__.stringify(div)).to.be.eql('<div></div>')
assert.deepEqual(__karma__.stringify(div), '<div></div>')

expect(__karma__.stringify([1, 2])).to.be.eql('[1, 2]')
assert.deepEqual(__karma__.stringify([1, 2]), '[1, 2]')
})
})
5 changes: 2 additions & 3 deletions test/client/util.spec.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
var chai = require('chai')
var expect = chai.expect
var assert = require('assert')

var util = require('../../client/util')

describe('util', function () {
it('parseQueryParams', function () {
var params = util.parseQueryParams('?id=123&return_url=http://whatever.com')

expect(params).to.be.eql({id: '123', return_url: 'http://whatever.com'})
assert.deepEqual(params, {id: '123', return_url: 'http://whatever.com'})
})
})

0 comments on commit d4c2286

Please sign in to comment.