jeudi 28 janvier 2016

Jasmine tests with Angular and ui bootstrap Modal keep failing. Why are my spy's not being called?

I am getting these three errors below when running my jasmine tests:

PhantomJS 1.9.8 (Linux 0.0.0) confirmOnExit directive record is dirty unsaved changes modal should call record.$save and call hideTab when saveChanges is called FAILED
Expected spy $save to have been called.

PhantomJS 1.9.8 (Linux 0.0.0) confirmOnExit directive record is dirty unsaved changes modal should call record.revert() and call hideTab when discardChanges is called FAILED
Expected spy revert to have been called.

PhantomJS 1.9.8 (Linux 0.0.0) confirmOnExit directive record is dirty unsaved changes modal should not save, call cancelHide, and close the modal when cancel is called FAILED
Expected 4 to be 3.

The functionality of the app works as it's supposed to but these three tests keep failing. Does anyone know why the spy's aren't being called? The test code is below:

    /* globals jasmine */

    describe('confirmOnExit directive', function() {
      var $compile, $rootScope, scope, compiledElement, directiveCtrl,
        UnsavedChangesModalSvc, state, $timeout, $document, modal,
        modalScope, modalCtrl, OnBeforeUnload;

      beforeEach(angular.mock.module('arcContentForms', function($provide) {
          $provide.value('OnBeforeUnload', {
            register: function() {}
          });
        }));
      beforeEach(angular.mock.module('ui.bootstrap'));
      beforeEach(angular.mock.module('arcAlerts'));

      var fakeTab = {
        id: '1234',
        hideTab: function() {},
        cancelHide: function() {}
      };



      function findButtonWithText(text) {
        var buttons = angular.element($document).find('button');
        for (var i = 0; i < buttons.length; i++) {
          var re = new RegExp(text, 'g');
          if (angular.element(buttons[i]).html().match(re)) {
            return angular.element(buttons[i]);
          }
        }
        return [];
      }

      function findElementByClass(element, cls) {
        var buttons = angular.element($document).find(element);
        var modal = [];
        for (var i = 0; i < buttons.length; i++) {
          if (buttons[i].classList.contains(cls)) {
            modal = angular.element(buttons[i]);
          }
        }
        return modal;
      }

      function findNumberOfModals() {
        var buttons = angular.element($document).find('div');
        var modal = 0;
        for (var i = 0; i < buttons.length; i++) {
          if (buttons[i].classList.contains('modal')) {
            modal++;
          }
        }
        return modal;
      }


      beforeEach(inject(function(_$compile_, _$rootScope_, _OnBeforeUnload_, _UnsavedChangesModalSvc_,
              _$state_, _$window_, $q, _$timeout_, _$document_) {
        $document = _$document_;
        $timeout = _$timeout_;
        state = _$state_;
        OnBeforeUnload = _OnBeforeUnload_;
        spyOn(OnBeforeUnload, 'register');

        UnsavedChangesModalSvc = _UnsavedChangesModalSvc_;
        $compile = _$compile_;
        $rootScope = _$rootScope_;
        scope = $rootScope.$new();
        scope.record = {
          _status: "",
          $save: function() {
            var deferred = $q.defer();
            $timeout(function() {
              deferred.resolve({});
            });
            return deferred.promise;
          },
          revert: function() {
            // changes everything back
          }

        };

        scope.tabId="1234";

        spyOn(UnsavedChangesModalSvc, 'createModal').and.callThrough();
        spyOn(scope.record, '$save').and.callThrough();

        fakeTab = {
          id: '1234',
          hideTab: function() {},
          cancelHide: function() {}
        };
        spyOn(fakeTab, "hideTab");
        spyOn(fakeTab, "cancelHide");

        compiledElement = $compile('<form name="testForm" class="form-horizontal" arc-confirm-on-exit="record" arc-confirm-on-exit-tab-id="tabId"></form>')(scope);
        scope.$digest();
      }));


      it('should compile', function() {
        expect(compiledElement).toBeDefined();
      });

      it('should register clearSessionStorage with the onBeforeUnload service', function() {
        expect(OnBeforeUnload.register).toHaveBeenCalled();
      });

      describe('record is dirty', function() {
        beforeEach(function() {
          scope.record.isDirty = function() {
            return true;
          };
        });

        it('should call createModal when state is changed', function() {
          $rootScope.$broadcast("arc::tab::notifyClose",  fakeTab);
          scope.$digest();
          expect(UnsavedChangesModalSvc.createModal).toHaveBeenCalled();
        });

        describe('unsaved changes modal', function() {
          beforeEach(function() {
            $rootScope.$broadcast("arc::tab::notifyClose", fakeTab);
            scope.$digest();
            modal = findElementByClass('div', 'modal');
            modalScope = modal.scope();
            modalCtrl = modalScope.UnsavedChangesCtrl;
          });

          it('should call record.$save and call hideTab when saveChanges is called', function() {
            modalCtrl.saveChanges();
            $timeout.flush();
            expect(scope.record.$save).toHaveBeenCalled();
            expect(fakeTab.hideTab).toHaveBeenCalled();
          });

          it('should call record.revert() and call hideTab when discardChanges is called', function() {
            spyOn(scope.record, 'revert');
            modalCtrl.discardChanges();
            $timeout.flush();
            expect(scope.record.revert).toHaveBeenCalled();
            expect(fakeTab.hideTab).toHaveBeenCalled();
          });

          it('should not save, call cancelHide, and close the modal when cancel is called', function() {
            var numModals = findNumberOfModals();
            spyOn(scope.record, 'revert');
            modalCtrl.cancel();
            $timeout.flush();
            expect(findNumberOfModals()).toBe(numModals-1);
            expect(scope.record.revert).not.toHaveBeenCalled();
            expect(fakeTab.hideTab).not.toHaveBeenCalled();
            expect(fakeTab.cancelHide).toHaveBeenCalled();
          });
        });
      });

      describe('record is clean', function() {
        beforeEach(function() {
          scope.record.isDirty = function() {
            return false;
          };
        });

        it('should call not createModal when state is changed', function() {
          $rootScope.$broadcast("arc::tab::notifyClose", fakeTab);
          scope.$digest();
          expect(UnsavedChangesModalSvc.createModal).not.toHaveBeenCalled();
        });
      });

      describe('different tab', function() {
        beforeEach(function() {
          scope.record.isDirty = function() {
            return true;
          };
        });


        it('should call not createModal when state is changed', function() {
          $rootScope.$broadcast("arc::tab::notifyClose", {id: 'not1234'});
          spyOn(scope.record, 'isDirty');
          scope.$digest();
          expect(scope.record.isDirty).not.toHaveBeenCalled();
        });
      });
    });

Aucun commentaire:

Enregistrer un commentaire