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

fix: datagrid empty state fixes #3528

Merged
merged 2 commits into from
Oct 3, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
66 changes: 17 additions & 49 deletions packages/ibm-products/src/components/Datagrid/Datagrid.test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable react/prop-types */
/**
* Copyright IBM Corp. 2022, 2023
*
Expand Down Expand Up @@ -238,7 +239,6 @@ const DatagridActions = (datagridState) => {
);
};

// eslint-disable-next-line react/prop-types
const DatagridPagination = ({ state, setPageSize, gotoPage, rows }) => {
const updatePagination = ({ page, pageSize }) => {
setPageSize(pageSize);
Expand All @@ -247,20 +247,16 @@ const DatagridPagination = ({ state, setPageSize, gotoPage, rows }) => {

return (
<Pagination
// eslint-disable-next-line react/prop-types
page={state.pageIndex + 1} // react-table is zero-based
// eslint-disable-next-line react/prop-types
pageSize={state.pageSize}
// eslint-disable-next-line react/prop-types
pageSizes={state.pageSizes || [10, 20, 30, 40, 50]}
// eslint-disable-next-line react/prop-types
totalItems={rows.length}
onChange={updatePagination}
/>
);
};

const EmptyUsage = ({ ...rest } = {}) => {
const EmptyUsage = ({ emptyStateType, ...rest } = {}) => {
const columns = React.useMemo(() => defaultHeader, []);
const [data] = useState(makeData(0));
const emptyStateTitle = 'Empty State Title';
Expand All @@ -275,6 +271,7 @@ const EmptyUsage = ({ ...rest } = {}) => {
emptyStateTitle,
emptyStateDescription,
emptyStateSize,
emptyStateType,
illustrationTheme,
DatagridActions,
DatagridBatchActions,
Expand Down Expand Up @@ -440,7 +437,6 @@ const range = (len) => {
return arr;
};

// eslint-disable-next-line react/prop-types
const Wrapper = ({ children }) => (
<div
style={{
Expand Down Expand Up @@ -1080,55 +1076,27 @@ describe(componentName, () => {

//Empty State
it('renders an empty table', async () => {
render(<EmptyUsage data-testid={dataTestId}></EmptyUsage>);
const { rerender } = render(<EmptyUsage data-testid={dataTestId} />);
screen.getByText('Empty State Title');
screen.getByText('Description test explaining why this card is empty.');
expect(
screen.getByRole('table').getElementsByTagName('tbody')[0].className
).toEqual('c4p--datagrid__empty-state-body');
document.querySelector(`.${pkg.prefix}--empty-state-type--noData`)
Copy link
Member

Choose a reason for hiding this comment

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

We should avoid document.querySelector if possible

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'll take another look

).toBeInTheDocument();

rerender(<EmptyUsage emptyStateType="error" />);
expect(
screen
.getByRole('table')
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr').length
).toEqual(1);
document.querySelector(`.${pkg.prefix}--empty-state-type--error`)
).toBeInTheDocument();

rerender(<EmptyUsage emptyStateType="notFound" />);
expect(
screen
.getByRole('table')
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr')[0]
.getElementsByTagName('td')[0].textContent
).toBeNull;
document.querySelector(`.${pkg.prefix}--empty-state-type--notFound`)
).toBeInTheDocument();

rerender(<EmptyUsage emptyStateType="12345" />);
expect(
screen
.getByRole('table')
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr')[0]
.getElementsByTagName('td')[0]
.getElementsByTagName('div')[0]
.getElementsByTagName('svg')[0]
).toBeDefined();

expect(
screen
.getByRole('table')
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr')[0]
.getElementsByTagName('td')[0]
.getElementsByTagName('div')[0]
.getElementsByTagName('h3')[0].textContent
).toEqual('Empty State Title');

expect(
screen
.getByRole('table')
.getElementsByTagName('tbody')[0]
.getElementsByTagName('tr')[0]
.getElementsByTagName('td')[0]
.getElementsByTagName('div')[0]
.getElementsByTagName('p')[0].textContent
).toEqual('Description test explaining why this card is empty.');
document.querySelector(`.${pkg.prefix}--empty-state-type--default`)
).toBeInTheDocument();
});

it('Initial Load', async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
NoDataEmptyState,
ErrorEmptyState,
NotFoundEmptyState,
EmptyState,
} from '../../EmptyStates';

const blockClass = `${pkg.prefix}--datagrid`;
Expand All @@ -38,6 +39,8 @@ const DatagridEmptyBody = (datagridState) => {
link: emptyStateLink,
};

const validEmptyStates = ['error', 'noData', 'notFound'];

return (
<TableBody
{...getTableBodyProps({ role: false })}
Expand All @@ -48,14 +51,20 @@ const DatagridEmptyBody = (datagridState) => {
colSpan={headers.length}
className={`${blockClass}__empty-state-cell`}
>
{emptyStateType === 'error' && (
<ErrorEmptyState {...emptyStateProps} />
)}
{emptyStateType === 'noData' && (
<NoDataEmptyState {...emptyStateProps} />
)}
{emptyStateType === 'notFound' && (
<NotFoundEmptyState {...emptyStateProps} />
{validEmptyStates.includes(emptyStateType) ? (
<>
{emptyStateType === 'error' && (
<ErrorEmptyState {...emptyStateProps} />
)}
{emptyStateType === 'noData' && (
<NoDataEmptyState {...emptyStateProps} />
)}
{emptyStateType === 'notFound' && (
<NotFoundEmptyState {...emptyStateProps} />
)}
</>
) : (
<EmptyState {...emptyStateProps} />
)}
</TableCell>
</TableRow>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export let EmptyState = React.forwardRef(
// Pass through any other property values as HTML attributes.
...rest
}
className={cx(blockClass, className, {
className={cx(blockClass, `${blockClass}-type--default`, className, {
[`${blockClass}-position--${illustrationPosition}`]: !!illustration,
})}
ref={ref}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export let ErrorEmptyState = React.forwardRef(
className={cx(
blockClass,
className,
`${blockClass}-position--${illustrationPosition}`
`${blockClass}-position--${illustrationPosition}`,
`${blockClass}-type--error`
)}
ref={ref}
{...getDevtoolsProps(componentName)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export let NoDataEmptyState = React.forwardRef(
className={cx(
blockClass,
className,
`${blockClass}-position--${illustrationPosition}`
`${blockClass}-position--${illustrationPosition}`,
`${blockClass}-type--noData`
)}
ref={ref}
{...getDevtoolsProps(componentName)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export let NoTagsEmptyState = React.forwardRef(
className={cx(
blockClass,
className,
`${blockClass}-position--${illustrationPosition}`
`${blockClass}-position--${illustrationPosition}`,
`${blockClass}-type--noTags`
)}
ref={ref}
{...getDevtoolsProps(componentName)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export let NotFoundEmptyState = React.forwardRef(
className={cx(
blockClass,
className,
`${blockClass}-position--${illustrationPosition}`
`${blockClass}-position--${illustrationPosition}`,
`${blockClass}-type--notFound`
)}
ref={ref}
{...getDevtoolsProps(componentName)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export let NotificationsEmptyState = React.forwardRef(
className={cx(
blockClass,
className,
`${blockClass}-position--${illustrationPosition}`
`${blockClass}-position--${illustrationPosition}`,
`${blockClass}-type--notifications`
)}
ref={ref}
{...getDevtoolsProps(componentName)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ export let UnauthorizedEmptyState = React.forwardRef(
className={cx(
blockClass,
className,
`${blockClass}-position--${illustrationPosition}`
`${blockClass}-position--${illustrationPosition}`,
`${blockClass}-type--unauthorized`
)}
ref={ref}
{...getDevtoolsProps(componentName)}
Expand Down
Loading