piotrgajow
piotrgajow

Reputation: 2950

Angular2 test if component has proper selector

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

Answers (1)

Estus Flask
Estus Flask

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

Related Questions