vendredi 11 décembre 2015

Why my test failed after adding didInsertElement to fetch json in ember?

I have a simple component that it's going to fetch data after the component is inserted. It was ok until I run my test. I got this error.

Assertion Failed: You have turned on testing mode, which disabled the run-loop's autorun. You will need to wrap any code with asynchronous side-effects in a run

I understand that the component is fetching data asynchronously but I'm not sure how to solve that in my integration test.

Here's my code

export default Ember.Component.extend({
  didInsertElement: function() {
    let source = this.get('source');
    let url = apiUrl + source;
    Ember.$.getJSON(url).then(function(response) {
      this.set('data', response.data);
    }.bind(this));
  },
  // something else
};

And this is my test.

moduleForComponent('panel', 'Integration | Component | panel', {
  integration: true,
  beforeEach () {
    this.render(hbs`{{panel}}`);
  }
});

test('it has source dropdown', function(assert) {
  assert.equal(this.$('select[name="Source"]').length, 1);
});

Without the fetching data bit, the test runs ok.




Aucun commentaire:

Enregistrer un commentaire