From cd3979aeab294655144409c8a58a3c02b7900ffe Mon Sep 17 00:00:00 2001 From: Bart Date: Wed, 28 Jun 2017 17:03:49 +0200 Subject: [PATCH 1/3] Cleanup of tests, improved readability of the test code (#1434) * Cleanup of tests, improved readability * Improved readability concerning timeout/loading tests, removed unnecessary 'this.' --- app/js/controllers.js | 4 +- .../controllers/AppFooterControllerSpec.js | 24 +- .../controllers/AppImPanelControllerSpec.js | 10 +- .../AppLangSelectControllerSpec.js | 23 +- .../controllers/AppWelcomeControllerSpec.js | 37 +-- .../ChangelogModalControlelerSpec.js | 55 ++-- .../DocumentModalControllerSpec.js | 145 +++++---- test/unit/controllers/EmbedModalController.js | 74 +++-- .../controllers/PeerSelectControllerSpec.js | 305 +++++++++--------- .../controllers/VideoModalControllerSpec.js | 152 +++++---- test/unit/directives/myHeadDirective.js | 14 +- test/unit/directives/myLangFooterDirective.js | 14 +- .../services/PhonebookContactsServiceSpec.js | 20 +- 13 files changed, 439 insertions(+), 438 deletions(-) diff --git a/app/js/controllers.js b/app/js/controllers.js index 2057bb6e..b579e8bb 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -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']) } $scope.$on('history_delete', function (e, historyUpdate) { - if (historyUpdate.msgs[$scope.messageID]) { + if (historyUpdate && historyUpdate.msgs && historyUpdate.msgs[$scope.messageID]) { $modalInstance.dismiss() } }) diff --git a/test/unit/controllers/AppFooterControllerSpec.js b/test/unit/controllers/AppFooterControllerSpec.js index 39f210af..4f857192 100644 --- a/test/unit/controllers/AppFooterControllerSpec.js +++ b/test/unit/controllers/AppFooterControllerSpec.js @@ -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() }) }) diff --git a/test/unit/controllers/AppImPanelControllerSpec.js b/test/unit/controllers/AppImPanelControllerSpec.js index 2b02a09a..260aa266 100644 --- a/test/unit/controllers/AppImPanelControllerSpec.js +++ b/test/unit/controllers/AppImPanelControllerSpec.js @@ -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() }) }) diff --git a/test/unit/controllers/AppLangSelectControllerSpec.js b/test/unit/controllers/AppLangSelectControllerSpec.js index c4cba5bb..07ddadca 100644 --- a/test/unit/controllers/AppLangSelectControllerSpec.js +++ b/test/unit/controllers/AppLangSelectControllerSpec.js @@ -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 () { }) 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 () { diff --git a/test/unit/controllers/AppWelcomeControllerSpec.js b/test/unit/controllers/AppWelcomeControllerSpec.js index 9e3cadd8..1787fce0 100644 --- a/test/unit/controllers/AppWelcomeControllerSpec.js +++ b/test/unit/controllers/AppWelcomeControllerSpec.js @@ -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 () { } } - 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 }) }) }) diff --git a/test/unit/controllers/ChangelogModalControlelerSpec.js b/test/unit/controllers/ChangelogModalControlelerSpec.js index 86c6ed1a..3ad28f62 100644 --- a/test/unit/controllers/ChangelogModalControlelerSpec.js +++ b/test/unit/controllers/ChangelogModalControlelerSpec.js @@ -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() }) }) diff --git a/test/unit/controllers/DocumentModalControllerSpec.js b/test/unit/controllers/DocumentModalControllerSpec.js index 9593b4fa..471b84b9 100644 --- a/test/unit/controllers/DocumentModalControllerSpec.js +++ b/test/unit/controllers/DocumentModalControllerSpec.js @@ -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 () { } } - $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 () { } } - 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() + }) }) }) diff --git a/test/unit/controllers/EmbedModalController.js b/test/unit/controllers/EmbedModalController.js index 7aa89649..72bfe10d 100644 --- a/test/unit/controllers/EmbedModalController.js +++ b/test/unit/controllers/EmbedModalController.js @@ -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 () { } } - $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 () { } } - $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() }) }) diff --git a/test/unit/controllers/PeerSelectControllerSpec.js b/test/unit/controllers/PeerSelectControllerSpec.js index 79e858e3..492f489e 100644 --- a/test/unit/controllers/PeerSelectControllerSpec.js +++ b/test/unit/controllers/PeerSelectControllerSpec.js @@ -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 () { } } - // 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() + }) }) }) diff --git a/test/unit/controllers/VideoModalControllerSpec.js b/test/unit/controllers/VideoModalControllerSpec.js index 9f5e14f7..b24e3ee1 100644 --- a/test/unit/controllers/VideoModalControllerSpec.js +++ b/test/unit/controllers/VideoModalControllerSpec.js @@ -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 () { } } - $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 () { } } - 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() + }) + }) }) diff --git a/test/unit/directives/myHeadDirective.js b/test/unit/directives/myHeadDirective.js index dc7294e9..029aebc4 100644 --- a/test/unit/directives/myHeadDirective.js +++ b/test/unit/directives/myHeadDirective.js @@ -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('
')($rootScope) - $rootScope.$digest() // Fire watchers + var compiledElement = this.$compile('
')(this.$rootScope) + this.$rootScope.$digest() expect(compiledElement.html()).toContain('tg_page_head') }) it('compiles a my-head element', function () { - var compiledElement = $compile('')($rootScope) - $rootScope.$digest() // Fire watchers + var compiledElement = this.$compile('')(this.$rootScope) + this.$rootScope.$digest() expect(compiledElement.html()).toContain('tg_page_head') }) }) diff --git a/test/unit/directives/myLangFooterDirective.js b/test/unit/directives/myLangFooterDirective.js index abb987d9..2ebdaff0 100644 --- a/test/unit/directives/myLangFooterDirective.js +++ b/test/unit/directives/myLangFooterDirective.js @@ -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 () { 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('
')($rootScope) - $rootScope.$digest() // Fire watchers + var compiledElement = this.$compile('
')(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('')($rootScope) - $rootScope.$digest() // Fire watchers + var compiledElement = this.$compile('')(this.$rootScope) + this.$rootScope.$digest() expect(compiledElement.html()).toContain('footer_lang_link') expect(compiledElement.html()).toContain('AppLangSelectController') }) diff --git a/test/unit/services/PhonebookContactsServiceSpec.js b/test/unit/services/PhonebookContactsServiceSpec.js index 2d3225b0..a6128ca0 100644 --- a/test/unit/services/PhonebookContactsServiceSpec.js +++ b/test/unit/services/PhonebookContactsServiceSpec.js @@ -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() From aea853055654b5b2d67231dde3badeda511d977b Mon Sep 17 00:00:00 2001 From: Bart Date: Wed, 28 Jun 2017 17:04:48 +0200 Subject: [PATCH 2/3] Added tests to improve Controller test coverage to over 10% (#1428) * fully tested CountrySelectModal Controller * Added test CountrySelectModal Controller for forgotten scenarios * fully tested ImportContactModal Controller * fully tested ProfileEditModal Controller and cleanup style of tests * fully tested PasswordRecoveryModal Controller * tested most of UsernameEditModal Controller * Improved readability, refactored beforeEach ImportContactModalControllerSpec * Tried to make the code more DRY * removed unnecessary 'this.' --- app/js/controllers.js | 14 +- .../CountrySelectModalControllerSpec.js | 181 ++++++++++++++++++ .../ImportContactModalControllerSpec.js | 160 ++++++++++++++++ .../PasswordRecoveryModalControllerSpec.js | 113 +++++++++++ .../ProfileEditModalControllerSpec.js | 108 +++++++++++ .../UsernameEditModalControllerSpec.js | 146 ++++++++++++++ 6 files changed, 716 insertions(+), 6 deletions(-) create mode 100644 test/unit/controllers/CountrySelectModalControllerSpec.js create mode 100644 test/unit/controllers/ImportContactModalControllerSpec.js create mode 100644 test/unit/controllers/PasswordRecoveryModalControllerSpec.js create mode 100644 test/unit/controllers/ProfileEditModalControllerSpec.js create mode 100644 test/unit/controllers/UsernameEditModalControllerSpec.js diff --git a/app/js/controllers.js b/app/js/controllers.js index b579e8bb..ad7ffe58 100644 --- a/app/js/controllers.js +++ b/app/js/controllers.js @@ -4455,9 +4455,11 @@ angular.module('myApp.controllers', ['myApp.i18n']) AppUsersManager.saveApiUser(user) $modalInstance.close() }, function (error) { - if (error.type == 'USERNAME_NOT_MODIFIED') { - error.handled = true - $modalInstance.close() + switch (error.type) { + case 'USERNAME_NOT_MODIFIED': + error.handled = true + $modalInstance.close() + break } })['finally'](function () { delete $scope.profile.updating @@ -4470,9 +4472,9 @@ angular.module('myApp.controllers', ['myApp.i18n']) return } MtpApiManager.invokeApi('account.checkUsername', { - username: newVal || '' + username: newVal }).then(function (valid) { - if ($scope.profile.username != newVal) { + if ($scope.profile.username !== newVal) { return } if (valid) { @@ -4481,7 +4483,7 @@ angular.module('myApp.controllers', ['myApp.i18n']) $scope.checked = {error: true} } }, function (error) { - if ($scope.profile.username != newVal) { + if ($scope.profile.username !== newVal) { return } switch (error.type) { diff --git a/test/unit/controllers/CountrySelectModalControllerSpec.js b/test/unit/controllers/CountrySelectModalControllerSpec.js new file mode 100644 index 00000000..8d7ea801 --- /dev/null +++ b/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() + }) + }) + }) +}) diff --git a/test/unit/controllers/ImportContactModalControllerSpec.js b/test/unit/controllers/ImportContactModalControllerSpec.js new file mode 100644 index 00000000..1603d348 --- /dev/null +++ b/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() + }) + }) +}) diff --git a/test/unit/controllers/PasswordRecoveryModalControllerSpec.js b/test/unit/controllers/PasswordRecoveryModalControllerSpec.js new file mode 100644 index 00000000..3c2285f1 --- /dev/null +++ b/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() + }) + }) +}) diff --git a/test/unit/controllers/ProfileEditModalControllerSpec.js b/test/unit/controllers/ProfileEditModalControllerSpec.js new file mode 100644 index 00000000..5fc86031 --- /dev/null +++ b/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() + }) +}) diff --git a/test/unit/controllers/UsernameEditModalControllerSpec.js b/test/unit/controllers/UsernameEditModalControllerSpec.js new file mode 100644 index 00000000..b09a26f4 --- /dev/null +++ b/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() + }) +}) From d4f07a714ec9e4d869b9787ca47e892ade146295 Mon Sep 17 00:00:00 2001 From: Bakyt Date: Wed, 28 Jun 2017 18:06:48 +0300 Subject: [PATCH 3/3] license url updated for nanoScrollerJS (#1392) --- app/vendor/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/vendor/README.md b/app/vendor/README.md index 79946f76..603a57de 100644 --- a/app/vendor/README.md +++ b/app/vendor/README.md @@ -46,7 +46,7 @@ Normalize, CSS-framework ### [nanoScrollerJS](https://github.com/jamesflorentino/nanoScrollerJS) **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