Browse Source

Added test for ChangeLogModalController, AppFooterController and PeerSelectController (#1355)

* Added test for ChangeLogModalController
This commit changes the test scores as follows

|     | % Statements | % Branch | % Functions | % Lines |
|:---:|:------------:|:--------:|-------------|---------|
| Old |      3.5     |     0    | 0.36        | 3.52    |
| New |     4.22     |   0.36   | 1.08        | 4.24    |

* Added tests for AppFooterController
This commit changes the test scores as follows

|     | % Statements | % Branch | % Functions | % Lines |
|:---:|:------------:|:--------:|-------------|---------|
| Old |     4.22     |   0.36   |    1.08     |   4.24  |
| New |     4.29     |   0.36   |    1.44     |   4.31  |

* Added tests for PeerSelectController
This commit changes the test scores as follows

|     | % Statements | % Branch | % Functions | % Lines |
|:---:|:------------:|:--------:|-------------|---------|
| Old |     4.29     |   0.36   |    1.44     |   4.31  |
| New |     5.61     |   1.16   |    2.88     |   5.64  |

* Change Time-out time to variable
Instead of doing time-outs based on a constant, the time-out is based on a variable. This will make changing the time of time-out easier.

* Rename AppFooterController test, removed unnecessary test, fixed code style

Renamed test/unit/AppFooterController.js to test/unit/AppFooterControllerSpec.js to conform with other test files.
Removed a test in the renamed file. It only tested if the controller would compile given the test-environment, something that was implicitly tested in the other (now only) test.
The code style over all test files was not consistent (spacing), this is fixed.

* Changed style to StandardJS

When running "standard test/unit/..", there were some errors on the code-style of the tests
These are fixed
master
Bart 8 years ago committed by Igor Zhukov
parent
commit
5099d14df9
  1. 34
      test/unit/AppFooterControllerSpec.js
  2. 69
      test/unit/ChangelogModalControlelerSpec.js
  3. 256
      test/unit/PeerSelectControllerSpec.js

34
test/unit/AppFooterControllerSpec.js

@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
/* global describe, it, inject, expect, beforeEach */
describe('AppFooterController', function () {
var $controller, $scope, service, serviceFlag
beforeEach(module('myApp.controllers'))
beforeEach(function () {
serviceFlag = false
service = {
switchLayout: function (parameter) {
serviceFlag = true
}
}
inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_
$scope = _$rootScope_.$new()
$controller('AppFooterController', {
$scope: $scope,
LayoutSwitchService: service
})
})
})
// define tests
it('calls the right function', function (done) {
expect(serviceFlag).toBe(false)
$scope.switchLayout(null)
expect(serviceFlag).toBe(true)
done()
})
})

69
test/unit/ChangelogModalControlelerSpec.js

@ -0,0 +1,69 @@ @@ -0,0 +1,69 @@
/* global describe, it, inject, expect, beforeEach, Config */
describe('ChangeLogModalController', function () {
var $controller, $scope, modal, modalFlag
beforeEach(module('myApp.controllers'))
beforeEach(function () {
modalFlag = false
modal = {
open: function (data) {
modalFlag = true
}
}
inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_
$scope = _$rootScope_.$new()
$controller('ChangelogModalController', {
$scope: $scope,
$modal: modal
})
})
})
// define tests
it('will have standard data when no function is called', function (done) {
expect($scope.changelogHidden).toBe(false)
expect($scope.changelogShown).toBe(false)
expect($scope.currentVersion).toBe(Config.App.version)
done()
})
it('will show the changelog', function (done) {
$scope.showAllVersions()
expect($scope.changelogHidden).toBe(false)
expect($scope.changelogShown).toBe(true)
done()
})
it('will allow to show any version when "changelogShown" is true', function (done) {
$scope.changelogShown = true
expect($scope.canShowVersion(null)).toBe(true)
expect($scope.canShowVersion('0.0.1')).toBe(true)
expect($scope.canShowVersion('0.1.0')).toBe(true)
expect($scope.canShowVersion('1.0.0')).toBe(true)
done()
})
it('will allow the version to be shown when the current verion is bigger than the last function', function (done) {
expect($scope.canShowVersion('100.100.100')).toBe(true)
done()
})
it('won\'t allow the version to be shown when it is smaller than the current version', function (done) {
expect($scope.changelogHidden).toBe(false)
expect($scope.canShowVersion('0.0.0')).toBe(false)
expect($scope.changelogHidden).toBe(true)
done()
})
it('will call modal when the changeUsername function is called', function (done) {
expect(modalFlag).toBe(false)
$scope.changeUsername()
expect(modalFlag).toBe(true)
done()
})
})

256
test/unit/PeerSelectControllerSpec.js

