How to test ChangeDetection value on a few components? Find a way to access easily to component metadata.

699    Asked by Amitraj in Python , Asked on Nov 5, 2019
Answered by Arun Singh

We can do it in following way

import { Component, Pipe, ChangeDetectionStrategy } from '@angular/core';

import { PipeResolver, DirectiveResolver } from '@angular/compiler';


@Component({

  selector: 'app-banner',

  template: '{{title}}',

  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);

  });

});



Your Answer

Interviews

Parent Categories