0

I am writing a simple unit test for a element click '

Here is my html

<div *ngIf="!hidden" id='button'>
    <button
        [color]="'primary'"
        [disabled]="disabled"
        [label]="lablel"
        id="createButton"
        (click)="handleNextClick()"
        id='button'
    ></button>
</div>

Here is my unit test

it('should handleNextClick method called on button click', () => {
        let createsearchCreateDiv = fixture.debugElement.query(By.css('#button'));
        createsearchCreateDiv.nativeElement.click();



        fixture.whenStable().then(() => {
            expect(component.handleNextClick).toHaveBeenCalled();
        });

    });

My test case is passed irrespective of below line

createsearchCreateDiv.nativeElement.click();

Ideally it should not pass only if I remove/comment above line.

Method is not getting called if I remove/comment click. But still test case getting passed

1 Answer 1

1

It's an async call, so you should use done, async/await or return the Promise:

it('should handleNextClick method called on button click', () => {
  // ...
  return fixture.whenStable().then(() => {
    expect(component.handleNextClick).toHaveBeenCalled();
  });
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.