import { changeField, openSelect } from '@/test-utils/select' import { fireEvent, render, screen } from '@testing-library/react' import MonthlyLaunchPeriodFilter from './MonthlyLaunchPeriodFilter' const mockPush = jest.fn() jest.mock('next/navigation', () => ({ usePathname: () => '/workspaces/dashboard/core-metrics/monthly-launches', useRouter: () => ({ push: mockPush }), })) const summary = { completed: 4, ongoing: 2, total: 6, monthStart: '2026-07-31T16:00:00.000Z', monthEnd: '2026-08-31T16:00:00.000Z', lastSyncedAt: '2026-08-31T04:00:00.000Z', syncStatus: 'ready' as const, } const options = [ { value: '2026-08', label: '本月', startDate: '2026-08-01', endDate: '2026-08-31' }, { value: '2026-07', label: '上月', startDate: '2026-07-01', endDate: '2026-07-31' }, ] describe('MonthlyLaunchPeriodFilter', () => { beforeEach(() => mockPush.mockClear()) it('shows available month presets and navigates with a month query', () => { render() openSelect(screen.getByRole('combobox', { name: '选择统计月份' })) expect(screen.getByRole('option', { name: '本月' })).toBeInTheDocument() expect(screen.getByRole('option', { name: '上月' })).toBeInTheDocument() expect(screen.getByRole('option', { name: '自定义日期' })).toBeInTheDocument() changeField(screen.getByRole('combobox', { name: '选择统计月份' }), { target: { value: '2026-07' }, }) expect(mockPush).toHaveBeenCalledWith( '/workspaces/dashboard/core-metrics/monthly-launches?month=2026-07', ) }) it('submits an inclusive custom date range', () => { render() changeField(screen.getByLabelText(/统计开始日期/), { target: { value: '2026-06-15' } }) changeField(screen.getByLabelText(/统计结束日期/), { target: { value: '2026-06-30' } }) expect(screen.getByRole('combobox', { name: '选择统计月份' })).toHaveValue('custom') fireEvent.submit(screen.getByRole('form', { name: '本月上新日期筛选' })) expect(mockPush).toHaveBeenCalledWith( '/workspaces/dashboard/core-metrics/monthly-launches?startDate=2026-06-15&endDate=2026-06-30', ) }) it('disables apply when the end date is before the start date', () => { render() changeField(screen.getByLabelText(/统计开始日期/), { target: { value: '2026-08-20' } }) changeField(screen.getByLabelText(/统计结束日期/), { target: { value: '2026-08-10' } }) expect(screen.getByRole('button', { name: /应用筛选/ })).toBeDisabled() expect(mockPush).not.toHaveBeenCalled() }) })