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

import SortHeader from '@/components/SortHeader'
import { renderWithProviders } from '@/test/render'

describe ('SortHeader', () => {
  it ('toggles the active sort direction and resets the page', () => {
    renderWithProviders (
        <SortHeader
            by="title"
            label="タイトル"
            currentOrder="title:asc"
            defaultDirection={{ title: 'asc' }}/>,
        { route: '/posts?tags=x&page=4&order=title%3Aasc' },
    )

    expect (screen.getByRole ('link', { name: 'タイトル ▲' })).toHaveAttribute (
        'href',
        '/posts?tags=x&page=1&order=title%3Adesc',
    )
  })

  it ('uses default direction for inactive fields', () => {
    renderWithProviders (
        <SortHeader
            by="updated_at"
            label="更新"
            currentOrder="title:desc"
            defaultDirection={{ title: 'asc', updated_at: 'desc' }}/>,
        { route: '/posts?page=2' },
    )

    expect (screen.getByRole ('link', { name: '更新' })).toHaveAttribute (
        'href',
        '/posts?page=1&order=updated_at%3Adesc',
    )
  })
})
