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

Ember: Testing WillDestroyElement Logic of Component in an Integration Test

本文关键字:组件 WillDestroyElement 逻辑 测试 集成测试 Ember      更新时间:2023-09-26

我有一个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插件事件(例如,使用此处描述的技术),那么我该如何在集成测试中测试拆卸逻辑呢?

您可以使用component.destroyElement(其中提到它将调用willDestroyElement钩子)或component.destroy

不访问组件实例的方法:

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

然后设置:

this.set('showComponent', false);