测试组件元数据

时间:2017-04-26 14:23:14

标签: angular testing jasmine

我们只想在少数组件上测试ChangeDetection值,但我们找不到轻松访问组件元数据的方法。 对于管道,我们发现:

OnPush

这里的主要问题是为组件找到一种简单的方法,并检查它是否为{{1}}。这有什么想法吗? 谢谢。

1 个答案:

答案 0 :(得分:3)

好像你在寻找:

new DirectiveResolver().resolve(TestComponent) as Component).changeDetection

完整代码:

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

<强> Plunker Example