3x-ui/frontend/src/test/date-time-picker.test.tsx
MHSanaei 89e200ead4 fix(frontend): key geo entries by page position and clear test-suite noise
Zod 4: use the `error` param instead of the deprecated `message`.
lint:deprecated missed these because tsgolint's no-deprecated does not
resolve object-literal properties on a `string | Params` union.

Geodata: key geo entry rows by page position. antd deprecates rowKey's
index argument, and kind:value repeats within a page because the reader
drops domain attributes (22 pairs in geosite_IR.dat, 108 in geosite_RU).

Nord/PIA: the "All cities/regions" option used a null value, which antd
warns on. Map it through a sentinel at the Select boundary so form state
stays null, with tests that fail when the sentinel is not mapped back.

Tests:
- Run the oxlint guard through node; .bin/oxlint is a sh shim Windows
  cannot spawn, and the swallowed error left both guard cases vacuous.
- Start unit workers with --no-experimental-webstorage; msw's localStorage
  probe made Node 25+ warn once per forked worker.
- Set IS_REACT_ACT_ENVIRONMENT, which RTL never sets with globals: false,
  and settle the async updates it exposed inside act(). The row-cells
  memo test now fails when memo is removed.
- Disable antd's click wave in Storybook; it re-rendered inside the next
  story's act() and tripped "not configured to support act".
- Assert InboundFormModal's validation log instead of leaking it, and
  give the rule-form test a well-formed clients/list response.
2026-09-25 21:21:03 +02:00

96 lines
3.3 KiB
TypeScript

import { act, fireEvent, screen } from '@testing-library/react';
import dayjs from 'dayjs';
import type { Dayjs } from 'dayjs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import DateTimePicker from '@/components/form/DateTimePicker';
import { setDatepicker } from '@/hooks/useDatepicker';
import { renderWithProviders } from './test-utils';
vi.mock('persian-calendar-suite', () => ({
PersianDateTimePicker: ({
maxDate,
onChange,
}: {
maxDate?: Date;
onChange?: (value: number) => void;
}) => (
<button
type="button"
aria-label="Persian date time picker"
data-testid="persian-date-time-picker"
data-max-date={maxDate?.toISOString()}
onClick={() => onChange?.((maxDate?.getTime() ?? 0) + 1)}
/>
),
}));
// Runs before the shared cleanup(), so the picker is still mounted and re-renders.
afterEach(() => act(() => setDatepicker('gregorian')));
function openPicker(): void {
const input = document.querySelector('.ant-picker input');
if (!input) throw new Error('picker input not rendered');
fireEvent.mouseDown(input);
fireEvent.click(input);
}
function clickDayCell(title: string): void {
const cell = document.querySelector(`.ant-picker-cell[title="${title}"] .ant-picker-cell-inner`);
if (!cell) throw new Error(`day cell ${title} not rendered`);
fireEvent.click(cell);
}
describe('DateTimePicker', () => {
it('commits a clicked calendar date without an OK press', () => {
const onChange = vi.fn<(next: Dayjs | null) => void>();
renderWithProviders(<DateTimePicker value={null} onChange={onChange} />);
openPicker();
const tomorrow = dayjs().add(1, 'day').format('YYYY-MM-DD');
clickDayCell(tomorrow);
expect(onChange).toHaveBeenCalled();
const committed = onChange.mock.calls.at(-1)?.[0];
expect(committed?.format('YYYY-MM-DD HH:mm:ss')).toBe(`${tomorrow} 00:00:00`);
});
it('renders no OK confirm button in the picker footer', () => {
renderWithProviders(<DateTimePicker value={null} onChange={vi.fn()} />);
openPicker();
expect(document.querySelector('.ant-picker-ok')).toBeNull();
});
it('hides the Gregorian clear control and disables dates after maxDate', () => {
const maxDate = dayjs().add(1, 'day').startOf('day');
renderWithProviders(
<DateTimePicker value={maxDate} onChange={vi.fn()} allowClear={false} maxDate={maxDate} />,
);
expect(document.querySelector('.ant-picker-clear')).toBeNull();
openPicker();
const blockedCell = document.querySelector(
`.ant-picker-cell[title="${maxDate.add(1, 'day').format('YYYY-MM-DD')}"]`,
);
expect(blockedCell?.classList.contains('ant-picker-cell-disabled')).toBe(true);
});
it('applies clear and max-date constraints to the Jalali picker', () => {
setDatepicker('jalalian');
const maxDate = dayjs('2030-01-02T03:04:05');
const onChange = vi.fn();
renderWithProviders(
<DateTimePicker value={maxDate} onChange={onChange} allowClear={false} maxDate={maxDate} />,
);
expect(document.querySelector('.jdp-clear')).toBeNull();
expect(screen.getByTestId('persian-date-time-picker').dataset.maxDate).toBe(
maxDate.toDate().toISOString(),
);
fireEvent.click(screen.getByTestId('persian-date-time-picker'));
expect(onChange).not.toHaveBeenCalled();
});
});