We just would like to test ChangeDetection value on few components but we don't find a way to access easly to component metadata. For pipe we have found:
it('should be marked as pure', () => {
expect(new PipeResolver().resolve(TranslatePipe).pure).toEqual(true);
});
Main problem here is to find a easy way to do it for component and check if it is OnPush or not. Any idea here?
So thanks.
Seems you are looking for:
new DirectiveResolver().resolve(TestComponent) as Component).changeDetection
Full code:
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);
});
});