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

feat(Pagination): Add testid for buttons #8038

Merged
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
33 changes: 33 additions & 0 deletions packages/vkui/src/components/Pagination/Pagination.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,37 @@ describe('Pagination', () => {
fireEvent.click(screen.getByText('Назад'));
expect(onChange.mock.calls[2][0]).toBe(3);
});

it('checks data-testid setup', () => {
const onChange = jest.fn();
const Fixture = ({ page }: { page: number }) => {
return (
<Pagination
pageButtonTestId={(day, active) => (active ? `active-day-${day}` : `day-${day}`)}
prevButtonTestId="prevButton"
nextButtonTestId="nextButton"
currentPage={page}
navigationButtonsStyle="caption"
siblingCount={0}
boundaryCount={1}
totalPages={100}
disabled={false}
onChange={onChange}
/>
);
};

const component = render(<Fixture page={0} />);
fireEvent.click(screen.getByTestId('day-3'));
expect(onChange.mock.calls[0][0]).toBe(3);

component.rerender(<Fixture page={3} />);
expect(screen.queryByTestId('active-day-3')).toBeTruthy();
fireEvent.click(screen.getByTestId('nextButton'));
expect(onChange.mock.calls[1][0]).toBe(4);

component.rerender(<Fixture page={4} />);
fireEvent.click(screen.getByTestId('prevButton'));
expect(onChange.mock.calls[2][0]).toBe(3);
});
});
27 changes: 23 additions & 4 deletions packages/vkui/src/components/Pagination/Pagination.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,18 @@ export interface PaginationProps extends Omit<HTMLAttributesWithRootRef<HTMLElem
* > Note: `CustomPaginationNavigationButton` наследует API [Button](https://vkcom.github.io/VKUI/#/Button).
*/
renderNavigationButton?: (props: CustomPaginationNavigationButton) => React.ReactNode;
/**
* e2e test-id для кнопок страниц
*/
pageButtonTestId?: (day: PaginationPageType, active: boolean) => string;
/**
* e2e test-id для кнопки `prev`
*/
prevButtonTestId?: string;
/**
* e2e test-id для кнопки `next`
*/
nextButtonTestId?: string;
}

/**
Expand All @@ -112,6 +124,9 @@ export const Pagination = ({
nextButtonLabel = 'Перейти на следующую страницу',
onChange,
renderPageButton,
pageButtonTestId,
prevButtonTestId,
nextButtonTestId,
renderNavigationButton,
...resetProps
}: PaginationProps): React.ReactNode => {
Expand Down Expand Up @@ -156,17 +171,18 @@ export const Pagination = ({

const renderPages = React.useCallback(
(page: PaginationPageType) => {
const isCurrent = page === currentPage;
const dataTestId = pageButtonTestId?.(page, isCurrent);

switch (page) {
case 'start-ellipsis':
case 'end-ellipsis':
return (
<li key={page}>
<PaginationPageEllipsis disabled={disabled} />
<PaginationPageEllipsis disabled={disabled} data-testid={dataTestId} />
</li>
);
default: {
const isCurrent = page === currentPage;

return (
<li key={page}>
<PaginationPageButton
Expand All @@ -176,6 +192,7 @@ export const Pagination = ({
disabled={disabled}
sizeY={sizeY}
renderPageButton={renderPageButton}
data-testid={dataTestId}
>
{page}
</PaginationPageButton>
Expand All @@ -184,7 +201,7 @@ export const Pagination = ({
}
}
},
[currentPage, disabled, getPageLabel, handleClick, renderPageButton, sizeY],
[currentPage, disabled, getPageLabel, handleClick, renderPageButton, sizeY, pageButtonTestId],
);

return (
Expand All @@ -201,6 +218,7 @@ export const Pagination = ({
disabled={isFirstPage || disabled}
onClick={handlePrevClick}
data-page={prevPage}
data-testid={prevButtonTestId}
renderNavigationButton={renderNavigationButton}
/>
</li>
Expand All @@ -215,6 +233,7 @@ export const Pagination = ({
disabled={isLastPage || disabled}
onClick={handleNextClick}
data-page={nextPage}
data-testid={nextButtonTestId}
renderNavigationButton={renderNavigationButton}
/>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface PaginationNavigationButtonOpts {
'disabled'?: boolean;
'onClick': (event: React.MouseEvent<HTMLElement>) => void;
'data-page': number | undefined;
'data-testid': string | undefined;
}

export interface PaginationNavigationButtonProps extends PaginationNavigationButtonOpts {
Expand All @@ -22,7 +23,7 @@ export interface PaginationNavigationButtonProps extends PaginationNavigationBut
*/
const getButtonPropsFromPaginationNavigationButton = (
opts: PaginationNavigationButtonOpts,
): ButtonProps & { 'data-page': number | undefined } => {
): ButtonProps & { 'data-page': number | undefined; 'data-testid': string | undefined } => {
const icon: React.ReactElement | null =
opts.style !== 'caption' ? (
<>
Expand Down Expand Up @@ -50,7 +51,8 @@ const getButtonPropsFromPaginationNavigationButton = (
'onClick': opts.onClick,
'children': caption,
'data-page': opts['data-page'],
} satisfies ButtonProps & { 'data-page': number | undefined };
'data-testid': opts['data-testid'],
} satisfies ButtonProps & { 'data-page': number | undefined; 'data-testid': string | undefined };
};

/**
Expand Down
Loading