@@ -3,6 +3,7 @@ import { render, fireEvent, act } from "@testing-library/react";
33import userEvent from "@testing-library/user-event" ;
44import DxcResultsetTable from "./ResultsetTable" ;
55import { ActionCellsPropsType } from "../table/types" ;
6+ import DxcCheckbox from "../checkbox/Checkbox" ;
67
78// Mocking DOMRect for Radix Primitive Popover
89( global as any ) . globalThis = global ;
@@ -178,54 +179,43 @@ const rows = [
178179 } ,
179180 ] ,
180181] ;
181- const rows2 = [
182+
183+ const columnsWithCheckbox = [
184+ { displayValue : "Id" , isSortable : true } ,
185+ { displayValue : "Checkbox" , isSortable : false } ,
186+ { displayValue : "Name" , isSortable : false } ,
187+ { displayValue : "City" , isSortable : false } ,
188+ ] ;
189+
190+ const rowsWithCheckbox = [
182191 [
192+ { displayValue : "001" , sortValue : "001" } ,
183193 {
184- displayValue : "546" ,
185- sortValue : "465" ,
186- } ,
187- {
188- displayValue : "OtherValue" ,
189- sortValue : "OtherValue" ,
190- } ,
191- {
192- displayValue : "OtherValue" ,
193- sortValue : "OtherValue" ,
194+ displayValue : < DxcCheckbox size = "fillParent" defaultChecked = { true } /> ,
194195 } ,
196+ { displayValue : "Peter" } ,
197+ { displayValue : "Miami" } ,
195198 ] ,
196199 [
200+ { displayValue : "002" , sortValue : "002" } ,
197201 {
198- displayValue : "978" ,
199- sortValue : "465" ,
200- } ,
201- {
202- displayValue : "OtherValue" ,
203- sortValue : "OtherValue" ,
204- } ,
205- {
206- displayValue : "OtherValue" ,
207- sortValue : "OtherValue" ,
208- } ,
209- {
210- displayValue : "" ,
202+ displayValue : < DxcCheckbox size = "fillParent" /> ,
211203 } ,
204+ { displayValue : "Louis" } ,
205+ { displayValue : "London" } ,
212206 ] ,
213207 [
208+ { displayValue : "003" , sortValue : "003" } ,
214209 {
215- displayValue : "678" ,
216- sortValue : "344" ,
217- } ,
218- {
219- displayValue : "OtherValue" ,
220- sortValue : "OtherValue" ,
221- } ,
222- {
223- displayValue : "OtherValue" ,
224- sortValue : "OtherValue" ,
210+ displayValue : < DxcCheckbox size = "fillParent" /> ,
225211 } ,
212+ { displayValue : "Lana" } ,
213+ { displayValue : "Amsterdam" } ,
226214 ] ,
227215] ;
228216
217+ const rows2 = [ ...rows ] . slice ( 0 , - 1 ) ;
218+
229219describe ( "Resultset table component tests" , ( ) => {
230220 test ( "Resultset table rendered correctly" , ( ) => {
231221 const { getByText } = render ( < DxcResultsetTable columns = { columns } rows = { rows } itemsPerPage = { 3 } /> ) ;
@@ -256,7 +246,7 @@ describe("Resultset table component tests", () => {
256246 window . HTMLElement . prototype . scrollIntoView = ( ) => { } ;
257247 window . HTMLElement . prototype . scrollTo = ( ) => { } ;
258248 const { getByText, getAllByRole } = render (
259- < DxcResultsetTable columns = { columns } showGoToPage rows = { rows } itemsPerPage = { 3 } />
249+ < DxcResultsetTable columns = { columns } showGoToPage rows = { rows } itemsPerPage = { 3 } /> ,
260250 ) ;
261251 expect ( getByText ( "Peter" ) ) . toBeTruthy ( ) ;
262252 expect ( getByText ( "Louis" ) ) . toBeTruthy ( ) ;
@@ -294,16 +284,57 @@ describe("Resultset table component tests", () => {
294284 expect ( component . queryByText ( "Cosmin" ) ) . not . toBeTruthy ( ) ;
295285 } ) ;
296286
297- test ( "Resultset table change rows should go to first page" , ( ) => {
298- const { queryByText, rerender } = render ( < DxcResultsetTable columns = { columns } rows = { rows } itemsPerPage = { 3 } /> ) ;
287+ test ( "Resultset table should go one page back when removing the last page data" , ( ) => {
288+ const { getAllByRole, queryByText, rerender } = render (
289+ < DxcResultsetTable columns = { columns } rows = { rows } itemsPerPage = { 3 } /> ,
290+ ) ;
291+ expect ( queryByText ( "1 to 3 of 10" ) ) . toBeTruthy ( ) ;
292+ const lastButton = getAllByRole ( "button" ) [ 4 ] ;
299293 expect ( queryByText ( "Peter" ) ) . toBeTruthy ( ) ;
294+ fireEvent . click ( lastButton ) ;
295+ expect ( queryByText ( "10 to 10 of 10" ) ) . toBeTruthy ( ) ;
300296 rerender ( < DxcResultsetTable columns = { columns } rows = { rows2 } itemsPerPage = { 3 } /> ) ;
301- expect ( queryByText ( "1 to 3 of 3" ) ) . toBeTruthy ( ) ;
297+ expect ( queryByText ( "7 to 9 of 9" ) ) . toBeTruthy ( ) ;
298+ } ) ;
299+
300+ test ( "Resultset table shouldn't go one page back when there is data left in the last page" , ( ) => {
301+ const { getAllByRole, queryByText, rerender } = render (
302+ < DxcResultsetTable columns = { columns } rows = { rows } itemsPerPage = { 2 } /> ,
303+ ) ;
304+ expect ( queryByText ( "1 to 2 of 10" ) ) . toBeTruthy ( ) ;
305+ const lastButton = getAllByRole ( "button" ) [ 4 ] ;
306+ expect ( queryByText ( "Peter" ) ) . toBeTruthy ( ) ;
307+ fireEvent . click ( lastButton ) ;
308+ expect ( queryByText ( "9 to 10 of 10" ) ) . toBeTruthy ( ) ;
309+ rerender ( < DxcResultsetTable columns = { columns } rows = { rows2 } itemsPerPage = { 2 } /> ) ;
310+ expect ( queryByText ( "9 to 9 of 9" ) ) . toBeTruthy ( ) ;
311+ } ) ;
312+
313+ test ( "Resultset table uncontrolled components maintain its value when sorting" , async ( ) => {
314+ const { getAllByRole } = render (
315+ < DxcResultsetTable columns = { columnsWithCheckbox } rows = { rowsWithCheckbox } itemsPerPage = { 3 } /> ,
316+ ) ;
317+ const columnHeader = getAllByRole ( "columnheader" ) [ 0 ] ;
318+ const sortButton = getAllByRole ( "button" ) [ 0 ] ;
319+
320+ expect ( getAllByRole ( "checkbox" ) [ 0 ] . getAttribute ( "aria-checked" ) ) . toBe ( "true" ) ;
321+
322+ expect ( columnHeader . getAttribute ( "aria-sort" ) ) . toBe ( "none" ) ;
323+
324+ fireEvent . click ( sortButton ) ;
325+
326+ expect ( columnHeader . getAttribute ( "aria-sort" ) ) . toBe ( "ascending" ) ;
327+
328+ fireEvent . click ( sortButton ) ;
329+
330+ expect ( columnHeader . getAttribute ( "aria-sort" ) ) . toBe ( "descending" ) ;
331+
332+ expect ( getAllByRole ( "checkbox" ) [ 0 ] . getAttribute ( "aria-checked" ) ) . toBe ( "false" ) ;
302333 } ) ;
303334
304335 test ( "Resultset table change itemsPerPage should go to first page" , ( ) => {
305336 const { getAllByRole } = render (
306- < DxcResultsetTable columns = { columns } rows = { rows } itemsPerPage = { 3 } itemsPerPageOptions = { [ 2 , 3 ] } />
337+ < DxcResultsetTable columns = { columns } rows = { rows } itemsPerPage = { 3 } itemsPerPageOptions = { [ 2 , 3 ] } /> ,
307338 ) ;
308339 const lastButton = getAllByRole ( "button" ) [ 4 ] ;
309340 expect ( getAllByRole ( "row" ) . length - 1 ) . toEqual ( 3 ) ;
@@ -320,7 +351,7 @@ describe("Resultset table component tests", () => {
320351 test ( "Resultset table with ActionsCell" , ( ) => {
321352 const onSelectOption = jest . fn ( ) ;
322353 const onClick = jest . fn ( ) ;
323- const actions : ActionCellsPropsType [ ' actions' ] = [
354+ const actions : ActionCellsPropsType [ " actions" ] = [
324355 {
325356 title : "icon1" ,
326357 onClick : onSelectOption ,
@@ -344,7 +375,7 @@ describe("Resultset table component tests", () => {
344375 title : "icon2" ,
345376 onClick : onClick ,
346377 } ,
347- ] ;
378+ ] ;
348379 const actionRows = [
349380 [
350381 {
@@ -362,7 +393,7 @@ describe("Resultset table component tests", () => {
362393 ] ,
363394 ] ;
364395 const { getAllByRole, getByRole, getByText } = render (
365- < DxcResultsetTable columns = { columns } rows = { actionRows } itemsPerPage = { 3 } />
396+ < DxcResultsetTable columns = { columns } rows = { actionRows } itemsPerPage = { 3 } /> ,
366397 ) ;
367398 const dropdown = getAllByRole ( "button" ) [ 2 ] ;
368399 act ( ( ) => {
0 commit comments