编程语言
首页 > 编程语言> > javascript – 如何测试在ember-qunit中调用Ember.run.debounce的函数?

javascript – 如何测试在ember-qunit中调用Ember.run.debounce的函数?

作者:互联网

我想测试的控制器包含以下内容:

filterText: '',
filteredFoos: (Ember.A()),

filterFoosImpl: function() {
    console.log('filterFoos begin' );
    var filterText = this.get('filterText');
    var filteredFoos = this.forEach(function(foo) {
        return (foo.get(name).indexOf(filterText) >= 0);
    });
    this.set('filteredFoos', filteredFoos);
},

filterFoos: function() {
    Ember.run.debounce(this.filterFoosImpl.bind(this), 300);
}.observes('model', 'filterText'),

现在我想编写一个测试,声明我在设置filterText时更新了filteredFoos.

要正确地做到这一点,我需要考虑Ember.run.debounce,
在我执行断言之前等待它发生.
我怎样才能做到这一点?

解决方法:

我也遇到了这个问题,为了消除去抖,我做了以下事情:

test('it triggers external action on a keyup event', function() {
    expect(1);

    // stub out the debounce method so we can treat this call syncronously
    Ember.run.debounce = function(target, func) {
      func.call(target);
    };

    var component = this.subject();
    var $component = this.append();

    var targetObject = {
      externalAction: function() {
        ok(true, 'external action called');
      }
    };

    component.set('keyUpAction', 'externalAction');

    component.set('targetObject', targetObject);

    $component.keyup();
});

我测试的组件看起来像这样:

export default Ember.TextField.extend({    
  triggerKeyUpAction: function() {
    this.sendAction('keyUpAction', event);
  },

  keyUp: function(/*event*/) {
    Ember.run.debounce(this, this.triggerKeyUpAction, 200);

    if(!this.get('value')){
      return;
    }

    this.set('value', String(this.get('value')).replace(/[^\d\.\,]/g, ''));
  }
});

标签:javascript,ember-js,ember-cli,qunit,ember-qunit
来源: https://codeday.me/bug/20190628/1321094.html