Skip to content

Can't click on button inside mat-sidenav when using fakeAsync #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
KarmaCop213 opened this issue Feb 21, 2024 · 1 comment
Closed

Can't click on button inside mat-sidenav when using fakeAsync #437

KarmaCop213 opened this issue Feb 21, 2024 · 1 comment

Comments

@KarmaCop213
Copy link

KarmaCop213 commented Feb 21, 2024

Can't click on button inside sidenav when using fakeAsync.

Simple example:

import { render, screen } from "@testing-library/angular"
import userEvent from "@testing-library/user-event"

import { fakeAsync } from "@angular/core/testing"
import { MatSidenavModule } from "@angular/material/sidenav"

describe("Test", () => {
  test("Click button sidenav", fakeAsync(async () => {
    await render(
      `
      <ng-container>
        <mat-sidenav-container>
            <mat-sidenav [opened]="true" position="end" mode="over" role="complementary">
                <button data-testid="test-button">test</button>
            </mat-sidenav>
            <mat-sidenav-content>
              <div></div>
            </mat-sidenav-content>
        </mat-sidenav-container>
      </ng-container>
      `,
      { imports: [MatSidenavModule] }
    )

    const user = userEvent.setup()

    await screen.findByTestId("test-button")

    await user.click(screen.getByTestId("test-button"))
  }))
})

getting:

Error: 1 timer(s) still in the queue.

with these lib versions:

"@angular/core": "15.2.9",
"@testing-library/angular": "14.5.1",
"@testing-library/user-event": "14.5.2",
"@angular/material": "15.2.9",

EDIT:
If I don't use fakeAsync it works.
If I use jest.useFakeTimers() I have the same problem, I have to call jest.useRealTimers() before the user.click if I want to make it work again.

timdeschryver added a commit that referenced this issue Feb 22, 2024
@timdeschryver
Copy link
Member

@KarmaCop213 fakeAsync is a bit tricky, and I won't recommend it.
See #438 for an example with jest fake timers.
The trick is to make user-event aware of the timers, and provide the advanceTimers method a way to advance the fake time:

  const user = userEvent.setup({
    advanceTimers: jest.advanceTimersByTime,
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants