11/**
2- * Copyright IBM Corp. 2016, 2023
2+ * Copyright IBM Corp. 2016, 2025
33 *
44 * This source code is licensed under the Apache-2.0 license found in the
55 * LICENSE file in the root directory of this source tree.
66 */
77
8- import { render } from '@testing-library/react' ;
8+ import { render , screen } from '@testing-library/react' ;
99import React from 'react' ;
1010import { TableContainer } from '../' ;
1111
@@ -31,4 +31,80 @@ describe('TableContainer', () => {
3131 const { container } = render ( < TableContainer data-testid = "test" /> ) ;
3232 expect ( container . firstChild ) . toHaveAttribute ( 'data-testid' , 'test' ) ;
3333 } ) ;
34+
35+ describe ( 'Header' , ( ) => {
36+ it ( 'should render a header with only a `title`' , ( ) => {
37+ const title = 'Random title' ;
38+
39+ const { container } = render (
40+ < TableContainer title = { title } >
41+ < div > Child content</ div >
42+ </ TableContainer >
43+ ) ;
44+
45+ const headerEl = container . querySelector ( '[class*="data-table-header"]' ) ;
46+ const titleEl = headerEl . querySelector ( 'h4' ) ;
47+ const descriptionEl = headerEl . querySelector ( 'p' ) ;
48+
49+ expect ( headerEl ) . toBeInTheDocument ( ) ;
50+ expect ( headerEl . childElementCount ) . toBe ( 1 ) ;
51+ expect ( titleEl ) . toHaveTextContent ( title ) ;
52+ expect ( descriptionEl ) . toBeNull ( ) ;
53+ } ) ;
54+
55+ it ( 'should render a header with only a `description`' , ( ) => {
56+ const description = 'Random description' ;
57+
58+ const { container } = render (
59+ < TableContainer description = { description } >
60+ < div > Child content</ div >
61+ </ TableContainer >
62+ ) ;
63+
64+ const headerEl = container . querySelector ( '[class*="data-table-header"]' ) ;
65+ const titleEl = headerEl . querySelector ( 'h4' ) ;
66+ const descriptionEl = headerEl . querySelector ( 'p' ) ;
67+
68+ expect ( headerEl ) . toBeInTheDocument ( ) ;
69+ expect ( headerEl . childElementCount ) . toBe ( 1 ) ;
70+ expect ( descriptionEl ) . toHaveTextContent ( description ) ;
71+ expect ( titleEl ) . toBeNull ( ) ;
72+ } ) ;
73+
74+ it ( 'should render a header with both a `title` and a `description`' , ( ) => {
75+ const title = 'Random title' ;
76+ const description = 'Random description' ;
77+
78+ const { container } = render (
79+ < TableContainer title = { title } description = { description } >
80+ < div > Child content</ div >
81+ </ TableContainer >
82+ ) ;
83+
84+ const headerEl = container . querySelector ( '[class*="data-table-header"]' ) ;
85+ const titleEl = headerEl . querySelector ( 'h4' ) ;
86+ const descriptionEl = headerEl . querySelector ( 'p' ) ;
87+
88+ expect ( headerEl ) . toBeInTheDocument ( ) ;
89+ expect ( headerEl . childElementCount ) . toBe ( 2 ) ;
90+ expect ( titleEl ) . toHaveTextContent ( title ) ;
91+ expect ( headerEl . firstChild ) . toHaveTextContent ( title ) ;
92+ expect ( descriptionEl ) . toHaveTextContent ( description ) ;
93+ expect ( headerEl . lastChild ) . toHaveTextContent ( description ) ;
94+ } ) ;
95+
96+ it ( 'should not render a header when neither a `title` nor a `description` is provided' , ( ) => {
97+ const { container } = render (
98+ < TableContainer >
99+ < div data-testid = "child-content" > Child content</ div >
100+ </ TableContainer >
101+ ) ;
102+
103+ const headerEl = container . querySelector ( '[class*="data-table-header"]' ) ;
104+ const childContent = screen . getByTestId ( 'child-content' ) ;
105+
106+ expect ( childContent ) . toHaveTextContent ( 'Child content' ) ;
107+ expect ( headerEl ) . not . toBeInTheDocument ( ) ;
108+ } ) ;
109+ } ) ;
34110} ) ;
0 commit comments