Angular2 test if component has proper selector

霸气de小男生 提交于 2020-01-04 08:05:07

问题


Is there any possibility in Angular2 to verify component selector with unit test?

e.g.

@Component({
    selector: 'my-component',
    template: '<p>test</p>'
})
export class MyComponent {}

and

....
it(`Component should have selector 'my-component'`, () => {
    expect(component.selector).toBe('my-component');
});

回答1:


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');

Where Reflect.getMetadata('annotations', ...) returns an array of class annotations.

This requires reflect-metadata or core-js/es7/reflect polyfills to be loaded. This polyfill is Angular prerequisite.



来源:https://stackoverflow.com/questions/44182326/angular2-test-if-component-has-proper-selector

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!