summaryrefslogtreecommitdiff
path: root/src/components/ui/pagination.test.tsx
blob: 77309b8fea79bf5c8f7ca9784208e7b3fd23c907 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import { render, screen } from '@testing-library/react';
import { describe, expect, test } from 'vitest';
import {
  Pagination,
  PaginationContent,
  PaginationEllipsis,
  PaginationItem,
  PaginationLink,
  PaginationNext,
  PaginationPrevious,
} from './pagination';

describe('Pagination UI', () => {
  test('renders pagination container and list slots', () => {
    render(
      <Pagination>
        <PaginationContent>
          <PaginationItem>
            <PaginationLink href='#'>1</PaginationLink>
          </PaginationItem>
        </PaginationContent>
      </Pagination>,
    );

    const nav = screen.getByLabelText('pagination');
    expect(nav).toHaveAttribute('data-slot', 'pagination');
    expect(screen.getByRole('link', { name: '1' })).toHaveAttribute(
      'data-slot',
      'pagination-link',
    );
  });

  test('sets active page attributes', () => {
    render(
      <PaginationLink href='#' isActive>
        2
      </PaginationLink>,
    );

    const link = screen.getByRole('link', { name: '2' });
    expect(link).toHaveAttribute('aria-current', 'page');
    expect(link).toHaveAttribute('data-active', 'true');
  });

  test('renders previous, next, and ellipsis affordances', () => {
    render(
      <div>
        <PaginationPrevious href='#' />
        <PaginationNext href='#' />
        <PaginationEllipsis />
      </div>,
    );

    expect(
      screen.getByRole('link', { name: 'Go to previous page' }),
    ).toBeInTheDocument();
    expect(screen.getByRole('link', { name: 'Go to next page' })).toBeInTheDocument();
    expect(screen.getByText('More pages')).toBeInTheDocument();
  });
});