From 318dac06ab7d4ce6ff338608e870e33b726d2b9c Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Wed, 16 Sep 2015 02:58:58 +0200 Subject: [PATCH] showcase: broadcast, emit, on, rootScope, scope Unit Tests --- .../app/cars/cars-error-modal.controller.spec.js | 2 +- .../src/showcase/app/cars/cars.controller.spec.js | 4 +- .../showcase/app/users/users-child.controller.js | 13 +++-- .../app/users/users-child.controller.spec.js | 59 ++++++++++++++++++++++ .../src/showcase/app/users/users.controller.js | 9 ++-- .../showcase/app/users/users.controller.spec.js | 18 ++++++- .../app/welcome/welcome.controller.spec.js | 2 +- 7 files changed, 89 insertions(+), 18 deletions(-) create mode 100644 angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js 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 bf72b5d..9ec61e0 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 @@ -20,7 +20,7 @@ describe('app.cars', function() { }); - describe('CarsErrorModalController controller', function () { + describe('CarsErrorModalController', function () { it('should invoke $modalInstance.close', function () { CarsErrorModalController.ok(); 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 81f1924..29130bd 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js @@ -26,7 +26,7 @@ describe('app.cars', function() { }); }); - describe('Cars controller', function () { + describe('CarsController', function () { it('should invoke GET all cars in service with success: old fashionable way', function () { @@ -131,7 +131,7 @@ describe('app.cars', function() { }); }); - describe('Cars controller', function () { + describe('CarsController', function () { it('should invoke GET all cars in service with success: alternative way', function () { diff --git a/angularjs/showcase/src/showcase/app/users/users-child.controller.js b/angularjs/showcase/src/showcase/app/users/users-child.controller.js index c7c4659..f866e2f 100644 --- a/angularjs/showcase/src/showcase/app/users/users-child.controller.js +++ b/angularjs/showcase/src/showcase/app/users/users-child.controller.js @@ -17,7 +17,7 @@ *

* * @description - * Users controller. + * UsersChildController controller. */ /* @ngInject */ function UsersChildController($rootScope, $scope, USERS) { @@ -30,6 +30,10 @@ vm.getEmit = function () { $scope.$emit(USERS.SCOPE.EMIT_FACT, emitFact); }; + vm.usersChildOnScopeBroadcast = function (events, broadcastUser) { + vm.broadcastUser = broadcastUser; + console.log('usersChildOnScopeBroadcast, events.name: ' + events.name); + }; // NEVER USE $rootScope.$on IN CONTROLLER BECAUSE IT IS NOT DESTROYED EVEN IF CONTROLLER WAS DESTROYED!!! // YOU WILL END UP HAVING AS MANY EVENT LISTENERS AS TIMES THIS CONTROLLER IS CREATED!!!! @@ -37,17 +41,12 @@ // LISTENING FOR EVENTS IN $scope IS THE RIGHT THING BECAUSE THESE EVENT LISTENERS ARE DESTROYED // AT THE SAME TIME AS THIS CONTROLLER :) - $scope.$on(USERS.ROOTSCOPE.BROADCAST, usersChildOnScopeBroadcast); + $scope.$on(USERS.ROOTSCOPE.BROADCAST, vm.usersChildOnScopeBroadcast); // function usersChildOnRootBroadcast(events, broadcastUser) { // vm.broadcastUser = broadcastUser; // console.log('usersChildOnRootBroadcast, events.name: ' + events.name); // } - function usersChildOnScopeBroadcast(events, broadcastUser) { - vm.broadcastUser = broadcastUser; - console.log('usersChildOnScopeBroadcast, events.name: ' + events.name); - } - } })(); diff --git a/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js b/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js new file mode 100644 index 0000000..d534ec4 --- /dev/null +++ b/angularjs/showcase/src/showcase/app/users/users-child.controller.spec.js @@ -0,0 +1,59 @@ +describe('app.users', function() { + 'use strict'; + + var $rootScope; + var $scope; + var USERS; + var UsersChildController; + + beforeEach(function() { + module('app.users'); + + inject(function($controller, _$rootScope_, _USERS_) { + $rootScope = _$rootScope_; + USERS = _USERS_; + $scope = $rootScope.$new(); + + spyOn($scope, '$emit'); + UsersChildController = $controller('UsersChildController', { + $rootScope: $rootScope, + $scope: $scope, + USERS: _USERS_ + }); + }); + }); + + describe('UsersChildController', function () { + + it('should be created successfully', function () { + expect(UsersChildController).toBeDefined(); + }); + + it('should be called $scope.$emit', function () { + var emitFact = { + title: 'Snake and Scarlett', + fact: 'it is canon' + }; + + UsersChildController.getEmit(); + + expect($scope.$emit).toHaveBeenCalledWith(USERS.SCOPE.EMIT_FACT, emitFact); + }); + + it('should be assigned broadcastUser', function () { + var rootScopeBroadcastUser = { + name: 'Snake', + lastName: 'Eyes', + city: 'classified' + }; + var event = { + name: 'USERS.ROOTSCOPE.BROADCAST' + }; + + UsersChildController.usersChildOnScopeBroadcast(event, rootScopeBroadcastUser); + + expect(UsersChildController.broadcastUser).toEqual(rootScopeBroadcastUser); + }); + }); + +}); diff --git a/angularjs/showcase/src/showcase/app/users/users.controller.js b/angularjs/showcase/src/showcase/app/users/users.controller.js index 280c616..71b7628 100644 --- a/angularjs/showcase/src/showcase/app/users/users.controller.js +++ b/angularjs/showcase/src/showcase/app/users/users.controller.js @@ -39,13 +39,12 @@ vm.getScopeBroadcast = function () { $scope.$broadcast(USERS.ROOTSCOPE.BROADCAST, scopeBroadcastUser); }; - - $scope.$on(USERS.SCOPE.EMIT_FACT, usersOnEmitFact); - - function usersOnEmitFact(events, emitFact) { + vm.usersOnEmitFact = function (events, emitFact) { vm.emitFact = emitFact; console.log('usersOnEmitFact, events.name: ' + events.name); - } + }; + + $scope.$on(USERS.SCOPE.EMIT_FACT, vm.usersOnEmitFact); } })(); diff --git a/angularjs/showcase/src/showcase/app/users/users.controller.spec.js b/angularjs/showcase/src/showcase/app/users/users.controller.spec.js index 971701b..6919a11 100644 --- a/angularjs/showcase/src/showcase/app/users/users.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/users/users.controller.spec.js @@ -14,7 +14,7 @@ describe('app.users', function() { USERS = _USERS_; $scope = $rootScope.$new(); - jasmine.createSpy($rootScope, '$rootScope.$broadcast'); + spyOn($rootScope, '$broadcast'); jasmine.createSpy($scope, '$scope.$broadcast'); UsersController = $controller('UsersController', { $rootScope: $rootScope, @@ -24,7 +24,7 @@ describe('app.users', function() { }); }); - describe('UsersController controller', function () { + describe('UsersController', function () { it('should be created successfully', function () { expect(UsersController).toBeDefined(); @@ -53,6 +53,20 @@ describe('app.users', function() { expect($scope.$broadcast).toHaveBeenCalledWith(USERS.ROOTSCOPE.BROADCAST, scopeBroadcastUser); }); + + it('should be assigned emitFact', function () { + var emitFact = { + title: 'Snake and Scarlett', + fact: 'it is canon' + }; + var event = { + name: 'USERS_SCOPE_EMIT_FACT' + }; + + UsersController.usersOnEmitFact(event, emitFact); + + expect(UsersController.emitFact).toEqual(emitFact); + }); }); }); 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 d45ec79..f6be9eb 100644 --- a/angularjs/showcase/src/showcase/app/welcome/welcome.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/welcome/welcome.controller.spec.js @@ -14,7 +14,7 @@ describe('app.welcome', function() { }); }); - describe('WelcomeController controller', function () { + describe('WelcomeController', function () { it('should be created successfully', function () { expect(WelcomeController).toBeDefined(); -- 2.1.4