showcase: testing Cars controller, mocking $modal and $modalInstance
authorGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 30 Aug 2015 17:46:30 +0000 (19:46 +0200)
committerGustavo Martin Morcuende <gu.martinm@gmail.com>
Sun, 30 Aug 2015 17:46:30 +0000 (19:46 +0200)
angularjs/showcase/src/showcase/app/cars/cars.controller.js
angularjs/showcase/src/showcase/app/cars/cars.controller.spec.js

index 7cc5793..1f47e58 100644 (file)
@@ -53,6 +53,7 @@
     // 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'];
+      // The modalInstance object will be seen from here and it will also be injected in CarsErrorModal controller
       var modalInstance = $modal.open({
         animation: true,
         templateUrl: 'app/cars/cars-error-modal.html',
       });
 
       modalInstance.result.then(function (selectedItem) {
+        // following this path after calling modalInstance.close from either this controller or
+        // CarsErrorModal controller.
         vm.selected = selectedItem;
       }, function (reason) {
+        // following this path after calling modalInstance.dismiss from either this controller or
+        // CarsErrorModal controller.
         if (reason === '$uibUnscheduledDestruction') {
           console.log('Modal\'s scope destroyed by unexpected mechanism');
         }
       });
 
       $timeout(function() {
-        console.log('closed by tiemout at: ' + new Date());
-        modalInstance.close('closed by tiemout');
+        console.log('closed by timeout at: ' + new Date());
+        modalInstance.close('closed by timeout');
       }, 5000);
 
       $timeout(function() {
-        console.log('dismissed by tiemout at: ' + new Date());
-        modalInstance.dismiss('dismissed by tiemout');
+        console.log('dismissed by timeout at: ' + new Date());
+        modalInstance.dismiss('dismissed by timeout');
       }, 10000);
     };
   }
index f307cf0..b4f2030 100644 (file)
@@ -73,19 +73,61 @@ describe('app.cars', function() {
         };
       })
   };
+  var $modalInstance = {
+    close: jasmine.createSpy('$modalInstance.close'),
+    dismiss: jasmine.createSpy('$modalInstance.dismiss'),
+    result: {
+      then: jasmine.createSpy('$modalInstance.result.then')
+        .and.callFake(function(successCallback, errorCallback) {
+          if (isSuccessCallBack) {
+            successCallback(onFulfilledValue);
+          } else {
+            errorCallback(onRejectedValue);
+          }
+        })
+    },
+    opened: {
+      then: jasmine.createSpy('$modalInstance.opened.then')
+        .and.callFake(function(successCallback, errorCallback) {
+          if (isSuccessCallBack) {
+            successCallback(onFulfilledValue);
+          } else {
+            errorCallback(onRejectedValue);
+          }
+        })
+    },
+    rendered: {
+      then: jasmine.createSpy('$modalInstance.rendered.then')
+        .and.callFake(function(successCallback, errorCallback) {
+          if (isSuccessCallBack) {
+            successCallback(onFulfilledValue);
+          } else {
+            errorCallback(onRejectedValue);
+          }
+        })
+    }
+  };
+  var $modal = {
+    open: jasmine.createSpy('$modal.open')
+      .and.callFake(function() {
+        return $modalInstance;
+      })
+  };
+  var $timeout;
   var Cars;
   var $q;
 
   beforeEach(function() {
     module('app.cars');
 
-    inject(function($controller, $modal, $timeout, _$q_) {
+    inject(function($controller, _$timeout_, _$q_) {
       Cars = $controller('Cars', {
         $modal: $modal,
-        $timeout: $timeout,
+        $timeout: _$timeout_,
         cars: cars
       });
       $q = _$q_;
+      $timeout = _$timeout_;
     });
   });
 
@@ -100,7 +142,6 @@ describe('app.cars', function() {
     });
 
     it('should invoke GET all cars in service with error: alternative way', function () {
-
       isSuccessCallBack = false;
       spyOn(Cars, 'doModal')
         .and.callFake(function() {
@@ -111,6 +152,87 @@ describe('app.cars', function() {
 
       expect(cars.getAll).toHaveBeenCalled();
       expect(Cars.doModal).toHaveBeenCalled();
+
+      isSuccessCallBack = true;
+    });
+
+    it('should invoke $modal.open', function () {
+
+      Cars.doModal('lg');
+
+      expect($modal.open).toHaveBeenCalled();
+    });
+
+    it('should invoke $modalInstance.result with success', function () {
+
+      Cars.doModal('lg');
+
+      expect($modalInstance.result.then).toHaveBeenCalled();
+      expect(Cars.selected).toEqual(onFulfilledValue);
+    });
+
+    it('should invoke $modalInstance.result with error', function () {
+      isSuccessCallBack = false;
+
+      Cars.doModal('lg');
+
+      expect($modalInstance.result.then).toHaveBeenCalled();
+
+      isSuccessCallBack = true;
+    });
+
+    it('should invoke $modalInstance.opened with success', function () {
+
+      Cars.doModal('lg');
+
+      expect($modalInstance.opened.then).toHaveBeenCalled();
+
+    });
+
+    it('should invoke $modalInstance.opened with error', function () {
+      isSuccessCallBack = false;
+
+      Cars.doModal('lg');
+
+      expect($modalInstance.opened.then).toHaveBeenCalled();
+
+      isSuccessCallBack = true;
+    });
+
+    it('should invoke $modalInstance.rendered with success', function () {
+
+      Cars.doModal('lg');
+
+      expect($modalInstance.rendered.then).toHaveBeenCalled();
+
+    });
+
+    it('should invoke $modalInstance.rendered with error', function () {
+      isSuccessCallBack = false;
+
+      Cars.doModal('lg');
+
+      expect($modalInstance.rendered.then).toHaveBeenCalled();
+
+      isSuccessCallBack = true;
+    });
+
+    it('should invoke $modalInstance.close because of timeout', function () {
+
+      Cars.doModal('lg');
+      $timeout.flush();
+
+      expect($modalInstance.close).toHaveBeenCalledWith('closed by timeout');
+
+    });
+
+    it('should invoke $modalInstance.dismiss because of timeout', function () {
+
+      Cars.doModal('lg');
+      $timeout.flush();
+
+      expect($modalInstance.dismiss).toHaveBeenCalledWith('dismissed by timeout');
+
     });
 
   });