أنجولار - اختبار Angular2 لمعرفة ما إذا كان المكون يحتوي على المحدد الصحيح


هل هناك أي إمكانية في Angular2 للتحقق من محدد المكون باستخدام اختبار الوحدة؟

على سبيل المثال

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

و

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

الإجابة رقم 1

تُستخدم بيانات التعريف Reflect بواسطة Angular DI لتخزين واسترجاع بيانات المزخرف.

من الممكن الحصول على البيانات الوصفية من الصف والتحقق منها:

  const [componentDecorator] = Reflect.getOwnMetadata('annotations', MyComponentClass);

  expect(componentDecorator.selector).toBe('my-component');

حيث تُعيد Reflect.getMetadata('annotations', ...) مصفوفة من التعليقات التوضيحية للفئة.

هذا يتطلب تحميل reflect-metadata أو core-js/es7/reflect كملحقات. هذا الملحق هو شرط مسبق لأنجولار.