Igor Zhukov 8 years ago
parent
commit
db1dcadae6
  1. 14
      app/js/controllers.js
  2. 2
      app/vendor/README.md
  3. 24
      test/unit/controllers/AppFooterControllerSpec.js
  4. 10
      test/unit/controllers/AppImPanelControllerSpec.js
  5. 23
      test/unit/controllers/AppLangSelectControllerSpec.js
  6. 37
      test/unit/controllers/AppWelcomeControllerSpec.js
  7. 55
      test/unit/controllers/ChangelogModalControlelerSpec.js
  8. 181
      test/unit/controllers/CountrySelectModalControllerSpec.js
  9. 141
      test/unit/controllers/DocumentModalControllerSpec.js
  10. 74
      test/unit/controllers/EmbedModalController.js
  11. 160
      test/unit/controllers/ImportContactModalControllerSpec.js
  12. 113
      test/unit/controllers/PasswordRecoveryModalControllerSpec.js
  13. 235
      test/unit/controllers/PeerSelectControllerSpec.js
  14. 108
      test/unit/controllers/ProfileEditModalControllerSpec.js
  15. 146
      test/unit/controllers/UsernameEditModalControllerSpec.js
  16. 152
      test/unit/controllers/VideoModalControllerSpec.js
  17. 14
      test/unit/directives/myHeadDirective.js
  18. 14
      test/unit/directives/myLangFooterDirective.js
  19. 20
      test/unit/services/PhonebookContactsServiceSpec.js

14
app/js/controllers.js

