testing-library / react-testing-library

🐐 Simple and complete React DOM testing utilities that encourage good testing practices.

Home Page:https://testing-library.com/react

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Snackbar not auto closing

zeeshanjan82 opened this issue · comments

  • @testing-library/react version: 14.2.1
  • Testing Framework and version: Jest version 29.7.0
  • DOM Environment:

I am running the unit tests in react inside VS Code and using Google Chrome. I am using version 20.10.0 of Node.js
I am using react version 18.2.0

Relevant code or config:

  beforeEach(() => jest.useFakeTimers({ advanceTimers: true }));
  afterEach(() => {
    jest.useRealTimers();
  });

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

    const wrongEmail = faker.internet.email();
    const emailInput = screen.getByRole('textbox', { name: /email/i });
    await user.type(emailInput, wrongEmail);
    const submitBtn = screen.getByRole('button', { name: /login:SIGN_IN/i, hidden: true });
    await user.click(submitBtn);

    await waitFor(() => expect(screen.queryByText('notifications:SIGNING_USER_MSG')).toBeInTheDocument(), { timeout: 1000 });
    act(() => {
      jest.advanceTimersByTime(7000);
    });
    expect(screen.queryByText('notifications:SIGNING_USER_MSG')).not.toBeVisible();

What you did:

I have a functionality where I have used notistick using snackbar and I am building a logic screen which only takes an email id and when the user clicks on a 'Sign In' button then the snackbar in displayed with the message 'notifications:SIGNING_USER_MSG' and when the login fails then I am displaying another snackbar, this specific snackbar auto closes after few seconds.

What happened:

I am able to access the snackbar but when I forward the timer and then expect the snackbar to have closed but it is still open, when I test the UI in google chrome then the snackbar auto closes after some seconds.

image

Have you tried using waitForElementToBeRemoved? It helps with waiting for the moment an element is no longer in the DOM.
Also, without having a reproduction we won't be able to help. Please take the time to create a minimal reproduction using out repro link: https://testing-library.com/new-rtl

thanks @MatanBobi I was able to fix the issue actually I have changed my approach to render the instead and go with the integration tests for now instead of unit tests.