vendredi 3 juin 2016

Mocking component function for the purposes of component integration test in EmberJS

Let'a assume a simple EmberJS component:

//app/components/my-component.js
export default Ember.Component.extend({

  classNames: ['cursor-pointer'],

  doSth(){
      // whatever, state of the component does not change
  },

  clickListener: Ember.on('click', function(){
      this.doSth();
  })
});

Now I would like to use integration tests to find out whether clicking the component reaches (delegates to) the doSth() method.

moduleForComponent('my-component', 'Integration | Component | stores parser previewer', {
  integration: true
});
test('should call doSth() on click', function (assert) {

   /* Line 3 - the place I tried to set mocked doSth() up */      

   this.render(hbs``);
   this.$('.cursor-pointer').click(); // triggers clickListener() correctly
});

The problem is that I cannot substitute method doSth() with my mock. Thus I never run into assert.ok() statement.

I have tried to insert following statements onto the line 3 in the test:

// idea 1
this.set('doSth', function(){
    assert.ok(true);
  });

// idea 2
this.doSth = function(){
   assert.ok(true);
};

None of the 2 approaches (idea 1, idea 2) worked. this.subject() is also unavailable since it's an integration test, not a unit test.




Aucun commentaire:

Enregistrer un commentaire