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

Ember: Testing WillDestroyElement Logic of Component in an Integration Test

我有一个 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 中描述的技术),我如何实际测试我的集成中的拆卸逻辑测试?

您可以使用 component.destroyElement (which mentions it will invoke willDestroyElement hook) or component.destroy

不访问组件实例的方法:

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

然后设置:

this.set('showComponent', false);