@ -3537,7 +3537,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
} }
$scope.$on('history_delete', function (e, historyUpdate) { $scope.$on('history_delete', function (e, historyUpdate) {
if (historyUpdate.msgs[$scope.messageID]) { if (historyUpdate && historyUpdate.msgs && historyUpdate.msgs[$scope.messageID]) {
$modalInstance.dismiss() $modalInstance.dismiss()
} }
}) })
@ -3571,7 +3571,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
} }
$scope.$on('history_delete', function (e, historyUpdate) { $scope.$on('history_delete', function (e, historyUpdate) {
if (historyUpdate.msgs[$scope.messageID]) { if (historyUpdate && historyUpdate.msgs && historyUpdate.msgs[$scope.messageID]) {
$modalInstance.dismiss() $modalInstance.dismiss()
} }
}) })
@ -4455,9 +4455,11 @@ angular.module('myApp.controllers', ['myApp.i18n'])
AppUsersManager.saveApiUser(user) AppUsersManager.saveApiUser(user)
$modalInstance.close() $modalInstance.close()
}, function (error) { }, function (error) {
if (error.type == 'USERNAME_NOT_MODIFIED') { switch (error.type) {
case 'USERNAME_NOT_MODIFIED':
error.handled = true error.handled = true
$modalInstance.close() $modalInstance.close()
break
} }
})['finally'](function () { })['finally'](function () {
delete $scope.profile.updating delete $scope.profile.updating
@ -4470,9 +4472,9 @@ angular.module('myApp.controllers', ['myApp.i18n'])
return return
} }
MtpApiManager.invokeApi('account.checkUsername', { MtpApiManager.invokeApi('account.checkUsername', {
username: newVal || '' username: newVal
}).then(function (valid) { }).then(function (valid) {
if ($scope.profile.username != newVal) { if ($scope.profile.username !== newVal) {
return return
} }
if (valid) { if (valid) {
@ -4481,7 +4483,7 @@ angular.module('myApp.controllers', ['myApp.i18n'])
$scope.checked = {error: true} $scope.checked = {error: true}
} }
}, function (error) { }, function (error) {
if ($scope.profile.username != newVal) { if ($scope.profile.username !== newVal) {
return return
} }
switch (error.type) { switch (error.type) {

2
app/vendor/README.md vendored

@ -46,7 +46,7 @@ Normalize, CSS-framework
### [nanoScrollerJS](https://github.com/jamesflorentino/nanoScrollerJS) ### [nanoScrollerJS](https://github.com/jamesflorentino/nanoScrollerJS)
**Author**: James Florentino **Author**: James Florentino
**License**: MIT, https://github.com/jamesflorentino/nanoScrollerJS/blob/master/LICENSE-MIT **License**: MIT, https://github.com/jamesflorentino/nanoScrollerJS/blob/master/LICENSE
Beautiful OS X Lion-like scrollbars Beautiful OS X Lion-like scrollbars

24
test/unit/controllers/AppFooterControllerSpec.js

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

10
test/unit/controllers/AppImPanelControllerSpec.js

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

23
test/unit/controllers/AppLangSelectControllerSpec.js

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

37
test/unit/controllers/AppWelcomeControllerSpec.js

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

55
test/unit/controllers/ChangelogModalControlelerSpec.js

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

181
test/unit/controllers/CountrySelectModalControllerSpec.js

@ -0,0 +1,181 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, afterEach, spyOn, jasmine, Config, SearchIndexManager */
describe('CountrySelectModalController', function () {
beforeEach(module('myApp.controllers'))
beforeEach(inject(function (_$controller_, _$rootScope_, ___) {
this.$controller = _$controller_
this.$rootScope = _$rootScope_
this._ = ___
this.$scope = _$rootScope_.$new()
this.createController = function () {
this.$controller('CountrySelectModalController', {
$scope: this.$scope,
$modalInstance: {},
$rootScope: this.$rootScope,
_: this._
})
}
spyOn(SearchIndexManager, 'indexObject').and.callThrough()
}))
beforeEach(function () {
this.ConfigCountryCodes = Config.CountryCodes
this.testData = {
singleCode: {
countryCode: ['NL', 'country_select_modal_country_nl', '+31'],
countryCode_full: 'NL Netherlands +31',
countryPhoneSets: [{ name: 'Netherlands', code: '+31' }]
},
multipleCode: {
countryCode: ['VA', 'country_select_modal_country_va', '+39 06 698', '+379'],
countryCode_full: 'VA Vatican City +39 06 698 +379',
countryPhoneSets: [{ name: 'Vatican City', code: '+39 06 698' }, { name: 'Vatican City', code: '+379' }]
},
multipleCode2: {
countryCode: ['AB', 'country_select_modal_country_ab', '+7 840', '+7 940', '+995 44'],
countryCode_full: 'AB Abkhazia +7 840 +7 940 +995 44',
countryPhoneSets: [{ name: 'Abkhazia', code: '+7 840' }, { name: 'Abkhazia', code: '+7 940' }, { name: 'Abkhazia', code: '+995 44' }]
},
allSetsSorted: function () {
return [].concat(this.multipleCode2.countryPhoneSets, this.singleCode.countryPhoneSets, this.multipleCode.countryPhoneSets)
},
allSetsUnsorted: function () {
return [].concat(this.singleCode.countryPhoneSets, this.multipleCode2.countryPhoneSets, this.multipleCode.countryPhoneSets)
}
}
})
afterEach(function () {
Config.CountryCodes = this.ConfigCountryCodes
})
// The tests before controller initiation.
// In order to mock Config data
it('initiates Country to select', function (done) {
Config.CountryCodes = [this.testData.singleCode.countryCode]
var expected = this.testData.singleCode.countryCode_full
this.createController()
expect(SearchIndexManager.indexObject).toHaveBeenCalledWith(0, expected, jasmine.any(Object))
done()
})
it('initiates Countriy to select with 2 (or more) country codes', function (done) {
Config.CountryCodes = [this.testData.multipleCode.countryCode]
var expected = this.testData.multipleCode.countryCode_full
this.createController()
expect(SearchIndexManager.indexObject).toHaveBeenCalledWith(0, expected, jasmine.any(Object))
done()
})
it('initiates Countries to select', function (done) {
Config.CountryCodes = [this.testData.singleCode.countryCode, this.testData.multipleCode.countryCode]
var expected1 = this.testData.singleCode.countryCode_full
var expected2 = this.testData.multipleCode.countryCode_full
this.createController()
expect(SearchIndexManager.indexObject).toHaveBeenCalledWith(0, expected1, jasmine.any(Object))
expect(SearchIndexManager.indexObject).toHaveBeenCalledWith(1, expected2, jasmine.any(Object))
done()
})
describe('(after initiation)', function () {
beforeEach(function () {
Config.CountryCodes = [this.testData.singleCode.countryCode, this.testData.multipleCode2.countryCode, this.testData.multipleCode.countryCode]
this.createController()
})
it('initiates the right values', function (done) {
expect(this.$scope.search).toEqual({})
expect(this.$scope.slice).toEqual({limit: 20, limitDelta: 20})
done()
})
it('creates a sorted list of all selectable countries', function (done) {
this.$rootScope.$digest()
var expected = this.testData.allSetsSorted()
expect(this.$scope.countries).toEqual(expected)
done()
})
it('creates a sorted list of all selectable countries for an empty string-input', function (done) {
this.$rootScope.$digest()
this.$scope.search.query = ''
this.$rootScope.$digest()
var expected = this.testData.allSetsSorted()
expect(this.$scope.countries).toEqual(expected)
done()
})
describe(', when an input is given,', function () {
beforeEach(function () {
this.$rootScope.$digest()
this.$scope.search.query = 'A'
})
it('creates a sorted list of all countries containing the input', function (done) {
var expected = this.testData.allSetsSorted()
expect(this.$scope.countries).toEqual(expected)
this.$rootScope.$digest()
expected = this.testData.multipleCode2.countryPhoneSets
expect(this.$scope.countries).toEqual(expected)
done()
})
it('restore the original list when the input is deleted', function (done) {
this.$rootScope.$digest()
this.$scope.search.query = ''
this.$rootScope.$digest()
var expected = this.testData.allSetsSorted()
expect(this.$scope.countries).toEqual(expected)
done()
})
it('restore the original list when the input is changed', function (done) {
this.$rootScope.$digest()
this.$scope.search.query = 'Ne'
this.$rootScope.$digest()
var expected = this.testData.singleCode.countryPhoneSets
expect(this.$scope.countries).toEqual(expected)
done()
})
})
describe(', when no sorting is available,', function () {
beforeEach(function () {
this.StringCompare = String.prototype.localeCompare
String.prototype.localeCompare = null
})
afterEach(function () {
String.prototype.localeCompare = this.StringCompare
})
it('creates a list of all selectable countries', function (done) {
this.$rootScope.$digest()
var expected = this.testData.allSetsUnsorted()
expect(this.$scope.countries).toEqual(expected)
done()
})
})
})
})

141
test/unit/controllers/DocumentModalControllerSpec.js

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

74
test/unit/controllers/EmbedModalController.js

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

160
test/unit/controllers/ImportContactModalControllerSpec.js

@ -0,0 +1,160 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */
describe('ImportContactModalController', function () {
beforeEach(module('myApp.controllers'))
beforeEach(function () {
this.modalInstance = {
close: jasmine.createSpy('close'),
dismiss: jasmine.createSpy('dismiss')
}
this.randomID = 123456852
function thenFinallyFactory (input) {
return {
then: function (callback) {
callback(input)
return {
finally: function (callback) {
callback()
}
}
}
}
}
this.AppUsersManager = {
thenValue: null,
importContact: function (phone, first, last) {
this.input = {
phone: phone,
first: first,
last: last
}
return thenFinallyFactory(this.thenValue)
}
}
this.ErrorService = {
show: jasmine.createSpy('show')
}
this.PhonebookContactsService = {
thenValue: false,
isAvailable: function () {
return false
},
openPhonebookImport: function () {
var then = thenFinallyFactory(this.thenValue)
return {
result: then
}
}
}
inject(function (_$controller_, _$rootScope_) {
this.$controller = _$controller_
this.$rootScope = _$rootScope_
this.$scope = _$rootScope_.$new()
this.createController = function () {
this.$controller('ImportContactModalController', {
$scope: this.$scope,
$modalInstance: this.modalInstance,
$rootScope: this.$rootScope,
AppUsersManager: this.AppUsersManager,
ErrorService: this.ErrorService,
PhonebookContactsService: this.PhonebookContactsService
})
}
})
})
it('can create a controller when no imported contacts are defined', function (done) {
this.createController()
expect(this.$scope.importContact).toEqual({})
done()
})
it('can create a controller when imported contacts are defined', function (done) {
this.$scope.importContact = { non_empty: true }
this.createController()
var expected = { non_empty: true }
expect(this.$scope.importContact).toEqual(expected)
done()
})
describe('(when the controller is created), ', function () {
beforeEach(function () {
this.createController()
})
it('does nothing when no phonenumber was entered', function (done) {
this.$scope.doImport()
expect(this.$scope.progress).not.toBeDefined()
this.$scope.importContact = {
first_name: 'bob'
}
expect(this.$scope.progress).not.toBeDefined()
done()
})
describe('when contact-information is added, it', function () {
beforeEach(function () {
this.$scope.importContact = {
phone: '+316132465798'
}
})
it('can handle phoneNumber that are not telegram users', function (done) {
this.$scope.doImport()
expect(this.ErrorService.show).toHaveBeenCalledWith({ error: {code: 404, type: 'USER_NOT_USING_TELEGRAM'} })
expect(this.modalInstance.close).toHaveBeenCalledWith(null)
expect(this.$scope.progress.enabled).not.toBeDefined()
done()
})
it('can import contacts that are telegram users', function (done) {
this.AppUsersManager.thenValue = this.randomID
this.$scope.doImport()
expect(this.ErrorService.show).not.toHaveBeenCalled()
expect(this.modalInstance.close).toHaveBeenCalledWith(this.randomID)
expect(this.$scope.progress.enabled).not.toBeDefined()
expect(this.AppUsersManager.input).toEqual({phone: '+316132465798', first: '', last: ''})
done()
})
it('can handle contacts with first and last name', function (done) {
this.$scope.importContact.first_name = 'jan'
this.$scope.importContact.last_name = 'wandelaar'
this.$scope.doImport()
expect(this.AppUsersManager.input).toEqual({phone: '+316132465798', first: 'jan', last: 'wandelaar'})
done()
})
})
it('can not import contacts from a phonebook if none were found', function (done) {
this.$scope.importPhonebook()
expect(this.modalInstance.dismiss).toHaveBeenCalled()
done()
})
it('can import contacts from a phonebook', function (done) {
this.PhonebookContactsService.thenValue = {0: 'dummy'}
this.$scope.importPhonebook()
expect(this.modalInstance.close).toHaveBeenCalledWith('dummy')
done()
})
})
})

113
test/unit/controllers/PasswordRecoveryModalControllerSpec.js

@ -0,0 +1,113 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */
describe('PasswordRecoveryModalController', function () {
beforeEach(module('myApp.controllers'))
beforeEach(function () {
this.PasswordManager = {
errorField: null,
recover: function () {
return this
},
then: function (callback, error) {
if (!this.errorField) {
callback({})
} else {
error(this.errorField)
}
return {
finally: function (final) {
final()
}
}
}
}
this.ErrorService = { alert: jasmine.createSpy('alert') }
this.modalInstance = {
close: jasmine.createSpy('close'),
dismiss: jasmine.createSpy('dismiss')
}
inject(function (_$controller_, _$rootScope_, ___) {
this.$controller = _$controller_
this.$scope = _$rootScope_.$new()
this.$scope.recovery = {}
this.$controller('PasswordRecoveryModalController', {
$scope: this.$scope,
$q: {},
_: ___,
PasswordManager: this.PasswordManager,
MtpApiManager: {},
ErrorService: this.ErrorService,
$modalInstance: this.modalInstance
})
})
})
it('can handle a successful password change', function (done) {
this.$scope.checkCode()
expect(this.$scope.recovery.updating).toBe(true)
expect(this.ErrorService.alert).toHaveBeenCalledWith('Password deactivated', 'You have disabled Two-Step Verification.')
expect(this.modalInstance.close).toHaveBeenCalled()
done()
})
describe('when an error occurs', function () {
beforeEach(function () {
this.PasswordManager.errorField = {}
})
it('cancels the recovery', function (done) {
this.$scope.checkCode()
expect(this.$scope.recovery.updating).not.toBeDefined()
expect(this.ErrorService.alert).not.toHaveBeenCalled()
expect(this.modalInstance.close).not.toHaveBeenCalled()
done()
})
it('can handle the error for an empty code', function (done) {
this.PasswordManager.errorField.type = 'CODE_EMPTY'
this.$scope.checkCode()
expect(this.$scope.recovery.error_field).toEqual('code')
done()
})
it('can handle the error for an invalid code', function (done) {
this.PasswordManager.errorField.type = 'CODE_INVALID'
this.$scope.checkCode()
expect(this.$scope.recovery.error_field).toEqual('code')
done()
})
it('can handle the error for an empty password', function (done) {
this.PasswordManager.errorField.type = 'PASSWORD_EMPTY'
this.$scope.checkCode()
expect(this.modalInstance.dismiss).toHaveBeenCalled()
done()
})
it('can handle the error for the unavailability of the recovery', function (done) {
this.PasswordManager.errorField.type = 'PASSWORD_RECOVERY_NA'
this.$scope.checkCode()
expect(this.modalInstance.dismiss).toHaveBeenCalled()
done()
})
it('can handle the error for an expired recovery', function (done) {
this.PasswordManager.errorField.type = 'PASSWORD_RECOVERY_EXPIRED'
this.$scope.checkCode()
expect(this.modalInstance.dismiss).toHaveBeenCalled()
done()
})
})
})

235
test/unit/controllers/PeerSelectControllerSpec.js

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

108
test/unit/controllers/ProfileEditModalControllerSpec.js

@ -0,0 +1,108 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */
describe('ProfileEditModalController', function () {
beforeEach(module('myApp.controllers'))
beforeEach(function () {
var id = 42
this.randomID = id
this.MtpApiManager = {
errorField: null,
getUserID: function () {
return {
then: function (callback) {
callback(id)
}
}
},
invokeApi: function (action, params) {
return this
},
then: function (callback, error) {
if (!this.errorField) {
callback({})
} else {
error(this.errorField)
}
return {
finally: function (final) {
final()
}
}
}
}
this.AppUsersManager = {
getUser: function (userId) {
return {
first_name: 'John',
last_name: 'Doe'
}
},
saveApiUser: jasmine.createSpy('saveApiUser')
}
this.$modalInstance = { close: jasmine.createSpy('close') }
inject(function (_$controller_, _$rootScope_) {
this.$controller = _$controller_
this.$scope = _$rootScope_.$new()
this.$controller('ProfileEditModalController', {
$scope: this.$scope,
$modalInstance: this.$modalInstance,
AppUsersManager: this.AppUsersManager,
MtpApiManager: this.MtpApiManager
})
})
})
it('should initiate the right scope', function (done) {
expect(this.$scope.profile).toEqual({first_name: 'John', last_name: 'Doe'})
expect(this.$scope.error).toEqual({})
done()
})
it('can send a successful profile update request', function (done) {
this.$scope.updateProfile()
expect(this.AppUsersManager.saveApiUser).toHaveBeenCalled()
expect(this.$modalInstance.close).toHaveBeenCalled()
done()
})
it('can handle empty name/surname', function (done) {
delete this.$scope.profile.first_name
delete this.$scope.profile.last_name
this.$scope.updateProfile()
expect(this.AppUsersManager.saveApiUser).toHaveBeenCalled()
expect(this.$modalInstance.close).toHaveBeenCalled()
done()
})
it('can handle an invalid first name error', function (done) {
this.MtpApiManager.errorField = {type: 'FIRSTNAME_INVALID'}
this.$scope.updateProfile()
expect(this.$scope.error.field).toEqual('first_name')
done()
})
it('can handle an invalid last name error', function (done) {
this.MtpApiManager.errorField = {type: 'LASTNAME_INVALID'}
this.$scope.updateProfile()
expect(this.$scope.error.field).toEqual('last_name')
done()
})
it('can handle an unmodified name error', function (done) {
this.MtpApiManager.errorField = {type: 'NAME_NOT_MODIFIED'}
this.$scope.updateProfile()
expect(this.$modalInstance.close).toHaveBeenCalled()
done()
})
})

146
test/unit/controllers/UsernameEditModalControllerSpec.js

@ -0,0 +1,146 @@
'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */
describe('UsernameEditModalController', function () {
beforeEach(module('myApp.controllers'))
beforeEach(function () {
this.MtpApiManager = {
errorField: false,
isValid: true,
invokeApi: function () {
return this
},
getUserID: function () {
return this
},
then: function (callback, error) {
if (!this.errorField) {
callback(this.isValid)
} else {
error(this.errorField)
}
return {
finally: function (final) {
final()
}
}
}
}
this.AppUsersManager = {
saveApiUser: jasmine.createSpy('saveApiUser'),
getUser: function (id) {
return { username: 'bob' }
}
}
this.modalInstance = {
close: jasmine.createSpy('close')
}
inject(function (_$controller_, _$rootScope_) {
this.$controller = _$controller_
this.$scope = _$rootScope_.$new()
this.$controller('UsernameEditModalController', {
$scope: this.$scope,
$modalInstance: this.modalInstance,
AppUsersManager: this.AppUsersManager,
MtpApiManager: this.MtpApiManager
})
})
})
it('constructs the information for the modal', function (done) {
var expected = {
username: 'bob'
}
expect(this.$scope.profile).toEqual(expected)
expect(this.$scope.error).toEqual({})
done()
})
it('can handle a successful update of the username', function (done) {
this.$scope.updateUsername()
expect(this.$scope.checked).toEqual({})
expect(this.AppUsersManager.saveApiUser).toHaveBeenCalled()
expect(this.modalInstance.close).toHaveBeenCalled()
done()
})
it('can handle a successful update of an empty/undefined username', function (done) {
delete this.$scope.profile.username
this.$scope.updateUsername()
expect(this.$scope.checked).toEqual({})
expect(this.AppUsersManager.saveApiUser).toHaveBeenCalled()
expect(this.modalInstance.close).toHaveBeenCalled()
done()
})
it('can handle an unsuccessful update of an unmodified username', function (done) {
this.MtpApiManager.errorField = { type: 'USERNAME_NOT_MODIFIED' }
this.$scope.updateUsername()
expect(this.$scope.checked).not.toBeDefined()
expect(this.AppUsersManager.saveApiUser).not.toHaveBeenCalled()
expect(this.modalInstance.close).toHaveBeenCalled()
done()
})
it('can check an empty username on change', function (done) {
this.$scope.profile.username = {}
var expected = {}
this.$scope.$digest()
expect(this.$scope.checked).toEqual(expected)
done()
})
it('can check an empty string as username', function (done) {
this.$scope.profile.username = ''
var expected = {}
this.$scope.$digest()
expect(this.$scope.checked).toEqual(expected)
done()
})
it('can check the initial username', function (done) {
// Previous username is expected to be valid
this.$scope.$digest()
var expected = true
expect(this.$scope.checked.success).toBe(expected)
done()
})
it('does not check anything when the name is not changed', function (done) {
this.$scope.$digest()
delete this.$scope.checked.success
this.$scope.$digest()
expect(this.$scope.checked.success).not.toBeDefined()
done()
})
it('can check an invalid username submission', function (done) {
this.MtpApiManager.isValid = false
this.$scope.$digest()
var expected = true
expect(this.$scope.checked.error).toBe(expected)
done()
})
it('can check an invalid username submission 2', function (done) {
this.MtpApiManager.errorField = { type: 'USERNAME_INVALID' }
this.$scope.$digest()
var expected = true
expect(this.$scope.checked.error).toBe(expected)
done()
})
})

152
test/unit/controllers/VideoModalControllerSpec.js

@ -1,20 +1,19 @@
'use strict' 'use strict'
/* global describe, it, inject, expect, beforeEach, jasmine */ /* global describe, it, inject, expect, beforeEach, jasmine, spyOn */
describe('VideoModalController', function () { describe('VideoModalController', function () {
var $controller, $scope, $rootScope, $docManager, $errService, $input, $messManager, $pSelectService, $modalI, createController
beforeEach(module('myApp.controllers')) beforeEach(module('myApp.controllers'))
beforeEach(function () { beforeEach(function () {
$docManager = {} this.AppDocsManager = {
$docManager.wrapVideoForFull = jasmine.createSpy('wrapVideoForFull') wrapVideoForFull: jasmine.createSpy('wrapVideoForFull'),
$docManager.saveDocFile = jasmine.createSpy('saveDocFile') saveDocFile: jasmine.createSpy('saveDocFile')
}
$input = {} this.ErrorService = {
$errService = { input: {},
confirm: function (message) { confirm: function (message) {
$input = message this.input = message
return { return {
then: function (f) { then: function (f) {
f() f()
@ -23,9 +22,10 @@ describe('VideoModalController', function () {
} }
} }
$pSelectService = { this.PeersSelectService = {
input: {},
selectPeer: function (options) { selectPeer: function (options) {
$input = options this.input = options
return { return {
then: function (f) { then: function (f) {
f('Peerselected') f('Peerselected')
@ -34,102 +34,114 @@ describe('VideoModalController', function () {
} }
} }
createController = function (spyBroadcast, spyOn) { this.AppMessagesManager = {
if (spyBroadcast) { deleteMessages: jasmine.createSpy('deleteMessages')
$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
})
} }
$messManager = {} this.$modalInstance = {
$messManager.deleteMessages = jasmine.createSpy('deleteMessages') dismiss: jasmine.createSpy('dismissModal')
}
$modalI = {}
$modalI.dismiss = jasmine.createSpy('dismissModal')
inject(function (_$controller_, _$rootScope_) { inject(function (_$controller_, _$rootScope_) {
$rootScope = _$rootScope_ this.$rootScope = _$rootScope_
$scope = $rootScope.$new() this.$scope = this.$rootScope.$new()
$scope.docID = 'randomvideo' this.$scope.docID = 'randomvideo'
$controller = _$controller_ 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 // define tests
it('sets the video in the scope', function (done) { it('sets the video in the scope', function (done) {
createController(false, false) expect(this.$scope.progress).toEqual({enabled: false})
expect(this.$scope.player).toEqual({})
expect($scope.progress).toEqual({enabled: false}) expect(this.AppDocsManager.wrapVideoForFull).toHaveBeenCalledWith(this.$scope.docID)
expect($scope.player).toEqual({})
expect($docManager.wrapVideoForFull).toHaveBeenCalledWith($scope.docID)
done() done()
}) })
it('forwards a message with a video', function (done) { it('forwards a message with a video', function (done) {
createController(true, false) this.$scope.messageID = 'id68567'
$scope.messageID = 'id68567' var messageID = this.$scope.messageID
$scope.forward() this.$scope.forward()
expect($input).toEqual({canSend: true}) expect(this.PeersSelectService.input).toEqual({canSend: true})
expect($scope.$broadcast).toHaveBeenCalledWith('history_focus', { expect(this.$scope.$broadcast).toHaveBeenCalledWith('history_focus', {
peerString: 'Peerselected', peerString: 'Peerselected',
attachment: { attachment: {
_: 'fwd_messages', _: 'fwd_messages',
id: [$scope.messageID] id: [messageID]
} }
}) })
done() done()
}) })
it('deletes a message with a video', function (done) { it('deletes a message with a video', function (done) {
createController(false, false) this.$scope.messageID = 'id235235'
$scope.messageID = 'id235235'
$scope.delete() this.$scope.delete()
expect($input).toEqual({type: 'MESSAGE_DELETE'}) expect(this.ErrorService.input).toEqual({type: 'MESSAGE_DELETE'})
expect($messManager.deleteMessages).toHaveBeenCalledWith([$scope.messageID]) expect(this.AppMessagesManager.deleteMessages).toHaveBeenCalledWith([this.$scope.messageID])
done() done()
}) })
it('downloads the document (video)', function (done) { it('downloads the document (video)', function (done) {
createController(false, false) this.$scope.download()
expect(this.AppDocsManager.saveDocFile).toHaveBeenCalledWith(this.$scope.docID)
$scope.download()
expect($docManager.saveDocFile).toHaveBeenCalledWith($scope.docID)
done() done()
}) })
it('delete a video linked to a message', function (done) { it('delete a video linked to a message', function (done) {
createController(false, true) this.$scope.messageID = 'id2352'
$scope.messageID = 'id2352'
$rootScope.$broadcast('history_delete') this.$rootScope.$broadcast('history_delete')
expect($scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function)) expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect($modalI.dismiss).not.toHaveBeenCalled() expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
done() done()
}) })
it('delete a video linked to a modal instance', function (done) { it('can not delete a video not linked to a message', function (done) {
createController(false, false) this.$scope.messageID = 'id42'
$scope.messageID = 'id6234'
var historyUpdate = {}
this.$rootScope.$broadcast('history_delete', historyUpdate)
expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
var $msgs = {} historyUpdate.msgs = {}
$msgs[$scope.messageID] = {message: 'some non-empty message'} this.$rootScope.$broadcast('history_delete', historyUpdate)
$rootScope.$broadcast('history_delete', {msgs: $msgs}) expect(this.$scope.$on).toHaveBeenCalledWith('history_delete', jasmine.any(Function))
expect($modalI.dismiss).toHaveBeenCalled() expect(this.$modalInstance.dismiss).not.toHaveBeenCalled()
done() 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 @@
/* global describe, it, inject, expect, beforeEach */ /* global describe, it, inject, expect, beforeEach */
describe('myHead directive', function () { describe('myHead directive', function () {
var $compile, $rootScope
beforeEach(module('myApp.templates')) beforeEach(module('myApp.templates'))
beforeEach(module('myApp.directives')) beforeEach(module('myApp.directives'))
beforeEach(inject(function (_$compile_, _$rootScope_) { beforeEach(inject(function (_$compile_, _$rootScope_) {
$compile = _$compile_ this.$compile = _$compile_
$rootScope = _$rootScope_ this.$rootScope = _$rootScope_
})) }))
it('compiles a my-head attribute', function () { it('compiles a my-head attribute', function () {
var compiledElement = $compile('<div my-head></div>')($rootScope) var compiledElement = this.$compile('<div my-head></div>')(this.$rootScope)
$rootScope.$digest() // Fire watchers this.$rootScope.$digest()
expect(compiledElement.html()).toContain('tg_page_head') expect(compiledElement.html()).toContain('tg_page_head')
}) })
it('compiles a my-head element', function () { it('compiles a my-head element', function () {
var compiledElement = $compile('<my-head></my-head>')($rootScope) var compiledElement = this.$compile('<my-head></my-head>')(this.$rootScope)
$rootScope.$digest() // Fire watchers this.$rootScope.$digest()
expect(compiledElement.html()).toContain('tg_page_head') expect(compiledElement.html()).toContain('tg_page_head')
}) })
}) })

14
test/unit/directives/myLangFooterDirective.js

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

20
test/unit/services/PhonebookContactsServiceSpec.js

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

Loading…
Cancel
Save