How to check a function that calls Ember.run.debounce in ember-qunit?

The controller I would like to test contains the following:

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'),

Now I would like to write test, which claims to be filteredFoosupdated when I install filterText.

To do this correctly, I will need to take into account Ember.run.debounceand wait for this to happen before I complete my statement. How can i do this?

+4
source share
1 answer

I also ran into this problem and in order to drown out debounce, I did the following:

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, ''));
  }
});
+3

Source: https://habr.com/ru/post/1548315/


All Articles