Below is my service.

'use strict';

angular.module('scroll-resize-listener', [])

.factory('scrollAndResizeListener',
  function ($rootScope, $window, $document, $timeout) {
    var id = 0;
    var listeners = {};
    var scrollTimeoutId;
    var resizeTimeoutId;

    function invokeListeners() {
      var clientHeight = $document[0].documentElement.clientHeight;
      var clientWidth = $document[0].documentElement.clientWidth;

      for (var key in listeners) {
        if (listeners.hasOwnProperty(key)) {
          listeners[key](clientHeight, clientWidth); // call listener with 
given arguments
        }
      }
    }

    $window.addEventListener('scroll', function () {
      $timeout.cancel(scrollTimeoutId);
      scrollTimeoutId = $timeout(invokeListeners, 50);
    });

    $window.addEventListener('resize', function () {
      $timeout.cancel(resizeTimeoutId);
      resizeTimeoutId = $timeout(invokeListeners, 50);
    });

    return {
      invoke: function () {
        $timeout.cancel(resizeTimeoutId);
        resizeTimeoutId = $timeout(invokeListeners, 50);
      },

      bindListener: function (listener) {
        var index = ++id;

        listeners[id] = listener;

        return function () {
          delete listeners[index];
        };
      }
    };
  }
);




And this is my test for this service.

'use strict';

describe('Scroll And Resize Listener', function () {
  
  var service;
  var $rootScopeMock;
  var $windowMock;
  var $documentMock;

  beforeEach(module('uniqlo.scroll-resize-listener'));
  beforeEach(module(initDependencies));
  beforeEach(inject(initService));

  it('it should register for a event listener', function () {
      function onScroll(){
      
    } 
    var listenerRemover = service.bindListener(onScroll);
    expect(service.bindListener(onScroll)).to.be.a('Function');
    expect(service.invoke).to.be.a('Function');
  });

  function initDependencies ($provide) {
    $provide.factory('$rootScope', function () {
      return {
        '$evalAsync': function (fn) {
          fn();
        }
      };
    });
    $provide.factory('$window', function () {
      return $windowMock;
    });
    $provide.factory('$document', function () {
      return {
        '$evalAsync': function (fn) {
          fn();
        }
      };
    });

    $windowMock = {
      addEventListener: function (event){
        return 
      }
    }
  }

  function initService (_scrollAndResizeListener_) {
    service = _scrollAndResizeListener_;
    // service = {
    //   invoke: sinon.spy(function () {
    //     return function invokeListener(){

    //     }
    //   })
    // }
  }
});





-- 
You received this message because you are subscribed to the Google Groups 
"AngularJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to