40 lines
1.2 KiB
TypeScript
40 lines
1.2 KiB
TypeScript
import { fireEvent, render, screen, waitFor } from '@testing-library/react'
|
|
import { CalendarDateInput } from '@/components/admin/DataStatisticsTab'
|
|
|
|
describe('CalendarDateInput', () => {
|
|
test('稳定日期值不会触发重复渲染,并会在日期变化时同步月份', async () => {
|
|
const consoleErrorSpy = jest.spyOn(console, 'error').mockImplementation(() => undefined)
|
|
|
|
try {
|
|
const { rerender } = render(
|
|
<CalendarDateInput
|
|
id="stats-start-date"
|
|
label="开始日期"
|
|
value="2026-08-15"
|
|
onChange={jest.fn()}
|
|
/>,
|
|
)
|
|
|
|
fireEvent.click(screen.getByRole('button', { name: '打开开始日期日历' }))
|
|
expect(screen.getByText('2026年08月')).toBeInTheDocument()
|
|
|
|
rerender(
|
|
<CalendarDateInput
|
|
id="stats-start-date"
|
|
label="开始日期"
|
|
value="2026-07-15"
|
|
onChange={jest.fn()}
|
|
/>,
|
|
)
|
|
|
|
await waitFor(() => {
|
|
expect(screen.getByText('2026年07月')).toBeInTheDocument()
|
|
})
|
|
|
|
expect(consoleErrorSpy.mock.calls.flat().join(' ')).not.toContain('Maximum update depth exceeded')
|
|
} finally {
|
|
consoleErrorSpy.mockRestore()
|
|
}
|
|
})
|
|
})
|