From 1d209b115200e563462ceb73a3cf7958247b445f Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Sun, 30 Aug 2015 03:23:46 +0200 Subject: [PATCH] showcase: app.cars, testing $modal and promises --- .../app/cars/cars-error-modal.controller.js | 4 +- .../src/showcase/app/cars/cars.controller.js | 18 ++++--- .../src/showcase/app/cars/cars.controller.spec.js | 59 ++++++++++++++++++---- 3 files changed, 61 insertions(+), 20 deletions(-) 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 c0fd641..2f6456a 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 @@ -32,13 +32,13 @@ vm.ok = function () { var isAllowedEvent = $modalInstance.close(vm.selected.car); console.log('close: broadcasted event to the modal scope before the modal closes. ' + - 'Was it allowed?' + isAllowedEvent); + 'Was it allowed? ' + isAllowedEvent); }; vm.cancel = function () { var isAllowedEvent = $modalInstance.dismiss('cancel'); console.log('dismiss: broadcasted event to the modal scope before the modal closes. ' + - 'Was it allowed?' + isAllowedEvent); + 'Was it allowed? ' + isAllowedEvent); }; } diff --git a/angularjs/showcase/src/showcase/app/cars/cars.controller.js b/angularjs/showcase/src/showcase/app/cars/cars.controller.js index def4d7d..7cc5793 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.js @@ -25,14 +25,13 @@ /* @ngInject */ function Cars($modal, $timeout, cars) { var vm = this; + vm.example = { text: 'try to send data', word: /^\s*\w*\s*$/, singleModel: 1 }; - vm.getCars = getCars; - - function getCars() { + vm.getCars = function () { // ES6 way. success and error are deprecated because they are not following the ES6 way. cars.getAll().then( // Success @@ -42,12 +41,17 @@ // Error function(reason) { console.log('Cars controller error: ' + reason); - doModal('lg'); + vm.doModal('lg'); } ); - } + }; - function doModal(size) { + // How to test "private" methods in controllers? + // Two options: + // a) Extracting the logic of the "private" method to some service. The service could be + // called ModalService and it could be used by any module. Does that ring a bell? :D + // b) The one I am using here. Attaching to vm/this because AngularJS is performing new MyController(). + vm.doModal = function (size) { var cars = ['car1', 'car2', 'car3']; var modalInstance = $modal.open({ animation: true, @@ -98,7 +102,7 @@ console.log('dismissed by tiemout at: ' + new Date()); modalInstance.dismiss('dismissed by tiemout'); }, 10000); - } + }; } })(); 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 d04427b..f307cf0 100644 --- a/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js +++ b/angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js @@ -1,13 +1,16 @@ describe('app.cars', function() { 'use strict'; - var Cars; + var isSuccessCallBack = true; + var onFulfilledValue = 'car1'; + var onRejectedValue = 'error'; // Why the heck do I need this stupid object if it is going to be spied by means of Jasmine? var cars = { getAll: function() { return {}; } }; + var Cars; var $q; beforeEach(function() { @@ -25,17 +28,25 @@ describe('app.cars', function() { describe('Cars controller', function () { - it('should invoke GET all cars in service: old fashionable way', function () { + it('should invoke GET all cars in service with success: old fashionable way', function () { spyOn(cars, 'getAll') .and.callFake(function() { - var deferred = $q.defer(); - return deferred.promise; + return { + then: function (successCallback, errorCallback) { + if (isSuccessCallBack) { + successCallback(onFulfilledValue); + } else { + errorCallback(onRejectedValue); + } + } + }; }); Cars.getCars(); expect(cars.getAll).toHaveBeenCalled(); + expect(Cars.cars).toEqual(onFulfilledValue); }); }); @@ -44,15 +55,25 @@ describe('app.cars', function() { describe('app.cars', function() { 'use strict'; - var Cars; + var isSuccessCallBack = true; + var onFulfilledValue = 'car1'; + var onRejectedValue = 'error'; // With object already implementing the required spy :) var cars = { - getAll: jasmine.createSpy('cars.getAll').and.callFake(function() { - return $q(function(resolve) { - resolve(); - }); - }) + getAll: jasmine.createSpy('cars.getAll') + .and.callFake(function() { + return { + then: function (successCallback, errorCallback) { + if (isSuccessCallBack) { + successCallback(onFulfilledValue); + } else { + errorCallback(onRejectedValue); + } + } + }; + }) }; + var Cars; var $q; beforeEach(function() { @@ -70,12 +91,28 @@ describe('app.cars', function() { describe('Cars controller', function () { - it('should invoke GET all cars in service: alternative way', function () { + it('should invoke GET all cars in service with success: alternative way', function () { Cars.getCars(); expect(cars.getAll).toHaveBeenCalled(); + expect(Cars.cars).toEqual(onFulfilledValue); }); + + it('should invoke GET all cars in service with error: alternative way', function () { + + isSuccessCallBack = false; + spyOn(Cars, 'doModal') + .and.callFake(function() { + return {}; + }); + + Cars.getCars(); + + expect(cars.getAll).toHaveBeenCalled(); + expect(Cars.doModal).toHaveBeenCalled(); + }); + }); }); -- 2.1.4