title: How to simulate window scrolling in a Angular Unit Test?
body: <p>How can you simulate or mock window scrolling in a Jasmine Unit Test and or set a window.pageYOffset property?</p>
  
  <p>I am using Angular 1.3, Jasmine 2.1 + Karma 0.12.28 and PhantomJS 1.9.12</p>
  
  <p>This is my directive:</p>
  
  <pre><code>'use strict';
  (function () {
  angular
    .module('myApp')
    .directive('scrollNews', scrollNews);
  
    function scrollNews(){
  
      var directive = {
        restrict: 'A',
        scope: false,
        link: link
      };
  
      return directive;
  
      function link(scope, element) {
        scope.limit = 2;
        //add one to the limit
        scope.loadMore = function() {
          scope.limit += 1;
        };
  
        var raw = element[0];
        angular.element(window).on('scroll', function () {
          var scrollFrontier = this.pageYOffset + 800;
          // when scrollFrontier has reached raw.scrollHeight, run loadMore()
          if (scrollFrontier &gt;= raw.scrollHeight) {
            scope.loadMore(); // run the function
            scope.$digest(); // update the HTML
          }
        });
      }
    }
  })();
  </code></pre>
  
  <p>And this is my testSpec:</p>
  
  <pre><code>describe('Directive: scroll news', function(){
  
    var element, $scope, $window;
  
    beforeEach(module('myApp'));
  
    beforeEach(inject(function($compile, $rootScope, _$window_){
      $scope = $rootScope;
      $window = _$window_;
  
      element = angular.element('&lt;div scroll-news&gt;&lt;/div&gt;');
      $compile(element)($scope);
      $scope.$digest();
    }));
  
    it('should have a limit of 2 when no scrolling have occurred', function(){
      expect($scope.limit).toBe(2);
    });
  
    it('should add one to the limit with loadMore function', function(){
      $scope.loadMore();
      expect($scope.limit).toBe(3);
    });
  
    it('should run loadMore() when scrolling has reached a specific height', function(){
      element.scrollHeight = 1400;
  
      var spy = spyOn($window, 'scrollTo');
      $window.scrollTo(0, 1400);
      expect(spy).toHaveBeenCalled();
  
     expect($scope.limit).toBe(3); // Logs: Expected 2 to be 3. - so loadMore() have not called
   });
  });
  </code></pre>
  
  <p>I want to simulate or mock a window scroll event to the element.scrollHeight, because if these two reaches the same height my directive runs the loadMore() function. But how can I make unit test of that?</p>
  
  <p>Btw the directive works fine in production :)</p>