4

I have a component and I want to check the selector in unit tests

@Component({
     selector: 'my-component',
)}

I want to test like this

describe('My Component', function(){ 
   it('should have a selector', function() {
      expect( ___ ).toBe('my-component');
   }); 
)}

2 Answers 2

6

In you need to use Reflect Metadata to have access to these hints. Supposing that the class of the component is MyComponent, you could do this to test the selector:

describe('My Component', function(){ 
  it('should have a selector', function() {
    var annotations = Reflect.getMetadata('annotations', MyComponent);
    // Supposing the first annotations is of type ComponentMetadata
    expect(annotations[0].selector).toBe('my-component');
  });
});

See this plunkr: https://plnkr.co/edit/HQc1qt?p=preview.

Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know how to test decorator's selector metadata. Thanks for indicating the right direction.
0

On rc you may need to retrieve the rereflactor from core then use the reflector.annotations to retrieve the info

import {reflector} from '@angular/core/src/reflection/reflection';

it('should have a selector', inject([SettingsComponent], (component: any) => {
    let meta = reflector.annotations(SettingsComponent)[0];
    expect(meta.selector).toBe('Settings');
}));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.