2015-11-08 82 views
1

我已经灰烬成分...烬:组件的测试WillDestroyElement逻辑在集成测试

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描述的技术),我如何在我的集成测试中真正测试拆解逻辑?

回答

2

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

办法做到这一点,而无需访问组件实例:

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

然后设置:

this.set('showComponent', false); 
+0

好吧,但我怎么访问, '组件',从一个集成测试? – sammy34

+0

'这个'应该有效。 –

+1

也许我应该提到我正在使用Ember 2.1和集成测试组件的新方法(请参阅http://guides.emberjs.com/v2.1.0/testing/testing-components/)。除非我错过了一些东西,在这种新方法中,'this'对象没有方法'destroyElement','destroy',也没有属性'组件'或任何类似的东西,我可以看到这会让我访问组件。 – sammy34