import { cleanup, fireEvent, render, screen } from '@testing-library/react';
import { afterEach, describe, expect, test, vi } from 'vitest';
import { TablePagination } from './TablePagination';

describe('TablePagination', () => {
  afterEach(cleanup);

  test('renders one shared Semi pagination pattern and changes pages', () => {
    const onPageChange = vi.fn();
    const { container } = render(<footer><TablePagination page={2} totalPages={4} info="共 80 条" onPageChange={onPageChange} /></footer>);

    expect(screen.getByText('共 80 条')).toHaveClass('v2-table-pagination-info');
    expect(container.querySelector('.v2-table-pagination')).toHaveClass('semi-page');
    expect(container.querySelector('.semi-page-item-small')).toHaveTextContent('2/4');
    const previous = screen.getByRole('button', { name: 'Previous' });
    const next = screen.getByRole('button', { name: 'Next' });
    fireEvent.click(previous);
    fireEvent.click(next);
    expect(onPageChange).toHaveBeenNthCalledWith(1, 1);
    expect(onPageChange).toHaveBeenNthCalledWith(2, 3);
  });

  test('clamps invalid pages and disables both boundaries for a single page', () => {
    render(<footer><TablePagination page={0} totalPages={0} info="暂无数据" onPageChange={vi.fn()} /></footer>);
    expect(screen.getByRole('button', { name: 'Previous' })).toHaveAttribute('aria-disabled', 'true');
    expect(screen.getByRole('button', { name: 'Next' })).toHaveAttribute('aria-disabled', 'true');
    expect(document.querySelector('.semi-page-item-small')).toHaveTextContent('1/1');
  });
});
