1

Preciso realizar os testes seguindo o lifecycle do Angular. Não consegui identificar alguma ferramenta ou forma de fazer isto. Basicamente seria chamar os testes com base no lifecycle.

Segue simples exemplo:

my-component.ts:

import { Component } from '@angular/core';

@Component({
  selector: 'my-component', 
  template: `...`
})
export class MyComponent implements OnInit, AfterViewChecked {
  constructor(private element: ElementRef) {}

  ngOnInit() {
    this.element.innerHTML = 'Init';
  }

  ngAfterViewChecked() {
    this.element.innerHTML = 'AfterViewChecked';
  }
}

my-component.spec.ts

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { MyComponent } from './my.component';

describe('MyComponent', () => {
  let component: MyComponent;
  let fixture: ComponentFixture<MyComponent>;

  beforeEach(async(() => {
    TestBed.configureTestingModule({
      declarations: [MyComponent]
    })
      .compileComponents();
  }));

  beforeEach(() => {
    fixture = TestBed.createComponent(MyComponent);
    component = fixture.componentInstance;
    fixture.detectChanges();
    const compiled = fixture.debugElement.nativeElement;
  });

  it('should create', () => {
    expect(component).toBeTruthy();
  });

  // DEVERIA SER CHAMADO NO ON INIT
  it('should initiate', () => {
    expect(compiled.textContent).toEqual('Init');
  });

  // DEVERIA SER CHAMADO APÓS O LIFECYCLE AfterViewChecked
  it('should render view', () => {
    expect(compiled.textContent).toEqual('ViewChecked');
  });
});

1 Resposta 1

0

Voce pode chamar a propria funcao do component:

 it('should initiate', () => {
    component.ngOnInit();
    expect(compiled.textContent).toEqual('Init');
  });

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .