Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,26 @@ function ViewTable< Item >( {

const hasData = !! data?.length;

if ( ! hasData ) {
return (
<div
className={ clsx( {
'dataviews-loading': isLoading,
'dataviews-no-results': ! isLoading,
} ) }
id={ tableNoticeId }
>
{ isLoading ? (
<p>
<Spinner />
</p>
) : (
empty
) }
</div>
);
}

const titleField = fields.find( ( field ) => field.id === view.titleField );
const mediaField = fields.find( ( field ) => field.id === view.mediaField );
const descriptionField = fields.find(
Expand Down Expand Up @@ -536,7 +556,7 @@ function ViewTable< Item >( {
</tr>
</thead>
{ /* Render grouped data if groupBy is specified */ }
{ hasData && groupField && dataByGroup ? (
{ groupField && dataByGroup ? (
Array.from( dataByGroup.entries() ).map(
( [ groupName, groupItems ] ) => (
<tbody key={ `group-${ groupName }` }>
Expand Down Expand Up @@ -597,58 +617,48 @@ function ViewTable< Item >( {
)
) : (
<tbody>
{ hasData &&
data.map( ( item, index ) => (
<TableRow
key={ getItemId( item ) }
item={ item }
level={
view.showLevels &&
typeof getItemLevel === 'function'
? getItemLevel( item )
: undefined
}
hasBulkActions={ hasBulkActions }
actions={ actions }
fields={ fields }
id={ getItemId( item ) || index.toString() }
view={ view }
titleField={ titleField }
mediaField={ mediaField }
descriptionField={ descriptionField }
selection={ selection }
getItemId={ getItemId }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
renderItemLink={ renderItemLink }
isItemClickable={ isItemClickable }
isActionsColumnSticky={
! isHorizontalScrollEnd
}
posinset={
isInfiniteScroll ? index + 1 : undefined
}
/>
) ) }
{ data.map( ( item, index ) => (
<TableRow
key={ getItemId( item ) }
item={ item }
level={
view.showLevels &&
typeof getItemLevel === 'function'
? getItemLevel( item )
: undefined
}
hasBulkActions={ hasBulkActions }
actions={ actions }
fields={ fields }
id={ getItemId( item ) || index.toString() }
view={ view }
titleField={ titleField }
mediaField={ mediaField }
descriptionField={ descriptionField }
selection={ selection }
getItemId={ getItemId }
onChangeSelection={ onChangeSelection }
onClickItem={ onClickItem }
renderItemLink={ renderItemLink }
isItemClickable={ isItemClickable }
isActionsColumnSticky={
! isHorizontalScrollEnd
}
posinset={
isInfiniteScroll ? index + 1 : undefined
}
/>
) ) }
</tbody>
) }
</table>
<div
className={ clsx( {
'dataviews-loading': isLoading,
'dataviews-no-results': ! hasData && ! isLoading,
} ) }
id={ tableNoticeId }
>
{ ! hasData &&
( isLoading ? (
<p>
<Spinner />
</p>
) : (
empty
) ) }
{ hasData && isLoading && (
{ isLoading && (
<p className="dataviews-loading-more">
<Spinner />
</p>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/**
* External dependencies
*/
import { render, screen } from '@testing-library/react';

/**
* Internal dependencies
*/
import ViewTable from '../index';
import type {
ViewTable as ViewTableType,
NormalizedField,
} from '../../../../types';

type Item = { id: string; title: string };

const fields = [
{
id: 'title',
label: 'Title',
type: 'text',
render: ( { item }: { item: Item } ) => <span>{ item.title }</span>,
getValue: ( { item }: { item: Item } ) => item.title,
},
] as unknown as NormalizedField< Item >[];

const view: ViewTableType = {
type: 'table',
fields: [],
titleField: 'title',
};

const defaultProps = {
actions: [],
fields,
getItemId: ( item: Item ) => item.id,
isItemClickable: () => false,
onChangeView: jest.fn(),
onChangeSelection: jest.fn(),
selection: [] as string[],
setOpenedFilter: jest.fn(),
view,
empty: <p>No results</p>,
};

describe( 'ViewTable', () => {
it( 'should not render table headers when data is empty', () => {
render( <ViewTable { ...defaultProps } data={ [] } /> );

expect( screen.queryByRole( 'table' ) ).not.toBeInTheDocument();
expect( screen.queryByRole( 'columnheader' ) ).not.toBeInTheDocument();
expect( screen.getByText( 'No results' ) ).toBeInTheDocument();
} );

it( 'should render a loading spinner without table headers when loading with no data', () => {
render( <ViewTable { ...defaultProps } data={ [] } isLoading /> );

expect( screen.queryByRole( 'table' ) ).not.toBeInTheDocument();
expect( screen.queryByRole( 'columnheader' ) ).not.toBeInTheDocument();
} );

it( 'should render table headers when data is present', () => {
const data = [
{ id: '1', title: 'Item 1' },
{ id: '2', title: 'Item 2' },
];
render( <ViewTable { ...defaultProps } data={ data } /> );

expect( screen.getByRole( 'table' ) ).toBeInTheDocument();
expect( screen.getByText( 'Item 1' ) ).toBeInTheDocument();
expect( screen.getByText( 'Item 2' ) ).toBeInTheDocument();
} );
} );
Loading