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: [M3-8518] - Add unit tests for SelectableTableRow component #10890

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
5 changes: 5 additions & 0 deletions packages/manager/.changeset/pr-10890-tests-1725525454848.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@linode/manager": Tests
---

Add unit tests for SelectableTableRow component ([#10890](https://github.com/linode/manager/pull/10890))
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import { fireEvent, render } from '@testing-library/react';
import * as React from 'react';

import { wrapWithTableBody } from 'src/utilities/testHelpers';

import { TableCell } from '../TableCell';
import { SelectableTableRow } from './SelectableTableRow';

const cells = [
{ id: 1, label: 'child-cell-1' },
{ id: 2, label: 'child-cell-2' },
{ id: 3, label: 'child-cell-3' },
];

const defaultArgs = {
children: cells.map((cell) => (
<TableCell key={cell.id}>{cell.label}</TableCell>
)),
handleToggleCheck: vi.fn(),
isChecked: false,
};

const ariaLabel = 'Select all entities on page';

describe('SelectableTableRow', () => {
it('should render table row with checkbox and child content', () => {
const { getAllByText, getByRole, getByText } = render(
wrapWithTableBody(<SelectableTableRow {...defaultArgs} />)
);

const checkbox = getByRole('checkbox', {
name: ariaLabel,
});

expect(checkbox).toBeInTheDocument();

cells.forEach((cell) => {
expect(getByText(cell.label)).toBeInTheDocument();
});

expect(getAllByText(/child-cell-/i)).toHaveLength(cells.length);
});

it('should call handleToggleCheck on click', () => {
const { getByRole } = render(
wrapWithTableBody(<SelectableTableRow {...defaultArgs} />)
);

const checkbox = getByRole('checkbox', {
name: ariaLabel,
});

fireEvent.click(checkbox);
expect(defaultArgs.handleToggleCheck).toHaveBeenCalled();
});

it.each([
[true, 'checked'],
[false, 'unchecked'],
])(
'should correctly reflect the checkbox state when isChecked is %s',
(isChecked) => {
const { getByRole } = render(
wrapWithTableBody(
<SelectableTableRow {...defaultArgs} isChecked={isChecked} />
)
);

const checkbox = getByRole('checkbox', { name: ariaLabel });

if (isChecked) {
expect(checkbox).toBeChecked();
} else {
expect(checkbox).not.toBeChecked();
}
}
);
});
Loading