Ember Integration:触发根元素上的click事件

时间:2016-01-19 14:09:59

标签: javascript jquery ember.js

我正在编写一些ember集成测试,我在测试组件上的所有内容时遇到了一些问题。例如,我有一个看起来像

的组件
// my-component.js
export default Ember.Component.extend({
  click() {
    console.log('here');
  }
});

看起来像这样的测试

// my-component-test.js
describeComponent(
  'my-component',
  'Integration: MyComponentComponent',
  {
    integration: true
  },
  function() {
    describe('interacting', function() {
      beforeEach(function() {
        this.render(hbs`
          {{my-component}}
        `);
      });

      it('registers the click', function() {
        this.$().click();
      });
    });
  }
);

在实际情况中,我显然想测试组件在该点击上做了什么,但不幸的是这个触发器不起作用(没有得到日志)。任何人都可以回答如何在根元素上触发click事件?

1 个答案:

答案 0 :(得分:5)

在Ember组件集成测试中,this.$()未选择组件的元素。它选择呈现组件的父元素。

因此,如果使用

渲染组件

this.render(hbs`{{my-component id="my-component}}`);

然后您可以使用

单击组件的元素

this.$('#my-component').click();