@ -0,0 +1,256 @@ @@ -0,0 +1,256 @@
/* global describe, it, inject, expect, beforeEach */
describe('PeerSelectController', function () {
var $controller, $scope, $q, $mod, $APManager, $EService, createController, timeoutTime, $promiseData, $promise, $promiseFlag
beforeEach(module('myApp.controllers'))
beforeEach(function () {
// The modalInstance will propably usually give a boolean as return.
// However, for testing purposes it is important to gain knowledge about the input of the function
$mod = {
close: function (arr) {
return arr
}
}
timeoutTime = 1000
$APManager = {
getPeerString: function (str) {
return 'P'.concat(str)
},
getPeerID: function (str) {
return str.slice(-1)
},
getPeer: function (id) {
return id.concat('peer')
}
}
// The controller is created in the test in order to test different initial content of scope variables.
createController = function () {
$controller('PeerSelectController', {
$scope: $scope,
$modalInstance: $mod,
$q: $q,
AppPeersManager: $APManager,
ErrorService: $EService
})
}
$promiseFlag = false
$promise = {
then: function (f) {
$promiseFlag = true
f()
}
}
$EService = {
confirm: function (data) {
$promiseData = data
return $promise
}
}
$q = {
when: function () {
return $promise
}
}
inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_
$scope = _$rootScope_.$new()
})
})
it('initialises properties', function (done) {
createController()
// Set timer to give the controller time to resolve.
setTimeout(function () {
expect($scope.selectedPeers).toBeDefined()
expect($scope.selectedPeersIDs).toBeDefined()
expect($scope.selectedCount).toBeDefined()
}, timeoutTime)
done()
})
it('compiles with a shareLinkPromise that resolves', function (done) {
var expected = 'testURL'
$scope.shareLinkPromise = {
then: function (resolve, reject) {
setTimeout(resolve(expected), timeoutTime)
}
}
createController()
setTimeout(function () {
expect($scope.shareLink.loading).toBe(true)
expect($scope.shareLink.url).not.toBeDefined()
setTimeout(function () {
expect($scope.shareLink.url).toBe(expected)
}, timeoutTime)
}, timeoutTime)
done()
})
it('compiles with a shareLinkPromise that doesn\'t resolve', function (done) {
$scope.shareLinkPromise = {
then: function (resolve, reject) {
setTimeout(reject(), timeoutTime)
}
}
createController()
setTimeout(function () {
expect($scope.shareLink.loading).toBe(true)
setTimeout(function () {
expect($scope.shareLink).not.toBeDefined()
}, timeoutTime)
}, timeoutTime)
done()
})
it('can select and submit a single dialog without confirmed type', function (done) {
createController()
$scope.dialogSelect('dialogX')
expect($promiseData).not.toBeDefined()
expect($promiseFlag).toBe(true)
done()
})
it('can select and submit a single dialog with confirmed type', function (done) {
createController()
$scope.confirm_type = 'INVITE_TO_GROUP'
$scope.dialogSelect('dialogX')
var expected = {
type: 'INVITE_TO_GROUP',
peer_id: 'X',
peer_data: 'Xpeer'
}
expect($promiseData).toEqual(expected)
expect($promiseFlag).toBe(true)
done()
})
it('can select a dialog', function (done) {
createController()
$scope.multiSelect = true
$scope.dialogSelect('dialogX')
var expected = ['X']
expect($scope.selectedPeers['X']).toBe('Xpeer')
expect($scope.selectedCount).toBe(1)
expect($scope.selectedPeerIDs).toEqual(expected)
done()
})
it('can select multiple dialogs', function (done) {
createController()
$scope.multiSelect = true
$scope.dialogSelect('dialogX')
$scope.dialogSelect('dialogZ')
$scope.dialogSelect('dialogY')
var expected = ['Y', 'Z', 'X']
expect($scope.selectedCount).toBe(3)
expect($scope.selectedPeerIDs).toEqual(expected)
done()
})
it('can unselect a dialog', function (done) {
createController()
$scope.multiSelect = true
$scope.selectedCount = 1
$scope.selectedPeers['Y'] = 'aYPeer'
$scope.selectedPeerIDs.unshift('Y')
$scope.dialogSelect('dialogY')
var expected = []
expect($scope.selectedPeers['Y']).not.toBeDefined()
expect($scope.selectedCount).toBe(0)
expect($scope.selectedPeerIDs).toEqual(expected)
done()
})
it('can select multiple dialogs', function (done) {
createController()
$scope.multiSelect = true
$scope.dialogSelect('dialogX')
$scope.dialogSelect('dialogZ')
$scope.dialogSelect('dialogY')
$scope.dialogSelect('dialogZ')
var expected = ['Y', 'X']
expect($scope.selectedCount).toBe(2)
expect($scope.selectedPeerIDs).toEqual(expected)
done()
})
it('can\'t submit a empty set of dialogs', function (done) {
createController()
expect($scope.submitSelected()).not.toBeDefined()
done()
})
it('can submit one dialog', function (done) {
createController()
$scope.selectedCount = 1
$scope.selectedPeers['test'] = 'peer'
var expected = ['Ptest']
expect($scope.submitSelected()).toEqual(expected)
done()
})
it('can submit multiple dialogs', function (done) {
createController()
$scope.selectedCount = 3
$scope.selectedPeers['test1'] = $scope.selectedPeers['test2'] = $scope.selectedPeers['test4'] = 'peer'
var expected = ['Ptest4', 'Ptest2', 'Ptest1']
expect($scope.submitSelected()).toEqual(expected)
done()
})
it('can toggle', function (done) {
createController()
var broadcastFlag = ''
$scope.$broadcast = function (input) { broadcastFlag = input }
$scope.toggleSearch()
expect(broadcastFlag).toBe('dialogs_search_toggle')
done()
})
})
Loading…
Cancel
Save