Run tests in a headless browser with vitest browser mode and playwright

This commit is contained in:
Alejandro Celaya
2025-04-08 11:49:33 +02:00
parent 09559c78af
commit 691e6c1afb
15 changed files with 451 additions and 182 deletions

View File

@@ -24,15 +24,13 @@ describe('<ManageServersRowDropdown />', () => {
};
const toggleDropdown = (user: UserEvent) => user.click(screen.getByRole('button'));
it.each([
[setUp],
[async () => {
const { user, container } = setUp();
await toggleDropdown(user);
it('passes a11y checks', async () => {
const { user, container } = setUp();
// Open menu
await toggleDropdown(user);
return { container };
}],
])('passes a11y checks', (setUp) => checkAccessibility(setUp()));
return checkAccessibility({ container });
});
it('renders expected amount of dropdown items', async () => {
const { user } = setUp();

View File

@@ -20,7 +20,13 @@ describe('<ServersDropdown />', () => {
</MemoryRouter>,
);
it('passes a11y checks', () => checkAccessibility(setUp()));
it('passes a11y checks', async () => {
const { user, ...rest } = setUp();
// Open menu
await user.click(screen.getByText('Servers'));
return checkAccessibility(rest);
});
it('contains the list of servers and the "mange servers" button', async () => {
const { user } = setUp();

View File

@@ -24,17 +24,13 @@ describe('ServersExporter', () => {
const createCsvjsonMock = (throwError = false) => (throwError ? erroneousToCsv : vi.fn(() => ''));
describe('exportServers', () => {
let originalConsole: Console;
const error = vi.fn();
beforeEach(() => {
originalConsole = global.console;
global.console = fromPartial<Console>({ error });
(global as any).Blob = class Blob {};
(global as any).URL = { createObjectURL: () => '' };
vi.stubGlobal('console', fromPartial<Console>({ error }));
});
afterEach(() => {
global.console = originalConsole;
vi.unstubAllGlobals();
});
it('logs an error if something fails', () => {

View File

@@ -1,28 +0,0 @@
import { screen, waitFor } from '@testing-library/react';
import type { DateInterval } from '../../../src/utils/dates/DateIntervalSelector';
import { DateIntervalSelector, INTERVAL_TO_STRING_MAP } from '../../../src/utils/dates/DateIntervalSelector';
import { checkAccessibility } from '../../__helpers__/accessibility';
import { renderWithEvents } from '../../__helpers__/setUpTest';
describe('<DateIntervalSelector />', () => {
const activeInterval: DateInterval = 'last7Days';
const onChange = vi.fn();
const setUp = () => renderWithEvents(
<DateIntervalSelector allText="All text" active={activeInterval} onChange={onChange} />,
);
it('passes a11y checks', () => checkAccessibility(setUp()));
it('passes props down to nested DateIntervalDropdownItems', async () => {
const { user } = setUp();
const btn = screen.getByRole('button');
await user.click(btn);
await waitFor(() => expect(screen.getByRole('menu')).toBeInTheDocument());
const items = screen.getAllByRole('menuitem');
expect(btn).toHaveTextContent(INTERVAL_TO_STRING_MAP[activeInterval] ?? '');
expect(items).toHaveLength(8);
});
});