1- import { render } from "@testing-library/react" ;
1+ import { fireEvent , render , waitFor } from "@testing-library/react" ;
22import DxcDataGrid from "./DataGrid" ;
33import { GridColumn , HierarchyGridRow } from "./types" ;
44
@@ -13,7 +13,7 @@ const columns: GridColumn[] = [
1313 } ,
1414 {
1515 key : "complete" ,
16- label : " % Complete" ,
16+ label : "% Complete" ,
1717 resizable : true ,
1818 sortable : true ,
1919 draggable : true ,
@@ -48,31 +48,234 @@ const expandableRows = [
4848 } ,
4949] ;
5050
51+ const hierarchyRows : HierarchyGridRow [ ] = [
52+ {
53+ name : "Root Node 1" ,
54+ value : "1" ,
55+ id : "a" ,
56+ childRows : [
57+ {
58+ name : "Child Node 1.1" ,
59+ value : "1.1" ,
60+ id : "aa" ,
61+ childRows : [
62+ {
63+ name : "Grandchild Node 1.1.1" ,
64+ value : "1.1.1" ,
65+ id : "aaa" ,
66+ } ,
67+ {
68+ name : "Grandchild Node 1.1.2" ,
69+ value : "1.1.2" ,
70+ id : "aab" ,
71+ } ,
72+ ] ,
73+ } ,
74+ {
75+ name : "Child Node 1.2" ,
76+ value : "1.2" ,
77+ id : "ab" ,
78+ } ,
79+ ] ,
80+ } ,
81+ {
82+ name : "Root Node 2" ,
83+ value : "2" ,
84+ id : "b" ,
85+ childRows : [
86+ {
87+ name : "Child Node 2.1" ,
88+ value : "2.1" ,
89+ id : "ba" ,
90+ childRows : [
91+ {
92+ name : "Grandchild Node 2.1.1" ,
93+ value : "2.1.1" ,
94+ id : "baa" ,
95+ } ,
96+ ] ,
97+ } ,
98+ {
99+ name : "Child Node 2.2" ,
100+ value : "2.2" ,
101+ id : "bb" ,
102+ } ,
103+ {
104+ name : "Child Node 2.3" ,
105+ value : "2.3" ,
106+ id : "bc" ,
107+ } ,
108+ ] ,
109+ } ,
110+ {
111+ name : "Root Node 3" ,
112+ value : "3" ,
113+ id : "c" ,
114+ childRows : [
115+ {
116+ name : "Child Node 3.1" ,
117+ value : "3.1" ,
118+ id : "cc" ,
119+ childRows : [
120+ {
121+ name : "Grandchild Node 3.1.1" ,
122+ value : "3.1.1" ,
123+ id : "ccc" ,
124+ } ,
125+ {
126+ name : "Grandchild Node 3.1.2" ,
127+ value : "3.1.2" ,
128+ id : "ccd" ,
129+ } ,
130+ ] ,
131+ } ,
132+ {
133+ name : "Child Node 3.2" ,
134+ value : "3.2" ,
135+ id : "cd" ,
136+ } ,
137+ ] ,
138+ } ,
139+ {
140+ name : "Root Node 4" ,
141+ value : "4" ,
142+ id : "d" ,
143+ childRows : [
144+ {
145+ name : "Child Node 4.1" ,
146+ value : "4.1" ,
147+ id : "da" ,
148+ childRows : [
149+ {
150+ name : "Grandchild Node 4.1.1" ,
151+ value : "4.1.1" ,
152+ id : "daa" ,
153+ } ,
154+ ] ,
155+ } ,
156+ {
157+ name : "Child Node 4.2" ,
158+ value : "4.2" ,
159+ id : "dd" ,
160+ } ,
161+ {
162+ name : "Child Node 4.3" ,
163+ value : "4.3" ,
164+ id : "de" ,
165+ } ,
166+ ] ,
167+ } ,
168+ {
169+ name : "Root Node 5" ,
170+ value : "5" ,
171+ id : "d" ,
172+ childRows : [
173+ {
174+ name : "Child Node 5.1" ,
175+ value : "5.1" ,
176+ id : "da" ,
177+ childRows : [
178+ {
179+ name : "Grandchild Node 5.1.1" ,
180+ value : "5.1.1" ,
181+ id : "daa" ,
182+ } ,
183+ ] ,
184+ } ,
185+ {
186+ name : "Child Node 5.2" ,
187+ value : "5.2" ,
188+ id : "dd" ,
189+ } ,
190+ {
191+ name : "Child Node 5.3" ,
192+ value : "5.3" ,
193+ id : "de" ,
194+ } ,
195+ ] ,
196+ } ,
197+ ] as HierarchyGridRow [ ] ;
198+
51199describe ( "Data grid component tests" , ( ) => {
52200 beforeAll ( ( ) => {
53201 ( global as any ) . CSS = {
54202 escape : ( str : string ) : string => str ,
55203 } ;
56204 window . HTMLElement . prototype . scrollIntoView = jest . fn ;
57205 } ) ;
206+
58207 test ( "Renders with correct content" , async ( ) => {
59- const { getByText, getAllByRole } = await render (
60- < DxcDataGrid columns = { columns } rows = { expandableRows } />
61- ) ;
208+ const { getByText, getAllByRole } = render ( < DxcDataGrid columns = { columns } rows = { expandableRows } /> ) ;
62209 expect ( getByText ( "46" ) ) . toBeTruthy ( ) ;
63210 const rows = getAllByRole ( "row" ) ;
64211 expect ( rows . length ) . toBe ( 5 ) ;
65212 } ) ;
66- // test("Content is sorted correctly", async () => {
67- // const { getByText, getAllByRole } = await render(<DxcDataGrid columns={columns} rows={expandableRows} />);
68- // expect(getByText("% Complete")).toBeTruthy();
69- // const headerCell = screen.getAllByRole("columnheader")[1];
70- // expect(getAllByRole("gridcell")[0].textContent).toBe("1");
71- // expect(headerCell.textContent).toBe(" % Complete");
72- // await fireEvent.click(headerCell);
73- // expect(headerCell.getAttribute("aria-sort")).toBe("ascending");
74- // expect(getByText("5")).toBeTruthy();
75- // // await waitFor(() => expect(getAllByRole("gridcell")[0].textContent).toBe("4"));
76- // //waitFor(() => expect(getAllByRole("gridcell").length).toBe(8));
77- // });
213+
214+ test ( "Renders hierarchy rows" , ( ) => {
215+ const onSelectRows = jest . fn ( ) ;
216+ const selectedRows = new Set < number | string > ( ) ;
217+ const { getAllByRole } = render (
218+ < DxcDataGrid
219+ columns = { columns }
220+ rows = { hierarchyRows }
221+ uniqueRowId = "id"
222+ selectable
223+ onSelectRows = { onSelectRows }
224+ selectedRows = { selectedRows }
225+ />
226+ ) ;
227+ const rows = getAllByRole ( "row" ) ;
228+ expect ( rows . length ) . toBe ( 5 ) ;
229+ } ) ;
230+
231+ test ( "Renders column headers" , ( ) => {
232+ const { getByText } = render ( < DxcDataGrid columns = { columns } rows = { expandableRows } /> ) ;
233+ expect ( getByText ( "ID" ) ) . toBeTruthy ( ) ;
234+ expect ( getByText ( "% Complete" ) ) . toBeTruthy ( ) ;
235+ } ) ;
236+
237+ test ( "Expands and collapses a row to show custom content" , async ( ) => {
238+ const { getAllByRole, getByText, queryByText } = render (
239+ < DxcDataGrid columns = { columns } rows = { expandableRows } uniqueRowId = "id" expandable />
240+ ) ;
241+ const buttons = getAllByRole ( "button" ) ;
242+ buttons [ 0 ] && fireEvent . click ( buttons [ 0 ] ) ;
243+ expect ( getByText ( "Custom content 1" ) ) . toBeTruthy ( ) ;
244+ buttons [ 0 ] && fireEvent . click ( buttons [ 0 ] ) ;
245+ expect ( queryByText ( "Custom content 1" ) ) . not . toBeTruthy ( ) ;
246+ } ) ;
247+
248+ test ( "Sorting by column works as expected" , async ( ) => {
249+ const { getAllByRole } = render (
250+ < DxcDataGrid columns = { columns } rows = { expandableRows } uniqueRowId = "id" expandable />
251+ ) ;
252+ const headers = getAllByRole ( "columnheader" ) ;
253+ const sortableHeader = headers [ 1 ] ;
254+
255+ sortableHeader && fireEvent . click ( sortableHeader ) ;
256+ expect ( sortableHeader ?. getAttribute ( "aria-sort" ) ) . toBe ( "ascending" ) ;
257+ await waitFor ( ( ) => {
258+ const cells = getAllByRole ( "gridcell" ) ;
259+ expect ( cells [ 1 ] ?. textContent ) . toBe ( "1" ) ;
260+ } ) ;
261+ sortableHeader && fireEvent . click ( sortableHeader ) ;
262+ expect ( sortableHeader ?. getAttribute ( "aria-sort" ) ) . toBe ( "descending" ) ;
263+ await waitFor ( ( ) => {
264+ const cells = getAllByRole ( "gridcell" ) ;
265+ expect ( cells [ 1 ] ?. textContent ) . toBe ( "5" ) ;
266+ } ) ;
267+ } ) ;
268+
269+ test ( "Expands multiple rows at once" , ( ) => {
270+ const { getAllByRole, getByText } = render (
271+ < DxcDataGrid columns = { columns } rows = { expandableRows } uniqueRowId = "id" expandable />
272+ ) ;
273+
274+ const buttons = getAllByRole ( "button" ) ;
275+ buttons [ 0 ] && fireEvent . click ( buttons [ 0 ] ) ;
276+ buttons [ 1 ] && fireEvent . click ( buttons [ 1 ] ) ;
277+
278+ expect ( getByText ( "Custom content 1" ) ) . toBeTruthy ( ) ;
279+ expect ( getByText ( "Custom content 2" ) ) . toBeTruthy ( ) ;
280+ } ) ;
78281} ) ;
0 commit comments