Reputation: 2950
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');
});
Upvotes: 2
Views: 876
Reputation: 222369
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.
Upvotes: 2