我有一个 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/