11import React from 'react' ;
2- import { render } from '@testing-library/react' ;
2+ import { render , screen } from '@testing-library/react' ;
33import '@testing-library/jest-dom/extend-expect' ;
44import { renderHook } from '@testing-library/react-hooks' ;
55
@@ -51,7 +51,7 @@ const Table = ({
5151 </ thead >
5252 < tbody >
5353 { rows . map ( ( row , idx ) => (
54- < tr key = { idx } >
54+ < tr role = "table-row" key = { idx } >
5555 { row . cells . map ( ( cell , idx ) => (
5656 < td key = { idx } > { cell . render ( ) } </ td >
5757 ) ) }
@@ -63,12 +63,12 @@ const Table = ({
6363} ;
6464
6565test ( 'Should render a basic table' , ( ) => {
66- const rtl = render ( < Table columns = { columns } data = { data } /> ) ;
66+ render ( < Table columns = { columns } data = { data } /> ) ;
6767
68- expect ( rtl . getByText ( 'Frodo' ) ) . toBeInTheDocument ( ) ;
69- expect ( rtl . getByText ( 'Baggins' ) ) . toBeInTheDocument ( ) ;
70- expect ( rtl . getByText ( 'Samwise' ) ) . toBeInTheDocument ( ) ;
71- expect ( rtl . getByText ( 'Gamgee' ) ) . toBeInTheDocument ( ) ;
68+ expect ( screen . getByText ( 'Frodo' ) ) . toBeInTheDocument ( ) ;
69+ expect ( screen . getByText ( 'Baggins' ) ) . toBeInTheDocument ( ) ;
70+ expect ( screen . getByText ( 'Samwise' ) ) . toBeInTheDocument ( ) ;
71+ expect ( screen . getByText ( 'Gamgee' ) ) . toBeInTheDocument ( ) ;
7272} ) ;
7373
7474const reverseData = [
@@ -89,6 +89,16 @@ test('Should be equal regardless of field order in data', () => {
8989 expect ( normalTl . asFragment ( ) ) . toEqual ( reverseTl . asFragment ( ) ) ;
9090} ) ;
9191
92+ test ( 'Should update table rows when data changes' , ( ) => {
93+ const { rerender } = render ( < Table columns = { columns } data = { data } /> ) ;
94+ expect ( screen . getAllByRole ( 'table-row' ) ) . toHaveLength ( 2 ) ;
95+
96+ let newData = [ ...data , { firstName : 'Bilbo' , lastName : 'Baggins' } ] ;
97+ rerender ( < Table columns = { columns } data = { newData } /> ) ;
98+
99+ expect ( screen . getAllByRole ( 'table-row' ) ) . toHaveLength ( 3 ) ;
100+ } ) ;
101+
92102const columnsWithRender : ColumnType < any > [ ] = [
93103 {
94104 name : 'firstName' ,
@@ -102,9 +112,9 @@ const columnsWithRender: ColumnType<any>[] = [
102112] ;
103113
104114test ( 'Should see custom row render HTML' , ( ) => {
105- const rtl = render ( < Table columns = { columnsWithRender } data = { data } /> ) ;
115+ render ( < Table columns = { columnsWithRender } data = { data } /> ) ;
106116
107- expect ( rtl . getAllByTestId ( 'first-name' ) ) . toHaveLength ( 2 ) ;
117+ expect ( screen . getAllByTestId ( 'first-name' ) ) . toHaveLength ( 2 ) ;
108118} ) ;
109119
110120const columnsWithColRender : ColumnType < any > [ ] = [
@@ -120,9 +130,9 @@ const columnsWithColRender: ColumnType<any>[] = [
120130] ;
121131
122132test ( 'Should see custom column render HTML' , ( ) => {
123- const rtl = render ( < Table columns = { columnsWithColRender } data = { data } /> ) ;
133+ render ( < Table columns = { columnsWithColRender } data = { data } /> ) ;
124134
125- expect ( rtl . getAllByTestId ( 'first-name' ) ) . toHaveLength ( 1 ) ;
135+ expect ( screen . getAllByTestId ( 'first-name' ) ) . toHaveLength ( 1 ) ;
126136} ) ;
127137
128138// to supress console error from test output
0 commit comments