Ember:在集成测试中测试组件的WillDestroyElement逻辑

时间:2015-11-08 13:58:27

标签: javascript jquery ember.js integration-testing

我有一个Ember组件......

Ember.Component.extend({
  onDidInsertElement: function(){
    var that = this;
    Ember.run.scheduleOnce('afterRender', () => {
      // Some code that sets up a jQuery plugin which attaches events etc
    });
  },
  onWillDestroyElement: function(){
    // Code that destroys the jQuery plugin (removing attached events etc)
  }
}

这是我的集成测试:

test('it renders', function (assert) {
  this.render(hbs`{{my-component }}`);

  // Here some code to make sure the rendered state is ok

  // then...
  // ?? How to fire the "willDestroyElement" lifecycle hook?
}

假设我知道如何检查页面上是否存在jQuery插件事件(例如使用here所述的技术),我如何在集成测试中实际测试拆卸逻辑?

1 个答案:

答案 0 :(得分:2)

您可以使用component.destroyElement(提及它会调用willDestroyElement挂钩)或component.destroy

在不访问组件实例的情况下执行此操作的方法:

this.set('showComponent', true);
this.render(hbs(`{{if showComponent}}{{my-component}}{{/if}}`));

然后设置:

this.set('showComponent', false);