Files
mirror-3x-ui/frontend/src/test/test-utils.tsx
T
Hamed f4030c889c fix(frontend): make inbound forms accessible to screen readers (#7025)
Expose selectable options and selected values, associate validation guidance with controls, and contain keyboard focus in inbound dialogs. Preserve create and edit serialization and add Chromium keyboard and accessibility checks.
2026-11-05 18:08:49 +01:00

81 lines
2.9 KiB
TypeScript

import type { ReactElement } from 'react';
import { render, fireEvent } from '@testing-library/react';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { ThemeProvider } from '@/hooks/useTheme';
import { AntdRootConfig } from '@/i18n/AntdRootConfig';
export function makeTestQueryClient() {
return new QueryClient({ defaultOptions: { queries: { retry: false } } });
}
export function renderWithProviders(ui: ReactElement, options?: { queryClient?: QueryClient }) {
const queryClient = options?.queryClient ?? makeTestQueryClient();
return render(
<QueryClientProvider client={queryClient}>
<AntdRootConfig>
<ThemeProvider>{ui}</ThemeProvider>
</AntdRootConfig>
</QueryClientProvider>,
);
}
export function fieldLabels(): string[] {
return Array.from(document.querySelectorAll('.ant-form-item-label label'))
.map((el) => (el.textContent ?? '').trim())
.filter(Boolean);
}
function selectRootForField(fieldId: string): HTMLElement {
const control = document.getElementById(fieldId);
const select = control?.closest('.ant-select') as HTMLElement | null;
if (!select) throw new Error(`Select not found for field id: ${fieldId}`);
return select;
}
function openSelect(select: HTMLElement) {
const target = (select.querySelector('.ant-select-selector') ?? select) as HTMLElement;
fireEvent.mouseDown(target);
}
function openDropdownOptions(): string[] {
return Array.from(
document.querySelectorAll(
'.ant-select-dropdown:not(.ant-select-dropdown-hidden) .ant-select-item-option',
),
)
.map((o) => (o.getAttribute('title') ?? o.textContent ?? '').trim())
.filter(Boolean);
}
export function listSelectOptions(fieldId: string): string[] {
const native = document.getElementById(fieldId);
if (native instanceof HTMLSelectElement) {
return Array.from(native.options)
.filter((option) => !option.hidden)
.map((option) => option.text);
}
const select = selectRootForField(fieldId);
openSelect(select);
const opts = openDropdownOptions();
fireEvent.keyDown(select, { key: 'Escape' });
return opts;
}
export function chooseSelectOption(fieldId: string, optionText: string) {
const native = document.getElementById(fieldId);
if (native instanceof HTMLSelectElement) {
const option = Array.from(native.options).find((item) => item.text === optionText);
if (!option) throw new Error(`Option '${optionText}' not found for field '${fieldId}'`);
fireEvent.change(native, { target: { value: option.value } });
return;
}
const select = selectRootForField(fieldId);
openSelect(select);
const option = Array.from(document.querySelectorAll('.ant-select-item-option')).find(
(o) => (o.getAttribute('title') ?? o.textContent ?? '').trim() === optionText,
);
if (!option) throw new Error(`Option '${optionText}' not found for field '${fieldId}'`);
fireEvent.click(option);
}