import { screen } from '@testing-library/react'
import { describe, expect, it } from 'vitest'

import Pagination from '@/components/common/Pagination'
import { renderWithProviders } from '@/test/render'

describe ('Pagination', () => {
  it ('builds page links while preserving existing query parameters', () => {
    renderWithProviders (
        <Pagination page={3} totalPages={5} siblingCount={1}/>,
        { route: '/posts?tags=abc&page=3' },
    )

    expect (screen.getByLabelText ('前のページ')).toHaveAttribute (
        'href',
        '/posts?tags=abc&page=2',
    )
    expect (screen.getByLabelText ('次のページ')).toHaveAttribute (
        'href',
        '/posts?tags=abc&page=4',
    )
    expect (screen.getByText ('3')).toHaveAttribute ('aria-current', 'page')
  })

  it ('does not render active previous and next controls at the edges', () => {
    const { rerender } = renderWithProviders (
        <Pagination page={1} totalPages={1}/>,
        { route: '/tags' },
    )

    expect (screen.queryByLabelText ('前のページ')).not.toBeInTheDocument ()
    expect (screen.queryByLabelText ('次のページ')).not.toBeInTheDocument ()

    rerender (<Pagination page={1} totalPages={2}/>)

    expect (screen.getByLabelText ('次のページ')).toHaveAttribute ('href', '/tags?page=2')
  })
})
