import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, expect, it, vi } from 'vitest'

import { NotFoundPage } from './NotFoundPage'

const navigateMock = vi.fn()

vi.mock('react-router-dom', async () => {
  const actual = await vi.importActual<typeof import('react-router-dom')>('react-router-dom')
  return {
    ...actual,
    useNavigate: () => navigateMock,
  }
})

describe('NotFoundPage', () => {
  it('renders the 404 state and routes users back to the dashboard', async () => {
    const user = userEvent.setup()

    render(<NotFoundPage />)

    expect(screen.getByText('404')).toBeInTheDocument()
    expect(screen.getByText('抱歉，您访问的页面不存在')).toBeInTheDocument()

    await user.click(screen.getByRole('button', { name: '返回首页' }))

    expect(navigateMock).toHaveBeenCalledWith('/dashboard')
  })
})
