Skip to content
Open
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
43 changes: 43 additions & 0 deletions src/page-control/__tests__/page-control.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
Copyright (c) Uber Technologies, Inc.

This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
import * as React from 'react';
import { render, fireEvent } from '@testing-library/react';
import '@testing-library/jest-dom';

import PageControl from '../page-control';

describe('PageControl', () => {
it('does not throw when a dot is clicked and onPageChange is not supplied', () => {
const { getAllByRole } = render(<PageControl currentPage={1} numPages={3} />);
const dots = getAllByRole('radio');
expect(() => fireEvent.click(dots[1])).not.toThrow();
});

it('disables every dot and blocks page changes when disabled is set', () => {
const onPageChange = jest.fn();
const { getAllByRole } = render(
<PageControl currentPage={1} numPages={3} disabled onPageChange={onPageChange} />
);
const dots = getAllByRole('radio');

dots.forEach((dot) => expect(dot).toBeDisabled());

fireEvent.click(dots[1]);
expect(onPageChange).not.toHaveBeenCalled();
});

it('calls onPageChange with the clicked page when enabled', () => {
const onPageChange = jest.fn();
const { getAllByRole } = render(
<PageControl currentPage={1} numPages={3} onPageChange={onPageChange} />
);
const dots = getAllByRole('radio');

fireEvent.click(dots[1]);
expect(onPageChange).toHaveBeenCalledWith({ nextPage: 2 });
});
});
3 changes: 2 additions & 1 deletion src/page-control/page-control.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ const PageControl = ({
$isVisible={isVisible(page)}
$size={getSize(page)}
checked={isActive(page)}
disabled={disabled}
key={page}
aria-label={`page ${page}`}
name={name}
onChange={() => onPageChange({ nextPage: page })}
onChange={() => onPageChange?.({ nextPage: page })}
tabIndex={isActive(page) ? 0 : -1}
type="radio"
{...DotProps}
Expand Down