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

test(Datagrid): add test coverage for new hook #3879

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
31 changes: 19 additions & 12 deletions packages/ibm-products/src/components/Datagrid/Datagrid.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ const ExpandedRow = ({ ...rest } = {}) => {
useExpandedRow
);

return <Datagrid datagridState={{ ...datagridState }} {...rest} />;
return <Datagrid datagridState={datagridState} {...rest} />;
};

const SelectItemsInAllPages = ({ ...rest } = {}) => {
Expand Down Expand Up @@ -1434,37 +1434,44 @@ describe(componentName, () => {
fireEvent.click(clickableRow);
});

function clickRow(rowNumber) {
async function clickRow(rowNumber, triggerAnotherExpander) {
const user = userEvent.setup({ delay: null });
const { click, hover, unhover } = user;
const rows = screen.getAllByRole('row');
const bodyRows = rows.filter(
(r) =>
!r.classList.contains('c4p--datagrid__head') &&
!r.classList.contains('c4p--datagrid__expanded-row')
!r.classList.contains(`${pkg.prefix}--datagrid__head`) &&
!r.classList.contains(`${pkg.prefix}--datagrid__expanded-row`)
);
const row = bodyRows[rowNumber];

const rowExpander = row.querySelector(`button[aria-label="Expand row"]`);
fireEvent.click(rowExpander);
const rowExpander = within(row).getByLabelText('Expand row');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL 😳

await click(rowExpander);

expect(row.nextElementSibling).toHaveClass(`${blockClass}__expanded-row`);
expect(row.nextElementSibling.textContent).toEqual(
`Content for ${rowNumber}`
);

fireEvent.mouseOver(row.nextElementSibling);
fireEvent.mouseLeave(row.nextElementSibling);
hover(row.nextElementSibling);
unhover(row.nextElementSibling);

const rowExpanderCollapse = row.querySelector(
`button[aria-label="Collapse row"]`
);
if (triggerAnotherExpander) {
const nextRow = bodyRows[rowNumber + 1];
const nextRowExpanderExpand =
within(nextRow).getByLabelText('Expand row');
fireEvent.click(nextRowExpanderExpand);
return;
}
const rowExpanderCollapse = within(row).getByLabelText('Collapse row');
fireEvent.click(rowExpanderCollapse);
}

it('should render with expandable rows and test by toggling the row open and closed', () => {
render(<ExpandedRow data-testid={dataTestId} />);
clickRow(1);
clickRow(4);
clickRow(8);
clickRow(8, true);
});

function hideSelectAll(rowNumber) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { useEffect } from 'react';
// flow.
export const useFocusRowExpander = ({
instance,
lastExpandedRowIndex,
lastExpandedRowIndex = 0,
blockClass,
activeElement,
}) => {
Expand All @@ -25,20 +25,18 @@ export const useFocusRowExpander = ({
}
const tableId = instance?.tableId;
const rowElements = document.querySelectorAll(`#${tableId} tbody tr`);
if (lastExpandedRowIndex) {
const rowElementsArray = Array.from(rowElements);
const activeRow = rowElementsArray.filter((r) => {
if (r.getAttribute('data-nested-row-id') === lastExpandedRowIndex) {
return r;
}
return null;
});
if (activeRow.length) {
const rowExpander = activeRow[0].querySelector(
`.${blockClass}__row-expander`
);
rowExpander.focus();
const rowElementsArray = Array.from(rowElements);
const activeRow = rowElementsArray.filter((r) => {
if (r.getAttribute('data-nested-row-id') === lastExpandedRowIndex) {
return r;
}
return null;
});
if (activeRow.length) {
const rowExpander = activeRow[0].querySelector(
`.${blockClass}__row-expander`
);
rowExpander.focus();
}
}, [
instance?.tableId,
Expand Down
Loading