Solo nos gustaría probar el valor de ChangeDetection en algunos componentes, pero no encontramos una forma de acceder fácilmente a los metadatos de los componentes. Para tubería hemos encontrado:
it('should be marked as pure', () => { expect(new PipeResolver().resolve(TranslatePipe).pure).toEqual(true); }); El problema principal aquí es encontrar una manera fácil de hacerlo para el componente y verificar si es OnPush o no. ¿Alguna idea aquí? Así que gracias.
Parece que estás buscando:
new DirectiveResolver().resolve(TestComponent) as Component).changeDetectionCódigo completo:
import { Component, Pipe, ChangeDetectionStrategy } from '@angular/core'; import { PipeResolver, DirectiveResolver } from '@angular/compiler'; @Component({ selector: 'app-banner', template: '<h1>{{title}}</h1>', changeDetection: ChangeDetectionStrategy.OnPush }) export class TestComponent { title = 'Test Tour of Heroes'; } @Pipe({ name: 'translate', pure: true }) export class TranslatePipe { transform() { } } describe('BannerComponent', () => { it('should be marked as pure', () => { expect(new PipeResolver().resolve(TranslatePipe).pure).toEqual(true); }); it('should be marked as onPush', () => { expect((new DirectiveResolver().resolve(TestComponent) as Component).changeDetection).toEqual(ChangeDetectionStrategy.OnPush); }); });