javascript - Ember : Testing WillDestroyElement Logic of Component in an Integration Test

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

最佳答案

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

无需访问组件实例即可实现此目的的方法:

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

然后设置:

this.set('showComponent', false);

关于javascript - Ember : Testing WillDestroyElement Logic of Component in an Integration Test,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33594747/

相关文章:

javascript - VueJS if/else : the else loads first, 然后 if 加载

javascript - 检测 iOS 中的水平触摸事件

javascript - Ember.js 在设置多个 Controller 模型时在 ApplicationRoute 中返回 promise

ember.js - 在 hbs 模板中在屏幕上显示 Handlebars 代码

javascript - 如何设置 cookie 的多个值并将它们全部显示在另一个页面上?

JavaScript:表单未通过 OnSubmit 进行验证

javascript - javascript 表单提交后的 Onsubmit 事件

jquery 根据输入字段总数隐藏/显示提交按钮

jquery - 根据浏览器宽度显示图像

ember.js - 从 Ember Data 支持的 ArrayController 添加/删除项目