Browse Source

Cleanup of tests, improved readability of the test code (#1434)

* Cleanup of tests, improved readability

* Improved readability concerning timeout/loading tests, removed unnecessary 'this.'
master
Bart 7 years ago committed by Igor Zhukov
parent
commit
cd3979aeab
  1. 4
      app/js/controllers.js
  2. 24
      test/unit/controllers/AppFooterControllerSpec.js
  3. 10
      test/unit/controllers/AppImPanelControllerSpec.js
  4. 23
      test/unit/controllers/AppLangSelectControllerSpec.js
  5. 37
      test/unit/controllers/AppWelcomeControllerSpec.js
  6. 55
      test/unit/controllers/ChangelogModalControlelerSpec.js
  7. 145
      test/unit/controllers/DocumentModalControllerSpec.js
  8. 74
      test/unit/controllers/EmbedModalController.js
  9. 305
      test/unit/controllers/PeerSelectControllerSpec.js
  10. 152
      test/unit/controllers/VideoModalControllerSpec.js
  11. 14
      test/unit/directives/myHeadDirective.js
  12. 14
      test/unit/directives/myLangFooterDirective.js
  13. 20
      test/unit/services/PhonebookContactsServiceSpec.js

4
app/js/controllers.js

@ -3537,7 +3537,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) @@ -3537,7 +3537,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
}
$scope.$on('history_delete', function (e, historyUpdate) {
if (historyUpdate.msgs[$scope.messageID]) {
if (historyUpdate && historyUpdate.msgs && historyUpdate.msgs[$scope.messageID]) {
$modalInstance.dismiss()
}
})
@ -3571,7 +3571,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) @@ -3571,7 +3571,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
}
$scope.$on('history_delete', function (e, historyUpdate) {
if (historyUpdate.msgs[$scope.messageID]) {
if (historyUpdate && historyUpdate.msgs && historyUpdate.msgs[$scope.messageID]) {
$modalInstance.dismiss()
}
})

24
test/unit/controllers/AppFooterControllerSpec.js

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

10
test/unit/controllers/AppImPanelControllerSpec.js

@ -2,21 +2,19 @@ @@ -2,21 +2,19 @@
/* global describe, it, inject, expect, beforeEach, jasmine */
describe('AppImPanelController', function () {
var $scope
beforeEach(module('myApp.controllers'))
beforeEach(function () {
inject(function (_$controller_, _$rootScope_) {
$scope = _$rootScope_.$new()
$scope.$on = jasmine.createSpy('$on')
_$controller_('AppImPanelController', { $scope: $scope })
this.$scope = _$rootScope_.$new()
this.$scope.$on = jasmine.createSpy('$on')
_$controller_('AppImPanelController', { $scope: this.$scope })
})
})
// define tests
it('sets $on(user_update) to no-operation function', function (done) {
expect($scope.$on).toHaveBeenCalledWith('user_update', angular.noop)
expect(this.$scope.$on).toHaveBeenCalledWith('user_update', angular.noop)
done()
})
})

23
test/unit/controllers/AppLangSelectControllerSpec.js

@ -2,18 +2,17 @@ @@ -2,18 +2,17 @@
/* global describe, it, inject, expect, beforeEach, xit */
describe('AppLangSelectController', function () {
var $controller, $scope
beforeEach(module('ui.bootstrap'))
beforeEach(module('myApp.services'))
beforeEach(module('myApp.controllers'))
beforeEach(function () {
inject(function (_$controller_, _$rootScope_, _, Storage, ErrorService, AppRuntimeManager) {
$controller = _$controller_
$scope = _$rootScope_.$new()
$controller('AppLangSelectController', {
$scope: $scope,
this.$controller = _$controller_
this.$scope = _$rootScope_.$new()
this.$controller('AppLangSelectController', {
$scope: this.$scope,
_: _,
Storage: Storage,
ErrorService: ErrorService,
@ -23,24 +22,24 @@ describe('AppLangSelectController', function () { @@ -23,24 +22,24 @@ describe('AppLangSelectController', function () {
})
it('holds the supportedLocales', function () {
expect($scope.supportedLocales).toBeDefined()
expect(this.$scope.supportedLocales).toBeDefined()
})
it('holds langNames', function () {
expect($scope.langNames).toBeDefined()
expect(this.$scope.langNames).toBeDefined()
})
it('holds the current locale', function () {
expect($scope.curLocale).toBeDefined()
expect(this.$scope.curLocale).toBeDefined()
})
it('has a locale form', function () {
expect($scope.form).toBeDefined()
expect($scope.form.locale).toBeDefined()
expect(this.$scope.form).toBeDefined()
expect(this.$scope.form.locale).toBeDefined()
})
it('allows to select a locale', function () {
expect($scope.localeSelect).toBeDefined()
expect(this.$scope.localeSelect).toBeDefined()
})
describe('when the user switches the locale', function () {

37
test/unit/controllers/AppWelcomeControllerSpec.js

@ -2,21 +2,18 @@ @@ -2,21 +2,18 @@
/* global describe, it, inject, expect, beforeEach */
describe('AppWelcomeController', function () {
var $controller, $rootScope, $scope, $location, MtpApiManager, ErrorService,
ChangelogNotifyService, LayoutSwitchService
beforeEach(module('myApp.controllers'))
beforeEach(function () {
ChangelogNotifyService = {
this.ChangelogNotifyService = {
checkUpdate: function () {}
}
LayoutSwitchService = {
this.LayoutSwitchService = {
start: function () {}
}
MtpApiManager = {
this.MtpApiManager = {
getUserID: function () {
return {
then: function () {}
@ -24,23 +21,19 @@ describe('AppWelcomeController', function () { @@ -24,23 +21,19 @@ describe('AppWelcomeController', function () {
}
}
module(function ($provide) {
$provide.value('MtpApiManager', MtpApiManager)
})
inject(function (_$controller_, _$rootScope_, _$location_) {
$controller = _$controller_
$rootScope = _$rootScope_
$location = _$location_
$scope = $rootScope.$new()
$controller('AppWelcomeController', {
$scope: $scope,
$location: $location,
MtpApiManager: MtpApiManager,
ErrorService: ErrorService,
ChangelogNotifyService: ChangelogNotifyService,
LayoutSwitchService: LayoutSwitchService
this.$controller = _$controller_
this.$rootScope = _$rootScope_
this.$location = _$location_
this.$scope = _$rootScope_.$new()
this.$controller('AppWelcomeController', {
$scope: this.$scope,
$location: this.$location,
MtpApiManager: this.MtpApiManager,
ErrorService: this.ErrorService,
ChangelogNotifyService: this.ChangelogNotifyService,
LayoutSwitchService: this.LayoutSwitchService
})
})
})

55
test/unit/controllers/ChangelogModalControlelerSpec.js

@ -2,69 +2,68 @@ @@ -2,69 +2,68 @@
/* global describe, it, inject, expect, beforeEach, Config */
describe('ChangeLogModalController', function () {
var $controller, $scope, modal, modalFlag
beforeEach(module('myApp.controllers'))
beforeEach(function () {
modalFlag = false
modal = {
this.modal = {
modalFlag: false,
open: function (data) {
modalFlag = true
this.modalFlag = true
}
}
inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_
this.$controller = _$controller_
this.$scope = _$rootScope_.$new()
$scope = _$rootScope_.$new()
$controller('ChangelogModalController', {
$scope: $scope,
$modal: modal
this.$controller('ChangelogModalController', {
$scope: this.$scope,
$modal: this.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)
expect(this.$scope.changelogHidden).toBe(false)
expect(this.$scope.changelogShown).toBe(false)
expect(this.$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)
this.$scope.showAllVersions()
expect(this.$scope.changelogHidden).toBe(false)
expect(this.$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)
this.$scope.changelogShown = true
expect(this.$scope.canShowVersion(null)).toBe(true)
expect(this.$scope.canShowVersion('0.0.1')).toBe(true)
expect(this.$scope.canShowVersion('0.1.0')).toBe(true)
expect(this.$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)
expect(this.$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)
expect(this.$scope.changelogHidden).toBe(false)
expect(this.$scope.canShowVersion('0.0.0')).toBe(false)
expect(this.$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)
expect(this.modal.modalFlag).toBe(false)
this.$scope.changeUsername()
expect(this.modal.modalFlag).toBe(true)
done()
})
})

145
test/unit/controllers/DocumentModalControllerSpec.js

@ -1,20 +1,19 @@ @@ -1,20 +1,19 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */
/* global describe, it, inject, expect, beforeEach, jasmine, spyOn */
describe('DocumentModalController', function () {
var $controller, $scope, $rootScope, $docManager, $errService, $input, $messManager, $pSelectService, $modalI, createController
beforeEach(module('myApp.controllers'))
beforeEach(function () {
$docManager = {}
$docManager.wrapForHistory = jasmine.createSpy('wrapForHistory')
$docManager.saveDocFile = jasmine.createSpy('saveDocFile')
this.AppDocsManager = {
wrapForHistory: jasmine.createSpy('wrapForHistory'),
saveDocFile: jasmine.createSpy('saveDocFile')
}
$input = {}
$errService = {
this.ErrorService = {
$input: {},
confirm: function (message) {
$input = message
this.$input = message
return {
then: function (f) {
f()
@ -23,9 +22,10 @@ describe('DocumentModalController', function () { @@ -23,9 +22,10 @@ describe('DocumentModalController', function () {
}
}
$pSelectService = {
this.PeersSelectService = {
$input: {},
selectPeer: function (options) {
$input = options
this.$input = options
return {
then: function (f) {
f('Peerselected')
@ -34,100 +34,103 @@ describe('DocumentModalController', function () { @@ -34,100 +34,103 @@ describe('DocumentModalController', function () {
}
}
createController = function (spyBroadcast, spyOn) {
if (spyBroadcast) {
$rootScope.$broadcast = jasmine.createSpy('$broadcast')
}
if (spyOn) {
$scope.$on = jasmine.createSpy('$on')
}
$controller('DocumentModalController', {
$scope: $scope,
$rootScope: $rootScope,
$modalInstance: $modalI,
PeersSelectService: $pSelectService,
AppMessagesManager: $messManager,
AppDocsManager: $docManager,
AppPeersManager: {},
ErrorService: $errService
})
this.AppMessagesManager = {
deleteMessages: jasmine.createSpy('deleteMessages')
}
$messManager = {}
$messManager.deleteMessages = jasmine.createSpy('deleteMessages')
$modalI = {}
$modalI.dismiss = jasmine.createSpy('dismissModal')
this.$modalInstance = {
dismiss: jasmine.createSpy('dismissModal')
}
inject(function (_$controller_, _$rootScope_) {
$rootScope = _$rootScope_
$scope = $rootScope.$new()
$scope.docID = 'randomdoc'
$controller = _$controller_
this.$rootScope = _$rootScope_
this.$scope = this.$rootScope.$new()
this.$scope.docID = 'randomdoc'
this.$controller = _$controller_
spyOn(this.$rootScope, '$broadcast').and.callThrough()
spyOn(this.$scope, '$on').and.callThrough()
this.$controller('DocumentModalController', {
$scope: this.$scope,
$rootScope: this.$rootScope,
$modalInstance: this.$modalInstance,
PeersSelectService: this.PeersSelectService,
AppMessagesManager: this.AppMessagesManager,
AppDocsManager: this.AppDocsManager,
AppPeersManager: {},
ErrorService: this.ErrorService
})
})
})
// define tests
it('sets the document in the scope', function (done) {
createController(false, false)
expect($docManager.wrapForHistory).toHaveBeenCalledWith($scope.docID)
expect(this.AppDocsManager.wrapForHistory).toHaveBeenCalledWith(this.$scope.docID)
done()
})
it('forwards a message with a document', function (done) {
createController(true, false)
$scope.messageID = 'id039'
this.$scope.messageID = 'id039'
var messageID = this.$scope.messageID
$scope.forward()
expect($input).toEqual({canSend: true})
expect($scope.$broadcast).toHaveBeenCalledWith('history_focus', {
this.$scope.forward()
expect(this.PeersSelectService.$input).toEqual({canSend: true})
expect(this.$scope.$broadcast).toHaveBeenCalledWith('history_focus', {
peerString: 'Peerselected',
attachment: {
_: 'fwd_messages',
id: [$scope.messageID]
id: [messageID]
}
})
done()
})
it('deletes a message with a document', function (done) {
createController(false, false)
$scope.messageID = 'id123'
this.$scope.messageID = 'id123'
$scope.delete()
expect($input).toEqual({type: 'MESSAGE_DELETE'})
expect($messManager.deleteMessages).toHaveBeenCalledWith([$scope.messageID])
this.$scope.delete()
expect(this.ErrorService.$input).toEqual({type: 'MESSAGE_DELETE'})
expect(this.AppMessagesManager.deleteMessages).toHaveBeenCalledWith([this.$scope.messageID])
done()
})
it('downloads the document', function (done) {
createController(false, false)
$scope.download()
expect($docManager.saveDocFile).toHaveBeenCalledWith($scope.docID)
this.$scope.download()
expect(this.AppDocsManager.saveDocFile).toHaveBeenCalledWith(this.$scope.docID)
done()
})
it('delete a document linked to a message', function (done) {
createController(false, true)
$scope.messageID = 'id33'
it('can not delete a document not linked to a message', function (done) {
this.$scope.messageID = 'id42'
$rootScope.$broadcast('history_delete')
expect($scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect($modalI.dismiss).not.toHaveBeenCalled()
var historyUpdate = {}
this.$rootScope.$broadcast('history_delete', historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
historyUpdate.msgs = {}
this.$rootScope.$broadcast('history_delete', historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
done()
})
it('delete a document linked to a modal instance', function (done) {
createController(false, false)
$scope.messageID = 'id876'
var $msgs = {}
$msgs[$scope.messageID] = {message: 'some non-empty message'}
$rootScope.$broadcast('history_delete', {msgs: $msgs})
expect($modalI.dismiss).toHaveBeenCalled()
done()
describe('when the document is related to the message', function () {
beforeEach(function () {
this.historyUpdate = {
msgs: {}
}
})
it('delete that document', function (done) {
this.$scope.messageID = 'id33'
this.historyUpdate.msgs[this.$scope.messageID] = 'an update for id33'
this.$rootScope.$broadcast('history_delete', this.historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).toHaveBeenCalled()
done()
})
})
})

74
test/unit/controllers/EmbedModalController.js

@ -2,18 +2,17 @@ @@ -2,18 +2,17 @@
/* global describe, it, inject, expect, beforeEach, jasmine */
describe('EmbedModalController', function () {
var $scope, $rootScope, $webpageManager, $errService, $input, $messManager, $pSelectService, $modalI
beforeEach(module('myApp.controllers'))
beforeEach(function () {
$webpageManager = {}
$webpageManager.wrapForFull = jasmine.createSpy('wrapForFull')
this.AppWebPagesManager = {
wrapForFull: jasmine.createSpy('wrapForFull')
}
$input = {}
$errService = {
this.ErrorService = {
input: {},
confirm: function (message) {
$input = message
this.input = message
return {
then: function (f) {
f()
@ -22,9 +21,10 @@ describe('EmbedModalController', function () { @@ -22,9 +21,10 @@ describe('EmbedModalController', function () {
}
}
$pSelectService = {
this.PeersSelectService = {
input: {},
selectPeer: function (options) {
$input = options
this.input = options
return {
then: function (f) {
f('Peerselected')
@ -33,60 +33,64 @@ describe('EmbedModalController', function () { @@ -33,60 +33,64 @@ describe('EmbedModalController', function () {
}
}
$messManager = {}
$messManager.deleteMessages = jasmine.createSpy('deleteMessages')
this.AppMessagesManager = {
deleteMessages: jasmine.createSpy('deleteMessages')
}
$modalI = {}
$modalI.dismiss = jasmine.createSpy('dismissModal')
this.$modalInstance = {
dismiss: jasmine.createSpy('dismissModal')
}
inject(function (_$controller_, _$rootScope_) {
$rootScope = _$rootScope_
$rootScope.$broadcast = jasmine.createSpy('$broadcast')
$scope = $rootScope.$new()
$scope.webpageID = 'www.notRelevant.com'
this.$rootScope = _$rootScope_
this.$rootScope.$broadcast = jasmine.createSpy('$broadcast')
this.$scope = this.$rootScope.$new()
this.$scope.webpageID = 'www.notRelevant.com'
_$controller_('EmbedModalController', {
$q: {},
$scope: $scope,
$rootScope: $rootScope,
$modalInstance: $modalI,
PeersSelectService: $pSelectService,
AppMessagesManager: $messManager,
$scope: this.$scope,
$rootScope: this.$rootScope,
$modalInstance: this.$modalInstance,
PeersSelectService: this.PeersSelectService,
AppMessagesManager: this.AppMessagesManager,
AppPeersManager: {},
AppPhotosManager: {},
AppWebPagesManager: $webpageManager,
ErrorService: $errService
AppWebPagesManager: this.AppWebPagesManager,
ErrorService: this.ErrorService
})
})
})
// define tests
it('sets the embeded webpage in the scope', function (done) {
expect($scope.nav).toEqual({})
expect($webpageManager.wrapForFull).toHaveBeenCalledWith($scope.webpageID)
expect(this.$scope.nav).toEqual({})
expect(this.AppWebPagesManager.wrapForFull).toHaveBeenCalledWith(this.$scope.webpageID)
done()
})
it('forwards a message with an embeded link', function (done) {
$scope.messageID = 'id1234234'
this.$scope.messageID = 'id1234234'
var messageID = this.$scope.messageID
$scope.forward()
expect($input).toEqual({canSend: true})
expect($scope.$broadcast).toHaveBeenCalledWith('history_focus', {
this.$scope.forward()
expect(this.PeersSelectService.input).toEqual({canSend: true})
expect(this.$scope.$broadcast).toHaveBeenCalledWith('history_focus', {
peerString: 'Peerselected',
attachment: {
_: 'fwd_messages',
id: [$scope.messageID]
id: [messageID]
}
})
done()
})
it('deletes a message with an embeded link', function (done) {
$scope.messageID = 'id979565673'
this.$scope.messageID = 'id979565673'
$scope.delete()
expect($input).toEqual({type: 'MESSAGE_DELETE'})
expect($messManager.deleteMessages).toHaveBeenCalledWith([$scope.messageID])
this.$scope.delete()
expect(this.ErrorService.input).toEqual({type: 'MESSAGE_DELETE'})
expect(this.AppMessagesManager.deleteMessages).toHaveBeenCalledWith([this.$scope.messageID])
done()
})
})

305
test/unit/controllers/PeerSelectControllerSpec.js

@ -2,22 +2,20 @@ @@ -2,22 +2,20 @@
/* 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 = {
this.$modalInstance = {
close: function (arr) {
return arr
}
}
timeoutTime = 1000
this.oneSecond = 1000
$APManager = {
this.AppPeersManager = {
getPeerString: function (str) {
return 'P'.concat(str)
},
@ -29,229 +27,232 @@ describe('PeerSelectController', function () { @@ -29,229 +27,232 @@ describe('PeerSelectController', function () {
}
}
// 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 = {
this.promise = {
promiseFlag: false,
then: function (f) {
$promiseFlag = true
this.$promiseFlag = true
f()
}
}
$EService = {
confirm: function (data) {
$promiseData = data
return $promise
var promise = this.promise
this.$q = {
when: function () {
return promise
}
}
$q = {
when: function () {
return $promise
this.ErrorService = {
$promiseData: {},
confirm: function (data) {
this.$promiseData = data
return promise
}
}
inject(function (_$controller_, _$rootScope_) {
$controller = _$controller_
$scope = _$rootScope_.$new()
this.$controller = _$controller_
this.$scope = _$rootScope_.$new()
// The controller is created in the test in order to test different initial content of scope variables.
this.createController = function () {
this.$controller('PeerSelectController', {
$scope: this.$scope,
$modalInstance: this.$modalInstance,
$q: this.$q,
AppPeersManager: this.AppPeersManager,
ErrorService: this.ErrorService
})
}
})
})
it('initialises properties', function (done) {
createController()
this.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)
expect(this.$scope.selectedPeers).toBeDefined()
expect(this.$scope.selectedPeersIDs).toBeDefined()
expect(this.$scope.selectedCount).toBeDefined()
}, this.oneSecond)
done()
})
it('compiles with a shareLinkPromise that resolves', function (done) {
var expected = 'testURL'
$scope.shareLinkPromise = {
var oneSecond = this.oneSecond
this.$scope.shareLinkPromise = {
then: function (resolve, reject) {
setTimeout(resolve(expected), timeoutTime)
setTimeout(resolve(expected), oneSecond)
}
}
createController()
this.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)
function afterLoad () {
expect(this.$scope.shareLink.url).toBe(expected)
}
function duringLoad () {
expect(this.$scope.shareLink.loading).toBe(true)
expect(this.$scope.shareLink.url).not.toBeDefined()
setTimeout(afterLoad, oneSecond)
}
setTimeout(duringLoad, oneSecond)
done()
})
it('compiles with a shareLinkPromise that doesn\'t resolve', function (done) {
$scope.shareLinkPromise = {
var oneSecond = this.oneSecond
this.$scope.shareLinkPromise = {
then: function (resolve, reject) {
setTimeout(reject(), timeoutTime)
setTimeout(reject(), oneSecond)
}
}
createController()
this.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'
function afterLoad () {
expect(this.$scope.shareLink).not.toBeDefined()
}
expect($promiseData).toEqual(expected)
expect($promiseFlag).toBe(true)
function duringLoad () {
expect(this.$scope.shareLink.loading).toBe(true)
setTimeout(afterLoad, oneSecond)
}
setTimeout(duringLoad, oneSecond)
done()
})
it('can select a dialog', function (done) {
createController()
describe('after initialisation', function () {
beforeEach(function () {
this.createController()
})
$scope.multiSelect = true
$scope.dialogSelect('dialogX')
it('can select and submit a single dialog without confirmed type', function (done) {
this.$scope.dialogSelect('dialogX')
var expected = ['X']
expect(this.ErrorService.$promiseData).toEqual({})
expect(this.promise.$promiseFlag).toBe(true)
expect($scope.selectedPeers['X']).toBe('Xpeer')
expect($scope.selectedCount).toBe(1)
expect($scope.selectedPeerIDs).toEqual(expected)
done()
})
done()
})
it('can select and submit a single dialog with confirmed type', function (done) {
this.$scope.confirm_type = 'INVITE_TO_GROUP'
this.$scope.dialogSelect('dialogX')
it('can select multiple dialogs', function (done) {
createController()
var peerId = 'X'
var expected = {
type: 'INVITE_TO_GROUP',
peer_id: peerId,
peer_data: this.AppPeersManager.getPeer(peerId)
}
$scope.multiSelect = true
$scope.dialogSelect('dialogX')
$scope.dialogSelect('dialogZ')
$scope.dialogSelect('dialogY')
expect(this.ErrorService.$promiseData).toEqual(expected)
expect(this.promise.$promiseFlag).toBe(true)
var expected = ['Y', 'Z', 'X']
done()
})
expect($scope.selectedCount).toBe(3)
expect($scope.selectedPeerIDs).toEqual(expected)
it('can select a dialog', function (done) {
this.$scope.multiSelect = true
this.$scope.dialogSelect('dialogX')
done()
})
var expected = {
selectedPeers: 'Xpeer',
selectedPeerIDs: ['X']
}
it('can unselect a dialog', function (done) {
createController()
expect(this.$scope.selectedPeers['X']).toBe(expected.selectedPeers)
expect(this.$scope.selectedCount).toBe(1)
expect(this.$scope.selectedPeerIDs).toEqual(expected.selectedPeerIDs)
$scope.multiSelect = true
$scope.selectedCount = 1
$scope.selectedPeers['Y'] = 'aYPeer'
$scope.selectedPeerIDs.unshift('Y')
done()
})
$scope.dialogSelect('dialogY')
it('can select multiple dialogs', function (done) {
this.$scope.multiSelect = true
this.$scope.dialogSelect('dialogX')
this.$scope.dialogSelect('dialogZ')
this.$scope.dialogSelect('dialogY')
var expected = []
var expected = ['Y', 'Z', 'X']
expect($scope.selectedPeers['Y']).not.toBeDefined()
expect($scope.selectedCount).toBe(0)
expect($scope.selectedPeerIDs).toEqual(expected)
expect(this.$scope.selectedCount).toBe(3)
expect(this.$scope.selectedPeerIDs).toEqual(expected)
done()
})
done()
})
it('can select multiple dialogs', function (done) {
createController()
it('can unselect a dialog', function (done) {
this.$scope.multiSelect = true
this.$scope.selectedCount = 1
this.$scope.selectedPeers['Y'] = 'aYPeer'
this.$scope.selectedPeerIDs.unshift('Y')
$scope.multiSelect = true
$scope.dialogSelect('dialogX')
$scope.dialogSelect('dialogZ')
$scope.dialogSelect('dialogY')
$scope.dialogSelect('dialogZ')
this.$scope.dialogSelect('dialogY')
var expected = ['Y', 'X']
var expected = []
expect($scope.selectedCount).toBe(2)
expect($scope.selectedPeerIDs).toEqual(expected)
expect(this.$scope.selectedPeers['Y']).not.toBeDefined()
expect(this.$scope.selectedCount).toBe(0)
expect(this.$scope.selectedPeerIDs).toEqual(expected)
done()
})
done()
})
it('can\'t submit a empty set of dialogs', function (done) {
createController()
it('can select multiple dialogs', function (done) {
this.$scope.multiSelect = true
this.$scope.dialogSelect('dialogX')
this.$scope.dialogSelect('dialogZ')
this.$scope.dialogSelect('dialogY')
this.$scope.dialogSelect('dialogZ')
expect($scope.submitSelected()).not.toBeDefined()
var expected = ['Y', 'X']
done()
})
expect(this.$scope.selectedCount).toBe(2)
expect(this.$scope.selectedPeerIDs).toEqual(expected)
it('can submit one dialog', function (done) {
createController()
done()
})
$scope.selectedCount = 1
$scope.selectedPeers['test'] = 'peer'
var expected = ['Ptest']
expect($scope.submitSelected()).toEqual(expected)
it('can\'t submit a empty set of dialogs', function (done) {
expect(this.$scope.submitSelected()).not.toBeDefined()
done()
})
done()
})
it('can submit multiple dialogs', function (done) {
createController()
it('can submit one dialog', function (done) {
this.$scope.selectedCount = 1
this.$scope.selectedPeers['test'] = 'peer'
var expected = ['Ptest']
expect(this.$scope.submitSelected()).toEqual(expected)
$scope.selectedCount = 3
$scope.selectedPeers['test1'] = $scope.selectedPeers['test2'] = $scope.selectedPeers['test4'] = 'peer'
done()
})
var expected = ['Ptest4', 'Ptest2', 'Ptest1']
expect($scope.submitSelected()).toEqual(expected)
it('can submit multiple dialogs', function (done) {
this.$scope.selectedCount = 3
this.$scope.selectedPeers['test1'] = this.$scope.selectedPeers['test2'] = this.$scope.selectedPeers['test4'] = 'peer'
done()
})
var expected = ['Ptest4', 'Ptest2', 'Ptest1']
expect(this.$scope.submitSelected()).toEqual(expected)
it('can toggle', function (done) {
createController()
done()
})
var broadcastFlag = ''
$scope.$broadcast = function (input) { broadcastFlag = input }
it('can toggle', function (done) {
var broadcastFlag = ''
this.$scope.$broadcast = function (input) { broadcastFlag = input }
$scope.toggleSearch()
expect(broadcastFlag).toBe('dialogs_search_toggle')
this.$scope.toggleSearch()
expect(broadcastFlag).toBe('dialogs_search_toggle')
done()
done()
})
})
})

152
test/unit/controllers/VideoModalControllerSpec.js

@ -1,20 +1,19 @@ @@ -1,20 +1,19 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */
/* global describe, it, inject, expect, beforeEach, jasmine, spyOn */
describe('VideoModalController', function () {
var $controller, $scope, $rootScope, $docManager, $errService, $input, $messManager, $pSelectService, $modalI, createController
beforeEach(module('myApp.controllers'))
beforeEach(function () {
$docManager = {}
$docManager.wrapVideoForFull = jasmine.createSpy('wrapVideoForFull')
$docManager.saveDocFile = jasmine.createSpy('saveDocFile')
this.AppDocsManager = {
wrapVideoForFull: jasmine.createSpy('wrapVideoForFull'),
saveDocFile: jasmine.createSpy('saveDocFile')
}
$input = {}
$errService = {
this.ErrorService = {
input: {},
confirm: function (message) {
$input = message
this.input = message
return {
then: function (f) {
f()
@ -23,9 +22,10 @@ describe('VideoModalController', function () { @@ -23,9 +22,10 @@ describe('VideoModalController', function () {
}
}
$pSelectService = {
this.PeersSelectService = {
input: {},
selectPeer: function (options) {
$input = options
this.input = options
return {
then: function (f) {
f('Peerselected')
@ -34,102 +34,114 @@ describe('VideoModalController', function () { @@ -34,102 +34,114 @@ describe('VideoModalController', function () {
}
}
createController = function (spyBroadcast, spyOn) {
if (spyBroadcast) {
$rootScope.$broadcast = jasmine.createSpy('$broadcast')
}
if (spyOn) {
$scope.$on = jasmine.createSpy('$on')
}
$controller('VideoModalController', {
$scope: $scope,
$rootScope: $rootScope,
$modalInstance: $modalI,
PeersSelectService: $pSelectService,
AppMessagesManager: $messManager,
AppDocsManager: $docManager,
AppPeersManager: {},
ErrorService: $errService
})
this.AppMessagesManager = {
deleteMessages: jasmine.createSpy('deleteMessages')
}
$messManager = {}
$messManager.deleteMessages = jasmine.createSpy('deleteMessages')
$modalI = {}
$modalI.dismiss = jasmine.createSpy('dismissModal')
this.$modalInstance = {
dismiss: jasmine.createSpy('dismissModal')
}
inject(function (_$controller_, _$rootScope_) {
$rootScope = _$rootScope_
$scope = $rootScope.$new()
$scope.docID = 'randomvideo'
$controller = _$controller_
this.$rootScope = _$rootScope_
this.$scope = this.$rootScope.$new()
this.$scope.docID = 'randomvideo'
this.$controller = _$controller_
spyOn(this.$rootScope, '$broadcast').and.callThrough()
spyOn(this.$scope, '$on').and.callThrough()
this.$controller('VideoModalController', {
$scope: this.$scope,
$rootScope: this.$rootScope,
$modalInstance: this.$modalInstance,
PeersSelectService: this.PeersSelectService,
AppMessagesManager: this.AppMessagesManager,
AppDocsManager: this.AppDocsManager,
AppPeersManager: {},
ErrorService: this.ErrorService
})
})
})
// define tests
it('sets the video in the scope', function (done) {
createController(false, false)
expect($scope.progress).toEqual({enabled: false})
expect($scope.player).toEqual({})
expect($docManager.wrapVideoForFull).toHaveBeenCalledWith($scope.docID)
expect(this.$scope.progress).toEqual({enabled: false})
expect(this.$scope.player).toEqual({})
expect(this.AppDocsManager.wrapVideoForFull).toHaveBeenCalledWith(this.$scope.docID)
done()
})
it('forwards a message with a video', function (done) {
createController(true, false)
$scope.messageID = 'id68567'
this.$scope.messageID = 'id68567'
var messageID = this.$scope.messageID
$scope.forward()
expect($input).toEqual({canSend: true})
expect($scope.$broadcast).toHaveBeenCalledWith('history_focus', {
this.$scope.forward()
expect(this.PeersSelectService.input).toEqual({canSend: true})
expect(this.$scope.$broadcast).toHaveBeenCalledWith('history_focus', {
peerString: 'Peerselected',
attachment: {
_: 'fwd_messages',
id: [$scope.messageID]
id: [messageID]
}
})
done()
})
it('deletes a message with a video', function (done) {
createController(false, false)
$scope.messageID = 'id235235'
this.$scope.messageID = 'id235235'
$scope.delete()
expect($input).toEqual({type: 'MESSAGE_DELETE'})
expect($messManager.deleteMessages).toHaveBeenCalledWith([$scope.messageID])
this.$scope.delete()
expect(this.ErrorService.input).toEqual({type: 'MESSAGE_DELETE'})
expect(this.AppMessagesManager.deleteMessages).toHaveBeenCalledWith([this.$scope.messageID])
done()
})
it('downloads the document (video)', function (done) {
createController(false, false)
$scope.download()
expect($docManager.saveDocFile).toHaveBeenCalledWith($scope.docID)
this.$scope.download()
expect(this.AppDocsManager.saveDocFile).toHaveBeenCalledWith(this.$scope.docID)
done()
})
it('delete a video linked to a message', function (done) {
createController(false, true)
$scope.messageID = 'id2352'
this.$scope.messageID = 'id2352'
$rootScope.$broadcast('history_delete')
expect($scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect($modalI.dismiss).not.toHaveBeenCalled()
this.$rootScope.$broadcast('history_delete')
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
done()
})
it('delete a video linked to a modal instance', function (done) {
createController(false, false)
$scope.messageID = 'id6234'
it('can not delete a video not linked to a message', function (done) {
this.$scope.messageID = 'id42'
var $msgs = {}
$msgs[$scope.messageID] = {message: 'some non-empty message'}
$rootScope.$broadcast('history_delete', {msgs: $msgs})
expect($modalI.dismiss).toHaveBeenCalled()
var historyUpdate = {}
this.$rootScope.$broadcast('history_delete', historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
historyUpdate.msgs = {}
this.$rootScope.$broadcast('history_delete', historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
done()
})
describe('when the video is related to the message', function () {
beforeEach(function () {
this.historyUpdate = {
msgs: {}
}
})
it('delete that video', function (done) {
this.$scope.messageID = 'id33'
this.historyUpdate.msgs[this.$scope.messageID] = 'an update for id33'
this.$rootScope.$broadcast('history_delete', this.historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).toHaveBeenCalled()
done()
})
})
})

14
test/unit/directives/myHeadDirective.js

@ -2,25 +2,23 @@ @@ -2,25 +2,23 @@
/* global describe, it, inject, expect, beforeEach */
describe('myHead directive', function () {
var $compile, $rootScope
beforeEach(module('myApp.templates'))
beforeEach(module('myApp.directives'))
beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_
$rootScope = _$rootScope_
this.$compile = _$compile_
this.$rootScope = _$rootScope_
}))
it('compiles a my-head attribute', function () {
var compiledElement = $compile('<div my-head></div>')($rootScope)
$rootScope.$digest() // Fire watchers
var compiledElement = this.$compile('<div my-head></div>')(this.$rootScope)
this.$rootScope.$digest()
expect(compiledElement.html()).toContain('tg_page_head')
})
it('compiles a my-head element', function () {
var compiledElement = $compile('<my-head></my-head>')($rootScope)
$rootScope.$digest() // Fire watchers
var compiledElement = this.$compile('<my-head></my-head>')(this.$rootScope)
this.$rootScope.$digest()
expect(compiledElement.html()).toContain('tg_page_head')
})
})

14
test/unit/directives/myLangFooterDirective.js

@ -2,8 +2,6 @@ @@ -2,8 +2,6 @@
/* global describe, it, inject, expect, beforeEach */
describe('myLangFooter directive', function () {
var $compile, $rootScope
beforeEach(module('ui.bootstrap'))
beforeEach(module('myApp.templates'))
// ErrorServiceProvider in myApp.services is needed by
@ -13,20 +11,20 @@ describe('myLangFooter directive', function () { @@ -13,20 +11,20 @@ describe('myLangFooter directive', function () {
beforeEach(module('myApp.directives'))
beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_
$rootScope = _$rootScope_
this.$compile = _$compile_
this.$rootScope = _$rootScope_
}))
it('compiles a my-lang-footer attribute', function () {
var compiledElement = $compile('<div my-lang-footer></div>')($rootScope)
$rootScope.$digest() // Fire watchers
var compiledElement = this.$compile('<div my-lang-footer></div>')(this.$rootScope)
this.$rootScope.$digest()
expect(compiledElement.html()).toContain('footer_lang_link')
expect(compiledElement.html()).toContain('AppLangSelectController')
})
it('compiles a my-lang-footer element', function () {
var compiledElement = $compile('<my-lang-footer></my-lang-footer>')($rootScope)
$rootScope.$digest() // Fire watchers
var compiledElement = this.$compile('<my-lang-footer></my-lang-footer>')(this.$rootScope)
this.$rootScope.$digest()
expect(compiledElement.html()).toContain('footer_lang_link')
expect(compiledElement.html()).toContain('AppLangSelectController')
})

20
test/unit/services/PhonebookContactsServiceSpec.js

@ -2,52 +2,50 @@ @@ -2,52 +2,50 @@
/* global describe, it, inject, expect, beforeEach, jasmine, xit */
describe('PhonebookContactsService', function () {
var PhonebookContactsService, $modal
beforeEach(module('ui.bootstrap'))
beforeEach(module('myApp.services'))
beforeEach(inject(function (_PhonebookContactsService_) {
PhonebookContactsService = _PhonebookContactsService_
this.PhonebookContactsService = _PhonebookContactsService_
}))
describe('Public API:', function () {
it('checks availability', function () {
expect(PhonebookContactsService.isAvailable).toBeDefined()
expect(this.PhonebookContactsService.isAvailable).toBeDefined()
})
it('open the phonebook for import', function () {
expect(PhonebookContactsService.openPhonebookImport).toBeDefined()
expect(this.PhonebookContactsService.openPhonebookImport).toBeDefined()
})
it('get phonebook contacts', function () {
expect(PhonebookContactsService.getPhonebookContacts).toBeDefined()
expect(this.PhonebookContactsService.getPhonebookContacts).toBeDefined()
})
describe('usage', function () {
describe('of isAvailable()', function () {
it('returns false in most cases', function (done) {
expect(PhonebookContactsService.isAvailable()).toBe(false)
expect(this.PhonebookContactsService.isAvailable()).toBe(false)
done()
})
})
describe('of openPhonebookImport()', function () {
beforeEach(function () {
$modal = {
this.$modal = {
open: jasmine.createSpy('open')
}
})
xit('opens a modal', function () {
PhonebookContactsService.openPhonebookImport()
expect($modal.open).toHaveBeenCalled()
this.PhonebookContactsService.openPhonebookImport()
expect(this.$modal.open).toHaveBeenCalled()
})
})
describe('of getPhonebookContacts()', function () {
xit('will get rejected in most cases', function (done) {
var promise = PhonebookContactsService.getPhonebookContacts()
var promise = this.PhonebookContactsService.getPhonebookContacts()
promise.finally(function () {
expect(promise.isFullfilled()).toBe(true)
done()

Loading…
Cancel
Save