From a98a7b1aaa3f74bff0a397bae05d1413f512c020 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 29 Oct 2024 22:22:01 +0100 Subject: [PATCH] [DataGridPro] Add list view tests (@KenanYusuf) (#15166) Co-authored-by: Kenan Yusuf --- .../src/tests/listView.DataGridPro.test.tsx | 98 +++++++++++++++++++ .../features/listView/useGridListView.tsx | 17 +++- 2 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 packages/x-data-grid-pro/src/tests/listView.DataGridPro.test.tsx diff --git a/packages/x-data-grid-pro/src/tests/listView.DataGridPro.test.tsx b/packages/x-data-grid-pro/src/tests/listView.DataGridPro.test.tsx new file mode 100644 index 000000000000..043ca3ab6179 --- /dev/null +++ b/packages/x-data-grid-pro/src/tests/listView.DataGridPro.test.tsx @@ -0,0 +1,98 @@ +import * as React from 'react'; +import { expect } from 'chai'; +import { createRenderer, screen } from '@mui/internal-test-utils'; +import { + DataGridPro, + DataGridProProps, + GridColDef, + GridListColDef, + GridRowsProp, +} from '@mui/x-data-grid-pro'; + +const rows: GridRowsProp = [{ id: '123567', title: 'test' }]; + +const columns: GridColDef[] = [{ field: 'id' }, { field: 'title' }]; + +const listColumn: GridListColDef = { + field: 'listColumn', + renderCell: (params) =>
Title: {params.row.title}
, +}; + +describe(' - List view', () => { + const { render } = createRenderer(); + + function Test(props: Partial) { + return ( +
+ +
+ ); + } + + it('should not render list column when list view is not enabled', () => { + render( +
+ +
, + ); + expect(screen.queryByTestId('list-column')).to.equal(null); + }); + + it('should render list column when list view is enabled', () => { + render( +
+ +
, + ); + expect(screen.getByTestId('list-column')).not.to.equal(null); + expect(screen.getByTestId('list-column')).to.have.text('Title: test'); + }); + + it('should render list column when `unstable_listView` prop updates', () => { + const { setProps } = render(); + expect(screen.queryByTestId('list-column')).to.equal(null); + + setProps({ unstable_listView: true }); + + expect(screen.getByTestId('list-column')).not.to.equal(null); + expect(screen.getByTestId('list-column')).to.have.text('Title: test'); + + setProps({ unstable_listView: false }); + + expect(screen.queryByTestId('list-column')).to.equal(null); + }); + + it('should update cell contents when the `renderCell` function changes', () => { + const { setProps } = render(); + + setProps({ + unstable_listColumn: { + ...listColumn, + renderCell: (params) =>
ID: {params.row.id}
, + }, + } as Partial); + + expect(screen.getByTestId('list-column')).to.have.text('ID: 123567'); + }); + + it('should warn if the `unstable_listColumn` prop is not provided when `unstable_listView` is enabled', () => { + expect(() => { + render( +
+ +
, + ); + }).toWarnDev( + [ + 'MUI X: The `unstable_listColumn` prop must be set if `unstable_listView` is enabled.', + 'To fix, pass a column definition to the `unstable_listColumn` prop, e.g. `{ field: "example", renderCell: (params) =>
{params.row.id}
}`.', + 'For more details, see https://mui.com/x/react-data-grid/list-view/', + ].join('\n'), + ); + }); +}); diff --git a/packages/x-data-grid/src/hooks/features/listView/useGridListView.tsx b/packages/x-data-grid/src/hooks/features/listView/useGridListView.tsx index 4f552b30eb49..246892724148 100644 --- a/packages/x-data-grid/src/hooks/features/listView/useGridListView.tsx +++ b/packages/x-data-grid/src/hooks/features/listView/useGridListView.tsx @@ -1,4 +1,5 @@ import * as React from 'react'; +import { warnOnce } from '@mui/x-internals/warning'; import type { GridListColDef } from '../../../models/colDef/gridColDef'; import { GridStateInitializer } from '../../utils/useGridInitializeState'; import { GridPrivateApiCommunity } from '../../../models/api/gridApiCommunity'; @@ -13,12 +14,14 @@ export const listViewStateInitializer: GridStateInitializer< Pick > = (state, props, apiRef) => ({ ...state, - listViewColumn: { ...props.unstable_listColumn, computedWidth: getListColumnWidth(apiRef) }, + listViewColumn: props.unstable_listColumn + ? { ...props.unstable_listColumn, computedWidth: getListColumnWidth(apiRef) } + : undefined, }); export function useGridListView( apiRef: React.MutableRefObject, - props: Pick, + props: Pick, ) { /* * EVENTS @@ -68,6 +71,16 @@ export function useGridListView( }); } }, [apiRef, props.unstable_listColumn]); + + React.useEffect(() => { + if (props.unstable_listView && !props.unstable_listColumn) { + warnOnce([ + 'MUI X: The `unstable_listColumn` prop must be set if `unstable_listView` is enabled.', + 'To fix, pass a column definition to the `unstable_listColumn` prop, e.g. `{ field: "example", renderCell: (params) =>
{params.row.id}
}`.', + 'For more details, see https://mui.com/x/react-data-grid/list-view/', + ]); + } + }, [props.unstable_listView, props.unstable_listColumn]); } function getListColumnWidth(apiRef: React.MutableRefObject) {