mercredi 2 septembre 2015

10 $digest() iterations reached error after 13 specs

I'm getting the known error Error: [$rootScope:infdig] 10 $digest() iterations reached. Aborting! But, I'm getting this error after running 13 specs exactly. If I comment ANY of my 14 specs, all test will pass. I'm assuming that everyone is connected but do not know why.

This is the file I'm running...

describe('Resources Service', function() {
  beforeEach(module('app'));
  HttpHelper.decorateHttpBackend();
  var ResourcesSrv, $httpBackend, localStorageService, $q, $rootScope;

  beforeEach(inject(function($injector) {
    ResourcesSrv = $injector.get('ResourcesSrv');
    $httpBackend = $injector.get('$httpBackend');
    localStorageService = $injector.get('localStorageService');
    $q = $injector.get('$q');
    $rootScope = $injector.get('$rootScope');
  }));

  afterEach(function() {
    localStorage.clear();
  });

  describe('#syncResources', function() {
    describe('when sync service fails', function() {
      beforeEach(function() {
        $httpBackend.when('GET', $httpBackend.customUrl('/resources')).respond(500);
        ResourcesSrv.syncResources();
        $httpBackend.flush();
      });

      it('leaves stored oldest resource id clean', function() {
        expect(localStorageService.get('resources.oldestResourceId')).toBeNull();
      });

      it('leaves service resources array empty', function() {
        expect(ResourcesSrv.resources.length).toEqual(0);
      });
    });

    describe('when sync service is working', function() {
      beforeEach(function() {
        $httpBackend.when('GET', $httpBackend.customUrl('/resources')).respond(200, window.testData.getPendingResources());
        ResourcesSrv.syncResources();
        $httpBackend.flush();
      });

      it('stores oldest resource id', function() {
        expect(localStorageService.get('resources.oldestResourceId')).toEqual(1);
      });

      it('fills service resources array', function() {
        expect(ResourcesSrv.resources.length).toEqual(5);
      });

      it('stores resources properly', function() {
        _.forEach(window.testData.getPendingResources(), function(resource) {
          expect(localStorageService.get('resources.resource' + resource.id)).not.toBe(null);
        });
      });

      it('stores formatted resources', function() {
        var resource = window.testData.getPendingResources()[0];
        var storedResource = localStorageService.get('resources.resource' + resource.id);
        expect(resource.id).toEqual(storedResource.id);
        expect(resource.type).toEqual(storedResource.type);
        expect(resource.name).toEqual(storedResource.name);
        expect(resource.progressStatus).toEqual(storedResource.progressStatus);
        expect(resource.createdAt).toEqual(storedResource.createdAt);
        expect(resource.details).toEqual(storedResource.details);
        expect(resource.activities).toEqual(storedResource.activities);
        expect(resource.categories).toEqual(storedResource.categories);
        expect(resource.workers).toEqual(storedResource.workers);
      });

      it('calls service with from_id param', function() {
        var expectedUrl = $httpBackend.customUrl('/resources') + '?from_id=' + window.testData.getPendingResources()[4].id;
        $httpBackend.expect('GET', expectedUrl).respond(200, []);
        ResourcesSrv.syncResources();
        $httpBackend.flush();
      });

      describe('getting completed and aborted resources', function() {
        beforeEach(function() {
          var response = [
            {
              'id':5,
              'progressStatus':'completed',
            },
            {
              'id':4,
              'progressStatus':'aborted',
            },
            {
              'id':3,
              'progressStatus':'pending',
            }
          ];

          var expectedUrl = $httpBackend.customUrl('/resources') + '?from_id=' + window.testData.getPendingResources()[4].id;
          $httpBackend.when('GET', expectedUrl).respond(200, response);
          ResourcesSrv.syncResources();
          $httpBackend.flush();
        });

        it('removes completed/aborted resources from local storage', function() {
          expect(localStorageService.get('resources.resource5')).toBe(null);
          expect(localStorageService.get('resources.resource4')).toBe(null);
          expect(localStorageService.get('resources.resource3')).not.toBe(null);
          expect(localStorageService.get('resources.resource2')).not.toBe(null);
          expect(localStorageService.get('resources.resource1')).not.toBe(null);
        });

        it('removes completed/aborted resources from local array', function() {
          expect(ResourcesSrv.resources.length).toBe(3);
        });
      });
    });
  });

  describe('#isSynced', function() {
    it('returns false with oldestResourceId oldestResourceId', function() {
      expect(ResourcesSrv.isSynced()).toBe(false);
    });

    describe('when sync service is working', function() {
      beforeEach(function() {
        $httpBackend.when('GET', $httpBackend.customUrl('/resources')).respond(200, window.testData.getPendingResources());
        ResourcesSrv.syncResources();
        $httpBackend.flush();
      });

      it('returns true with stored oldestResourceId', function() {
        expect(ResourcesSrv.isSynced()).toBe(true);
      });
    });
  });

  describe('#getResource', function() {
    var resources = window.testData.getPendingResources();

    beforeEach(function() {
      $httpBackend.when('GET', $httpBackend.customUrl('/resources')).respond(200, resources);
      ResourcesSrv.syncResources();
      $httpBackend.flush();
    });

    it('gets stored resource', function() {
      expect(ResourcesSrv.getResource(resources[0].id)).not.toBe(null);
    });

    it('returns nil trying to get non stored resource', function() {
      expect(ResourcesSrv.getResource('some invalid id')).toBe(null);
    });
  });

  describe('#setCurrentResource', function() {
    var storedResource = { not: 'important' };

    beforeEach(function() {
      localStorageService.set('resources.resource1', storedResource);
    });

    it('returns resolved promise passing valid resource id', function() {
      var promise = ResourcesSrv.setCurrentResource(1),
          successHandler = jasmine.createSpy();

      promise.then(successHandler);
      $rootScope.$apply();
      expect(successHandler).toHaveBeenCalledWith(storedResource);
    });
  });
});

Aucun commentaire:

Enregistrer un commentaire