vendredi 20 avril 2018

Angular testing DOM events working even that they are asynchronous

I have a component that is changing the text when clicking on a button.

@Component({
  template: `
   <button (click)="onClick()">Change</button>
    <p></p>
  `
})
export class ClickComponent{
  name = 'init';
  onClick() {
    this.name = 'changed';
  }
}

And a simple test for it.

  it('should changed on click', () => {
    const button = fixture.nativeElement.querySelector('button');
    button.click();
    fixture.detectChanges();
    expect(fixture.nativeElement.querySelector('p').textContent).toEqual('changed');
  });

And the test passes. But I don't understand how. Event handlers are asynchronous and I don't wrap my code with the async helper and the test passes. What is the explanation for this?

Aucun commentaire:

Enregistrer un commentaire