Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(Pagination): add tests #1605

Merged
merged 5 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions src/components/Pagination/__tests__/Pagination.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import React from 'react';

import {noop} from 'lodash';

import {render, screen} from '../../../../test-utils/utils';
import {Pagination} from '../Pagination';
import {PaginationQa, getPaginationPageQa} from '../constants';

describe('Pagination component', () => {
test('Single page', () => {
render(<Pagination pageSize={20} total={20} onUpdate={noop} page={0} />);

const firstButton = screen.getByTestId(PaginationQa.PaginationButtonFirst);
expect(firstButton).toBeDisabled();

const prevButton = screen.getByTestId(PaginationQa.PaginationButtonPrevious);
expect(prevButton).toBeDisabled();

const nextButton = screen.getByTestId(PaginationQa.PaginationButtonNext);
expect(nextButton).toBeDisabled();
});

describe('Two pages', () => {
const PAGE_SIZE = 20;
const TOTAL = PAGE_SIZE + 1;

const FIRST_PAGE = 1;
const SECOND_PAGE = 2;

test('First page is current', () => {
render(
<Pagination pageSize={PAGE_SIZE} total={TOTAL} onUpdate={noop} page={FIRST_PAGE} />,
);

const firstPageButton = screen.getByTestId(getPaginationPageQa(FIRST_PAGE));
expect(firstPageButton).toHaveAttribute('aria-pressed', 'true');

const secondPageButton = screen.getByTestId(getPaginationPageQa(SECOND_PAGE));
expect(secondPageButton).toHaveAttribute('aria-pressed', 'false');

const firstButton = screen.getByTestId(PaginationQa.PaginationButtonFirst);
expect(firstButton).toBeDisabled();

const prevButton = screen.getByTestId(PaginationQa.PaginationButtonPrevious);
expect(prevButton).toBeDisabled();

const nextButton = screen.getByTestId(PaginationQa.PaginationButtonNext);
expect(nextButton).not.toBeDisabled();
});

test('Second page is current', () => {
render(
<Pagination
pageSize={PAGE_SIZE}
total={TOTAL}
onUpdate={noop}
page={SECOND_PAGE}
/>,
);

const firstPageButton = screen.getByTestId(getPaginationPageQa(FIRST_PAGE));
expect(firstPageButton).toHaveAttribute('aria-pressed', 'false');

const secondPageButton = screen.getByTestId(getPaginationPageQa(SECOND_PAGE));
expect(secondPageButton).toHaveAttribute('aria-pressed', 'true');

const firstButton = screen.getByTestId(PaginationQa.PaginationButtonFirst);
expect(firstButton).not.toBeDisabled();

const prevButton = screen.getByTestId(PaginationQa.PaginationButtonPrevious);
expect(prevButton).not.toBeDisabled();

const nextButton = screen.getByTestId(PaginationQa.PaginationButtonNext);
expect(nextButton).toBeDisabled();
});
});

test('Total property undefined', () => {
render(<Pagination pageSize={20} onUpdate={noop} page={0} total={undefined} />);

const nextButton = screen.getByTestId(PaginationQa.PaginationButtonNext);

expect(nextButton).not.toBeDisabled();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import {Button} from '../../../Button';
import {block} from '../../../utils/cn';
import {PaginationQa} from '../../constants';
import {getPaginationPageQa} from '../../constants';
import type {PageItem, PaginationProps, PaginationSize} from '../../types';

import './PaginationPage.scss';
Expand All @@ -18,7 +18,7 @@ type Props = {
};

export const PaginationPage = ({item, size, pageSize, className, onUpdate}: Props) => {
const qa = `${PaginationQa.PaginationPage}-${item.page}`;
const qa = getPaginationPageQa(item.page);
if (item.simple) {
return (
<div data-qa={qa} className={b('simple', {size}, className)}>
Expand Down
4 changes: 4 additions & 0 deletions src/components/Pagination/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ export const PaginationQa = {
PaginationButtonPrevious: 'pagination-button-previous',
PaginationButtonNext: 'pagination-button-next',
};

export const getPaginationPageQa = (pageNumber: number) => {
return `${PaginationQa.PaginationPage}-${pageNumber}`;
};
Loading