3x-ui/frontend/src/test/test-utils.tsx
Alireza Ahmadi e2184777a0
fix(frontend): make the panel usable with screen readers (#6907)
* fix(frontend): make the panel usable with screen readers

* fix(frontend): keep mobile tab names and the inbound expiry hint

Review of #6907 found two gaps in the screen-reader pass:

- On phone-width screens catTabLabel renders only the icon and names the
  tab by putting aria-label on it, but callers now pass that icon with
  aria-hidden for the desktop label, so every Settings, Xray and Host
  category tab lost its name under TalkBack. catTabLabel now clears
  aria-hidden on the icon it uses as the name.
- DateTimePicker forwarded id and aria-label but not aria-describedby,
  so the inbound expiry hint ("leave blank to never expire") was never
  attached. It now forwards it on both the Gregorian and Jalali input.
2026-10-23 18:29:47 +02:00

68 lines
2.3 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 select = selectRootForField(fieldId);
openSelect(select);
const opts = openDropdownOptions();
fireEvent.keyDown(select, { key: 'Escape' });
return opts;
}
export function chooseSelectOption(fieldId: string, optionText: string) {
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);
}