Add tests for CaseTypeListCtrl's deleteCaseType method
authorRené Olivo <minet.ws@gmail.com>
Mon, 23 Apr 2018 23:54:34 +0000 (19:54 -0400)
committerRené Olivo <minet.ws@gmail.com>
Mon, 23 Apr 2018 23:54:34 +0000 (19:54 -0400)
ang/crmCaseType.js
tests/karma/unit/crmCaseTypeSpec.js

index e58f37194372ebd0a0c0da7f7265b03980ac5a1b..2ae4b98b3f12c5bc57674516aca855e18972a463 100644 (file)
       })
         .then(function (data) {
           delete caseTypes.values[caseType.id];
-          $scope.$digest();
         });
     };
     $scope.revertCaseType = function (caseType) {
index 24004d4ea469ab4cf0bf275dce3fbc14593a4309..e6c8aa944f0218b1c467d53b331f1794257c6378 100644 (file)
@@ -347,5 +347,49 @@ describe('crmCaseType', function() {
         });
       });
     });
+
+    describe('deleteCaseType', function() {
+      var caseType = { id: _.uniqueId() };
+
+      beforeEach(function() {
+        crmApiSpy.and.returnValue($q.resolve(caseType));
+        scope.caseTypes[caseType.id] = caseType;
+
+        scope.deleteCaseType(caseType);
+        scope.$digest();
+      });
+
+      describe('when the case type can be deleted', function() {
+        it('deletes the case from the api', function() {
+          expect(crmApiSpy).toHaveBeenCalledWith('CaseType', 'delete', { id: caseType.id }, jasmine.any(Object));
+        });
+
+        it('removes the case type from the list', function() {
+          expect(scope.caseTypes[caseType.id]).toBeUndefined();
+        });
+      });
+
+      describe('when the case type cannot be delted', function() {
+        var error = { error_message: 'Error Message' };
+
+        beforeEach(function() {
+          var errorHandler;
+
+          crmApiSpy.and.returnValue($q.reject(error));
+          scope.caseTypes[caseType.id] = caseType;
+
+          spyOn(CRM, 'alert');
+          scope.deleteCaseType(caseType);
+          scope.$digest();
+
+          errorHandler = crmApiSpy.calls.mostRecent().args[3].error;
+          errorHandler(error);
+        });
+
+        it('displays the error message', function() {
+          expect(CRM.alert).toHaveBeenCalledWith(error.error_message, ts('Error'), 'error');
+        });
+      });
+    });
   });
 });