Angular2测试组件是否具有适当的选择器 [英] Angular2 test if component has proper selector

查看:243
本文介绍了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 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