Skip to content

Commit

Permalink
Move <Pagination> component to dedicated folder (#2915)
Browse files Browse the repository at this point in the history
  • Loading branch information
balanza authored Aug 23, 2024
1 parent ea80c85 commit 8273c8a
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 1 deletion.
File renamed without changes.
58 changes: 58 additions & 0 deletions assets/js/common/Pagination/Pagination.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import React from 'react';
import { action } from '@storybook/addon-actions';

import Pagination from '.';

export default {
title: 'Components/Pagination',
component: Pagination,
argTypes: {
pages: {
control: {
type: 'number',
},
defaultValue: 5,
},
currentPage: {
control: {
type: 'number',
},
defaultValue: 1,
},
currentItemsPerPage: {
control: {
type: 'number',
},
defaultValue: 10,
},
itemsPerPageOptions: {
control: {
type: 'array',
},
defaultValue: [10],
},
},
render: (args) => (
<Pagination
pages={args.pages}
currentPage={args.currentPage}
currentItemsPerPage={args.currentItemsPerPage}
itemsPerPageOptions={args.itemsPerPageOptions}
onSelect={(value) => {
action('onSelect')(value);
}}
onChangeItemsPerPage={(value) => {
action('onChangeItemsPerPage')(value);
}}
/>
),
};

export const Default = {
args: {
pages: 5,
currentPage: 1,
currentItemsPerPage: 10,
itemsPerPageOptions: [10, 20, 50],
},
};
88 changes: 88 additions & 0 deletions assets/js/common/Pagination/Pagination.test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import React, { act } from 'react';
import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import '@testing-library/jest-dom';
import { noop } from 'lodash';
import Pagination from '.';

describe('Pagination component', () => {
it('should render', () => {
render(
<Pagination
pages={5}
currentPage={1}
currentItemsPerPage={10}
itemsPerPageOptions={[10, 20, 50]}
onSelect={noop}
onChangeItemsPerPage={noop}
/>
);
expect(screen.getByText('Results per page')).toBeInTheDocument();
expect(screen.getByText('10')).toBeInTheDocument();

for (let i = 1; i <= 5; i += 1) {
expect(screen.getByText(`${i}`)).toBeInTheDocument();
}
expect(screen.queryByText('6')).not.toBeInTheDocument();
});

it('should select the correct page', async () => {
const user = userEvent.setup();

const onSelect = jest.fn();
render(
<Pagination
pages={5}
currentPage={1}
currentItemsPerPage={10}
itemsPerPageOptions={[10, 20, 50]}
onSelect={onSelect}
onChangeItemsPerPage={noop}
/>
);

for (let i = 1; i <= 5; i += 1) {
// This includes the current page, too. Is it expected?
// eslint-disable-next-line no-await-in-loop
await act(() => user.click(screen.getByText(`${i}`)));
expect(onSelect).toHaveBeenCalledTimes(1);
expect(onSelect).toHaveBeenCalledWith(i);
onSelect.mockClear();
}
});

it('should select items per page', async () => {
const user = userEvent.setup();

const onChangeItemsPerPage = jest.fn();
const currentItemsPerPage = 10;
const itemsPerPageOptions = [10, 20, 50];
render(
<Pagination
pages={5}
currentPage={1}
currentItemsPerPage={currentItemsPerPage}
itemsPerPageOptions={itemsPerPageOptions}
onSelect={noop}
onChangeItemsPerPage={onChangeItemsPerPage}
/>
);

for (let i = 0; i < itemsPerPageOptions.length; i += 1) {
// open dropdown
// eslint-disable-next-line no-await-in-loop
await act(() => user.click(screen.getByText(`${currentItemsPerPage}`)));

// select item
const selection = itemsPerPageOptions[i];
const [selectable] = screen.getAllByText(`${selection}`).reverse();
// eslint-disable-next-line no-await-in-loop
await act(() => user.click(selectable));

// check if the correct item was selected
expect(onChangeItemsPerPage).toHaveBeenCalledTimes(1);
expect(onChangeItemsPerPage).toHaveBeenCalledWith(selection);
onChangeItemsPerPage.mockClear();
}
});
});
3 changes: 3 additions & 0 deletions assets/js/common/Pagination/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import Pagination from './Pagination';

export default Pagination;
2 changes: 1 addition & 1 deletion assets/js/common/Table/Table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import React, { useState, useEffect } from 'react';
import classNames from 'classnames';
import { page, pages } from '@lib/lists';
import Pagination from '@common/Pagination';
import { TableFilters, createFilter } from './filters';
import { defaultRowKey } from './defaultRowKey';
import SortingIcon from './SortingIcon';
import EmptyState from './EmptyState';
import CollapsibleTableRow from './CollapsibleTableRow';
import Pagination from './Pagination';

const defaultCellRender = (content) => (
<p className="text-gray-900 whitespace-no-wrap">{content}</p>
Expand Down

0 comments on commit 8273c8a

Please sign in to comment.