Skip to content

Fire events that update state during the test are wrapped in act(...) #222

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

Open
wants to merge 1 commit into
base: v5
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions src/app/app.test.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import * as React from 'react';
import App from '@app/index';
import { render, screen, } from '@testing-library/react';
import userEvent from '@testing-library/user-event'
import '@testing-library/jest-dom'
import { act, render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';

describe('App tests', () => {
test('should render default App component', () => {
Expand All @@ -14,42 +14,57 @@ describe('App tests', () => {
it('should render a nav-toggle button', () => {
render(<App />);

expect(screen.getByRole('button', { name: "Global navigation"})).toBeVisible();
expect(screen.getByRole('button', { name: 'Global navigation' })).toBeVisible();
});

// I'm fairly sure that this test not going to work properly no matter what we do since JSDOM doesn't actually
// I'm fairly sure that this test not going to work properly no matter what we do since JSDOM doesn't actually
// draw anything. We could potentially make something work, likely using a different test environment, but
// using Cypress for this kind of test would be more efficient.
it.skip('should hide the sidebar on smaller viewports', () => {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 600 });
act(() => {
Object.defineProperty(window, 'innerWidth', { writable: true, configurable: true, value: 600 });
});

render(<App />);
act(() => {
render(<App />);
});

window.dispatchEvent(new Event('resize'));
act(() => {
window.dispatchEvent(new Event('resize'));
});

expect(screen.queryByRole('link', { name: "Dashboard"})).not.toBeInTheDocument();
expect(screen.queryByRole('link', { name: 'Dashboard' })).not.toBeInTheDocument();
});

it('should expand the sidebar on larger viewports', () => {
render(<App />);

window.dispatchEvent(new Event('resize'));
act(() => {
window.dispatchEvent(new Event('resize'));
});

expect(screen.getByRole('link', { name: "Dashboard"})).toBeVisible();
expect(screen.getByRole('link', { name: 'Dashboard' })).toBeVisible();
});

it('should hide the sidebar when clicking the nav-toggle button', async () => {
const user = userEvent.setup()
const user = userEvent.setup();

render(<App />);
act(() => {
render(<App />);
});

act(() => {
window.dispatchEvent(new Event('resize'));
});

window.dispatchEvent(new Event('resize'));
const button = screen.getByRole('button', { name: "Global navigation"})
const button = screen.getByRole('button', { name: 'Global navigation' });

expect(screen.getByRole('link', { name: "Dashboard"})).toBeVisible();
expect(screen.getByRole('link', { name: 'Dashboard' })).toBeVisible();

await user.click(button);

expect(screen.queryByRole('link', { name: "Dashboard"})).not.toBeInTheDocument();
act(() => {
expect(screen.queryByRole('link', { name: 'Dashboard' })).not.toBeInTheDocument();
});
});
});