);
it('should cycle through the sort states correctly', async () => {
const MockTable = () => {
const { getSortingDirection, toggleSort } = Sortable.useSortable();
return (
{
toggleSort('id');
}}
direction={getSortingDirection('id')}
title={`Sort id ${getSortingDirection('id')}`}
>
id
{
toggleSort('name');
}}
direction={getSortingDirection('name')}
title={`Sort name ${getSortingDirection('name')}`}
>
name
{mockData.map(renderRow)}
);
};
const { getByTitle } = customRender();
fireEvent.click(getByTitle('Sort id unsorted'));
await waitForElement(() => getByTitle('Sort id asc'));
fireEvent.click(getByTitle('Sort id asc'));
await waitForElement(() => getByTitle('Sort id desc'));
fireEvent.click(getByTitle('Sort id desc'));
await waitForElement(() => getByTitle('Sort id unsorted'));
});
it('should set initial sorting', () => {
const MockTable = () => {
const { getSortingDirection } = Sortable.useSortable('id', 'desc');
return (
id
name
{mockData.map(renderRow)}
);
};
const { getByTitle } = customRender();
getByTitle('Sort id desc');
getByTitle('Sort name unsorted');
});
it('should reset other sorts when I click on a new sort', async () => {
const MockTable = () => {
const { getSortingDirection, toggleSort } = Sortable.useSortable('id', 'desc');
return (
toggleSort('id')}
>
id
toggleSort('name')}
>
name
{mockData.map(renderRow)}
);
};
const { getByTitle } = customRender();
// Click on the name sort, which is currently unsorted.
fireEvent.click(getByTitle('Sort name unsorted'));
// Since id was previously sorted by `desc` order, it'll fallback to unsorted
// due to the fact `name` is now getting the sort.
await waitForElement(() => getByTitle('Sort id unsorted'));
// Name should've cycled through to the `asc` sort.
getByTitle('Sort name asc');
});
});
describe('Feature Bulk', () => {
// eslint-disable-next-line react/prop-types
const BulkTable: React.FC<{
innerGroup?: React.ReactNode;
selectedItemsText?: React.ReactNode | ((s: string | number) => React.ReactNode);
outerGroup?: React.ReactNode;
}> = ({ innerGroup, selectedItemsText, outerGroup }) => {
const {
isItemSelected,
toggleItem,
itemsSelectedCount,
isAllItemsSelected,
isPartiallySelected,
toggleAllItems,
} = BatchSelect.useBatchSelect(mockData);
const nextSelectedItems =
typeof selectedItemsText === 'function'
? selectedItemsText(itemsSelectedCount)
: selectedItemsText;
return (
);
};
it('should have rendered the select item checkboxes', async () => {
const { getByAltText } = customRender();
getByAltText('Select All Items');
getByAltText('Select item 1');
getByAltText('Select item 2');
getByAltText('Select item 3');
getByAltText('Select item 4');
});
it('should display a deselect all items checkbox if at least 1 item is selected', async () => {
const { getByAltText } = customRender();
fireEvent.click(getByAltText('Select item 1'));
await waitForElement(() => getByAltText('Deselect All Items'));
});
it('should change the headers checkbox text according to the number of items selected', async () => {
const { getByAltText, getByText } = customRender();
fireEvent.click(getByAltText('Select item 1'));
await waitForElement(() => getByText('1 selected item'));
fireEvent.click(getByAltText('Select item 2'));
await waitForElement(() => getByText('2 selected items'));
});
it('should be possible to pass in your own text that displays how many items are selected', async () => {
const { getByAltText, getByText } = customRender(
,
);
fireEvent.click(getByAltText('Select item 1'));
await waitForElement(() => getByText('whatever text you want'));
});
it('should be possible to pass in your own function to the selectedItemsText', async () => {
const { getByAltText, getByText } = customRender(
`you picked ${items} item`} />,
);
fireEvent.click(getByAltText('Select item 1'));
await waitForElement(() => getByText('you picked 1 item'));
});
it('should be possible to pass in your own react node to the selectedItemsText', async () => {
const { getByAltText, getByText, getByTestId } = customRender(
(
you have chosen {items} items
)}
/>,
);
fireEvent.click(getByAltText('Select item 1'));
await waitForElement(() => getByText('you have chosen 1 items'));
fireEvent.click(getByAltText('Select item 2'));
await waitForElement(() => getByText('you have chosen 2 items'));
getByTestId('my-custom-component');
});
it('should render our custom innerGroup and outerGroup when at least 1 or more items selected', async () => {
const { getByText, getByAltText } = customRender(
My Inner Group Item}
outerGroup={