Javascript Ember:在集成测试中测试组件的元素逻辑
我有一个余烬组件Javascript Ember:在集成测试中测试组件的元素逻辑,javascript,jquery,ember.js,integration-testing,Javascript,Jquery,Ember.js,Integration Testing,我有一个余烬组件 Ember.Component.extend({ onDidInsertElement: function(){ var that = this; Ember.run.scheduleOnce('afterRender', () => { // Some code that sets up a jQuery plugin which attaches events etc }); }, onWillDestroyElemen
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插件事件(例如,使用所描述的技术),我如何在集成测试中实际测试拆卸逻辑?您可以使用(其中提到它将调用willDestroyElement
hook)或
在不访问组件实例的情况下执行此操作的方法:
this.set('showComponent', true);
this.render(hbs(`{{if showComponent}}{{my-component}}{{/if}}`));
然后设置:
this.set('showComponent', false);
好的,但是我如何从集成测试中访问“组件”呢?也许我应该提到我正在使用Ember 2.1和集成测试组件的新方法(请参见此处)。除非我遗漏了什么,否则在这种新方法中,'this'对象没有方法'destroyElement'、'destroy'和属性'component'或任何我能看到的可以让我访问组件的类似内容。我还可以补充一点,我想用
this.render(hbs
)来“清除”渲染,但这似乎并没有拆掉第一个组件。我还寻找了额外的测试挂钩(比如测试设置逻辑中的“beforeach”和“afterEach”),但同样无法在组件似乎被破坏的任何地方挂钩。在ember qunit源代码中寻找线索也没有多大帮助。让我觉得我可能走错了方向…通常在单元测试中测试willDestroyElement
,我认为在集成测试中可能会用到一些条件。类似于this.set('showComponent',true)
,render(hbs({{if showComponent}}{{my component}}{{/if}}')
然后设置this.set('showComponent',false)
。你可以试试。宾果:)。好主意!也许只是更新你的答案,为未来的人提供最新的建议。。。