From: Gustavo Martin Morcuende Date: Sun, 13 Sep 2015 23:44:53 +0000 (+0200) Subject: showcase: renaming controllers to NameController and using always $scope and $rootSco... X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=cebe73e5f6aa8bcac043b0b2d2331e11be99bf44;p=JavaScriptForFun showcase: renaming controllers to NameController and using always $scope and $rootScope variables in specs. --- diff --git a/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.js b/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.js index 2f6456a..d40d7e8 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.js +++ b/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.js @@ -3,11 +3,11 @@ angular .module('app.cars') - .controller('CarsErrorModal', CarsErrorModal); + .controller('CarsErrorModalController', CarsErrorModalController); /** * @ngdoc controller - * @name app.cars.controller:CarsErrorModal + * @name app.cars.controller:CarsErrorModalController * * @requires $modalInstance * @requires app.cars.cars @@ -21,7 +21,7 @@ * Controller for error modal in cars application. */ /* @ngInject */ - function CarsErrorModal($modalInstance, cars) { + function CarsErrorModalController($modalInstance, cars) { var vm = this; vm.cars = cars; diff --git a/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.spec.js b/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.spec.js index f85e9f4..bf72b5d 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars-error-modal.controller.spec.js @@ -6,13 +6,13 @@ describe('app.cars', function() { close: jasmine.createSpy('modalInstance.close'), dismiss: jasmine.createSpy('modalInstance.dismiss') }; - var CarsErrorModal; + var CarsErrorModalController; beforeEach(function() { module('app.cars'); inject(function($controller) { - CarsErrorModal = $controller('CarsErrorModal', { + CarsErrorModalController = $controller('CarsErrorModalController', { $modalInstance: $modalInstance, cars: cars }); @@ -20,16 +20,16 @@ describe('app.cars', function() { }); - describe('CarsErrorModal controller', function () { + describe('CarsErrorModalController controller', function () { it('should invoke $modalInstance.close', function () { - CarsErrorModal.ok(); + CarsErrorModalController.ok(); expect($modalInstance.close).toHaveBeenCalledWith('car1'); }); it('should invoke $modalInstance.dismiss', function () { - CarsErrorModal.cancel(); + CarsErrorModalController.cancel(); expect($modalInstance.dismiss).toHaveBeenCalledWith('cancel'); }); diff --git a/angularjs/showcase/src/showcase/app/cars/cars.controller.js b/angularjs/showcase/src/showcase/app/cars/cars.controller.js index 7734c20..dec4420 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.js @@ -3,11 +3,11 @@ angular .module('app.cars') - .controller('Cars', Cars); + .controller('CarsController', CarsController); /** * @ngdoc controller - * @name app.cars.controller:Cars + * @name app.cars.controller:CarsController * * @requires $modal * @requires $timeout @@ -20,10 +20,10 @@ *

* * @description - * Cars controller. + * CarsController controller. */ /* @ngInject */ - function Cars($modal, $timeout, cars) { + function CarsController($modal, $timeout, cars) { var vm = this; vm.example = { @@ -40,7 +40,7 @@ }, // Error function(reason) { - console.log('Cars controller error: ' + reason); + console.log('CarsController controller error: ' + reason); vm.doModal('lg'); } ); @@ -51,19 +51,19 @@ cars.getById(1).then( function (value) { - console.log('getCar getById: Cars controller value: ' + JSON.stringify(value)); + console.log('getCar getById: CarsController controller value: ' + JSON.stringify(value)); }, function(reason) { - console.log('getCar getById: Cars controller reason: ' + JSON.stringify(reason)); + console.log('getCar getById: CarsController controller reason: ' + JSON.stringify(reason)); } ); cars.getExpectedValue().then( function (value) { - console.log('getCar getExpectedValue: Cars controller value: ' + JSON.stringify(value)); + console.log('getCar getExpectedValue: CarsController controller value: ' + JSON.stringify(value)); }, function(reason) { - console.log('getCar getExpectedValue: Cars controller reason: ' + JSON.stringify(reason)); + console.log('getCar getExpectedValue: CarsController controller reason: ' + JSON.stringify(reason)); } ); @@ -74,10 +74,12 @@ cars.getExpectedValue().then( function (value) { - console.log('getResolvedPromise getExpectedValue: Cars controller value: ' + JSON.stringify(value)); + console.log('getResolvedPromise getExpectedValue: CarsController controller value: ' + + JSON.stringify(value)); }, function(reason) { - console.log('getResolvedPromise getExpectedValue: Cars controller reason: ' + JSON.stringify(reason)); + console.log('getResolvedPromise getExpectedValue: CarsController controller reason: ' + + JSON.stringify(reason)); } ); @@ -95,7 +97,7 @@ var modalInstance = $modal.open({ animation: true, templateUrl: 'app/cars/cars-error-modal.html', - controller: 'CarsErrorModal as vm', + controller: 'CarsErrorModalController as vm', size: size, backdrop: 'static', keyboard: false, diff --git a/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js b/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js index b4f2030..81f1924 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js @@ -10,14 +10,14 @@ describe('app.cars', function() { return {}; } }; - var Cars; + var CarsController; var $q; beforeEach(function() { module('app.cars'); inject(function($controller, $modal, $timeout, _$q_) { - Cars = $controller('Cars', { + CarsController = $controller('CarsController', { $modal: $modal, $timeout: $timeout, cars: cars @@ -43,10 +43,10 @@ describe('app.cars', function() { }; }); - Cars.getCars(); + CarsController.getCars(); expect(cars.getAll).toHaveBeenCalled(); - expect(Cars.cars).toEqual(onFulfilledValue); + expect(CarsController.cars).toEqual(onFulfilledValue); }); }); @@ -114,14 +114,14 @@ describe('app.cars', function() { }) }; var $timeout; - var Cars; + var CarsController; var $q; beforeEach(function() { module('app.cars'); inject(function($controller, _$timeout_, _$q_) { - Cars = $controller('Cars', { + CarsController = $controller('CarsController', { $modal: $modal, $timeout: _$timeout_, cars: cars @@ -135,46 +135,46 @@ describe('app.cars', function() { it('should invoke GET all cars in service with success: alternative way', function () { - Cars.getCars(); + CarsController.getCars(); expect(cars.getAll).toHaveBeenCalled(); - expect(Cars.cars).toEqual(onFulfilledValue); + expect(CarsController.cars).toEqual(onFulfilledValue); }); it('should invoke GET all cars in service with error: alternative way', function () { isSuccessCallBack = false; - spyOn(Cars, 'doModal') + spyOn(CarsController, 'doModal') .and.callFake(function() { return {}; }); - Cars.getCars(); + CarsController.getCars(); expect(cars.getAll).toHaveBeenCalled(); - expect(Cars.doModal).toHaveBeenCalled(); + expect(CarsController.doModal).toHaveBeenCalled(); isSuccessCallBack = true; }); it('should invoke $modal.open', function () { - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modal.open).toHaveBeenCalled(); }); it('should invoke $modalInstance.result with success', function () { - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modalInstance.result.then).toHaveBeenCalled(); - expect(Cars.selected).toEqual(onFulfilledValue); + expect(CarsController.selected).toEqual(onFulfilledValue); }); it('should invoke $modalInstance.result with error', function () { isSuccessCallBack = false; - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modalInstance.result.then).toHaveBeenCalled(); @@ -183,7 +183,7 @@ describe('app.cars', function() { it('should invoke $modalInstance.opened with success', function () { - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modalInstance.opened.then).toHaveBeenCalled(); @@ -192,7 +192,7 @@ describe('app.cars', function() { it('should invoke $modalInstance.opened with error', function () { isSuccessCallBack = false; - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modalInstance.opened.then).toHaveBeenCalled(); @@ -201,7 +201,7 @@ describe('app.cars', function() { it('should invoke $modalInstance.rendered with success', function () { - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modalInstance.rendered.then).toHaveBeenCalled(); @@ -210,7 +210,7 @@ describe('app.cars', function() { it('should invoke $modalInstance.rendered with error', function () { isSuccessCallBack = false; - Cars.doModal('lg'); + CarsController.doModal('lg'); expect($modalInstance.rendered.then).toHaveBeenCalled(); @@ -219,7 +219,7 @@ describe('app.cars', function() { it('should invoke $modalInstance.close because of timeout', function () { - Cars.doModal('lg'); + CarsController.doModal('lg'); $timeout.flush(); expect($modalInstance.close).toHaveBeenCalledWith('closed by timeout'); @@ -228,7 +228,7 @@ describe('app.cars', function() { it('should invoke $modalInstance.dismiss because of timeout', function () { - Cars.doModal('lg'); + CarsController.doModal('lg'); $timeout.flush(); expect($modalInstance.dismiss).toHaveBeenCalledWith('dismissed by timeout'); diff --git a/angularjs/showcase/src/showcase/app/cars/cars.html b/angularjs/showcase/src/showcase/app/cars/cars.html index fda95e9..28d2756 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.html +++ b/angularjs/showcase/src/showcase/app/cars/cars.html @@ -1,7 +1,7 @@
-
+ diff --git a/angularjs/showcase/src/showcase/app/welcome/welcome.controller.js b/angularjs/showcase/src/showcase/app/welcome/welcome.controller.js index dacf8b5..bf94b1e 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.controller.js +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.controller.js @@ -3,16 +3,16 @@ angular .module('app.welcome') - .controller('Welcome', Welcome); + .controller('WelcomeController', WelcomeController); // When using
(strict mode) we must always // manually identify dependencies. // Instead I am going to use ngInject because it is cool :) - // Welcome.$inject = ['$location']; + // WelcomeController.$inject = ['$location']; /** * @ngdoc controller - * @name app.welcome.controller:Welcome + * @name app.welcome.controller:WelcomeController * * @requires $location * @@ -22,10 +22,10 @@ *

* * @description - * Welcome controller. + * WelcomeController controller. */ /* @ngInject */ - function Welcome($location) { + function WelcomeController($location) { var vm = this; vm.hello = 'Hello World'; diff --git a/angularjs/showcase/src/showcase/app/welcome/welcome.controller.spec.js b/angularjs/showcase/src/showcase/app/welcome/welcome.controller.spec.js index b86f10d..d45ec79 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.controller.spec.js @@ -1,23 +1,23 @@ describe('app.welcome', function() { 'use strict'; - var controller; - var scope; + var WelcomeController; + var $scope; beforeEach(function() { module('app.welcome'); inject(function($controller, _$rootScope_) { - scope = _$rootScope_.$new(); - scope.hello = 'Hello World'; - controller = $controller('Welcome', {$scope: scope}); + $scope = _$rootScope_.$new(); + $scope.hello = 'Hello World'; + WelcomeController = $controller('WelcomeController', {$scope: $scope}); }); }); - describe('Welcome controller', function () { + describe('WelcomeController controller', function () { it('should be created successfully', function () { - expect(controller).toBeDefined(); + expect(WelcomeController).toBeDefined(); }); }); diff --git a/angularjs/showcase/src/showcase/app/welcome/welcome.html b/angularjs/showcase/src/showcase/app/welcome/welcome.html index b2e2f94..fc49266 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.html +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.html @@ -1,5 +1,5 @@ -
+
{{ vm.hello }} diff --git a/angularjs/showcase/src/showcase/app/widgets/example-controller-as.directive.spec.js b/angularjs/showcase/src/showcase/app/widgets/example-controller-as.directive.spec.js index 74fabb8..7efccf9 100644 --- a/angularjs/showcase/src/showcase/app/widgets/example-controller-as.directive.spec.js +++ b/angularjs/showcase/src/showcase/app/widgets/example-controller-as.directive.spec.js @@ -2,29 +2,29 @@ 'use strict'; describe('exampleControllerAsDirective', function() { - var compile; - var rootScope; + var $compile; + var $rootScope; var template; beforeEach(module('app.widgets')); - beforeEach(inject(function($compile, $rootScope) { - compile = $compile; - rootScope = $rootScope; + beforeEach(inject(function(_$compile_, _$rootScope_) { + $compile = _$compile_; + $rootScope = _$rootScope_; template = ''; })); it('should assign min value 3', function() { - var scope; + var $scope; var element; var vm; - scope = rootScope.$new(); + $scope = $rootScope.$new(); // IN THE REAL WORLD, BETTER DECLARE THE CONTROLLER RELATED TO MY DIRECTIVE IN A DIFFERENT FILE AND // TEST THE CONTROLLER WITH ITS OWN SPEC!!!!! // see: http://stackoverflow.com/a/15314876 - element = compile(template)(scope); - scope.$digest(); + element = $compile(template)($scope); + $scope.$digest(); vm = element.controller('controllerAsDirective');