Angular2 测试组件是否有合适的选择器 [英] Angular2 test if component has proper selector
本文介绍了Angular2 测试组件是否有合适的选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Angular2 有没有可能通过单元测试来验证组件选择器?
Is there any possibility in Angular2 to verify component selector with unit test?
例如
@Component({
selector: 'my-component',
template: '<p>test</p>'
})
export class MyComponent {}
和
....
it(`Component should have selector 'my-component'`, () => {
expect(component.selector).toBe('my-component');
});
推荐答案
Reflect
元数据被 Angular DI 用来存储和检索装饰器数据.
Reflect
metadata is used by Angular DI to store and retrieve decorator data.
可以从类中获取元数据并对其进行断言:
It is possible to get metadata from the class and assert it:
const [componentDecorator] = Reflect.getOwnMetadata('annotations', MyComponentClass);
expect(componentDecorator.selector).toBe('my-component');
其中 Reflect.getMetadata('annotations', ...)
返回类注释数组.
Where Reflect.getMetadata('annotations', ...)
returns an array of class annotations.
这需要加载 reflect-metadata
或 core-js/es7/reflect
polyfills.这个 polyfill 是 Angular 的先决条件.
This requires reflect-metadata
or core-js/es7/reflect
polyfills to be loaded. This polyfill is Angular prerequisite.
这篇关于Angular2 测试组件是否有合适的选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文