简体   繁体   English

模拟jquery选择器并返回html元素

[英]Mocking jquery selector and returning an html element

I am trying to test a javascript function that requires that a certain element be present in the DOM. 我正在尝试测试一个javascript函数,该函数要求DOM中存在某个元素。 Below is the function that I am trying to test: 下面是我要测试的功能:

auction.save_scroll_state = function() {
    var user_agent = $window.navigator.userAgent.toLowerCase();
    var is_android = user_agent.indexOf("android") > -1;

    if (is_android) {
      var restore_pos = null;
      debugger
      if ($location.search().section == "items") {
        restore_pos = $('#all-items').scrollTop();
        $('#all-items').one('scroll', function(){$('#all-items').scrollTo(restore_pos)});
      }
      else if ($location.search().section == "user_items"){
        restore_pos = $('#your-items').scrollTop();
        $('#your-items').one('scroll', function(){$('#your-items').scrollTo(restore_pos)});
      }
    }
  };

When I run the test and get to the debugger any type $('#all-items').length it returns 0 indicating that the element cannot be found. 当我运行测试并到达调试器时,任何类型的$('#all-items').length返回0指示找不到元素。 I am trying to form some kind of Jasime spy that will mock out the jquery selector. 我试图形成某种Jasime间谍,它将模仿jquery选择器。 My test is below: 我的测试如下:

it ('save_scroll_state android user_agent all-items', inject(function($bwAppState, $location, $window) {
  $window.navigator = {userAgent: "Mozilla/5.0 (Linux; Android 7.0; SM-G950F Build/NRD90M) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.83 Mobile Safari/537.36"};
  var items_div = $("<div id='all-items'></div>");
  var jq_div_spy = spyOn($.fn, 'html' ).and.returnValue(items_div);
  var items_div_spy = spyOn(items_div, 'scrollTop');
  spyOn($location, "search").and.returnValue({section: 'items'});
  $bwAppState.auction.save_scroll_state();
  expect(items_div_spy).toHaveBeenCalled();
  delete $window.nagivator;
}));

The main goal is to make sure that scrollTop() gets called on some HTML element that has and id tag of id="all-items" . 主要目标是确保在具有id标签为id="all-items"某些HTML元素上调用scrollTop()

Any help would be much appreciated 任何帮助将非常感激

Not ideally, but could do as: 不理想,但可以这样做:

describe('ScrollTop on .test', function(){

    var scrollTopSpy;

    beforeEach(function(){
        scrollTopSpy = jasmine.createSpyObj('$-spy', ['scrollTop']); 
        spyOn(window, '$').and.callFake(function(selector){
            if(selector == '.test') {
                return scrollTopSpy;
            }
            else {
                return $(selector);
            }
        });
    });

    it('is called', function(){
        $('.test').scrollTop();
        expect($('.test').scrollTop).toHaveBeenCalled();
    });

    it('is not called', function(){
        expect($('.test').scrollTop).not.toHaveBeenCalled();
    })
})

Please not that only call of scrollTop on .test is performed, test is ignorant of real .test element existence in DOM. 请不仅执行对.testscrollTop调用,而且测试不知道DOM中存在真正的.test元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM