简体   繁体   English

Ember.js:如何在组件测试中断言事件未绑定?

[英]Ember.js: How to assert event unbound in component test?

In component code like: 在像这样的组件代码中:

didInsertElement() {
  this.$('.something').on(//...);
},
willDestroyElement() {
  this.$('.something').off(//...);
}

You have the method of querying for events $._data(this.$('.something')[0], 'events') , but when destroyed, the element is gone. 您可以使用查询事件$._data(this.$('.something')[0], 'events') ,但是销毁该元素后,该元素就消失了。 Is there a way to assert in test that the event was actually unbound before destroyed? 有没有办法在测试中断言该事件在销毁之前实际上是未绑定的? Is there a way to query for all events on the page then somehow filter for the one in question? 有没有一种方法可以查询页面上的所有事件,然后以某种方式过滤出相关事件?

There is no way to programmatically determine whether an event has been bound or unbound to an element without adding an interface in between and tracking it there. 如果不在两者之间添加接口并在那里进行跟踪,就无法以编程方式确定事件是否已绑定到元素。

So in effect, you've actually answered your own question: accessing the listener via jQuery is the way to go. 因此,实际上,您实际上已经回答了自己的问题:通过jQuery访问侦听器是必经之路。 Cache the element before destroying the component, and use it for the lookup after. 在销毁组件之前先缓存元素,然后将其用于之后的查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM