1- import { Fragment } from 'react' ;
21import { render , screen } from '@testing-library/react' ;
32import userEvent from '@testing-library/user-event' ;
43
54import { ExpandableSection , ExpandableSectionVariant } from '../ExpandableSection' ;
6- import { ExpandableSectionToggle } from '../ExpandableSectionToggle ' ;
5+ import styles from '@patternfly/react-styles/css/components/ExpandableSection/expandable-section ' ;
76
87const props = { contentId : 'content-id' , toggleId : 'toggle-id' } ;
98
@@ -22,7 +21,7 @@ test('Renders ExpandableSection expanded', () => {
2221 expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
2322} ) ;
2423
25- test ( 'ExpandableSection onToggle called ' , async ( ) => {
24+ test ( 'Calls onToggle when clicked ' , async ( ) => {
2625 const mockfn = jest . fn ( ) ;
2726 const user = userEvent . setup ( ) ;
2827
@@ -32,6 +31,21 @@ test('ExpandableSection onToggle called', async () => {
3231 expect ( mockfn . mock . calls ) . toHaveLength ( 1 ) ;
3332} ) ;
3433
34+ test ( 'Does not call onToggle when not clicked' , async ( ) => {
35+ const mockfn = jest . fn ( ) ;
36+ const user = userEvent . setup ( ) ;
37+
38+ render (
39+ < >
40+ < ExpandableSection onToggle = { mockfn } > test </ ExpandableSection >
41+ < button onClick = { ( ) => { } } > Test clicker</ button >
42+ </ >
43+ ) ;
44+
45+ await user . click ( screen . getByRole ( 'button' , { name : 'Test clicker' } ) ) ;
46+ expect ( mockfn ) . not . toHaveBeenCalled ( ) ;
47+ } ) ;
48+
3549test ( 'Renders Uncontrolled ExpandableSection' , ( ) => {
3650 const { asFragment } = render (
3751 < ExpandableSection { ...props } toggleText = "Show More" >
@@ -42,20 +56,6 @@ test('Renders Uncontrolled ExpandableSection', () => {
4256 expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
4357} ) ;
4458
45- test ( 'Detached ExpandableSection renders successfully' , ( ) => {
46- const { asFragment } = render (
47- < Fragment >
48- < ExpandableSection isExpanded isDetached { ...props } >
49- test
50- </ ExpandableSection >
51- < ExpandableSectionToggle isExpanded direction = "up" { ...props } >
52- Toggle text
53- </ ExpandableSectionToggle >
54- </ Fragment >
55- ) ;
56- expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
57- } ) ;
58-
5959test ( 'Disclosure ExpandableSection' , ( ) => {
6060 const { asFragment } = render (
6161 < ExpandableSection { ...props } displaySize = "lg" isWidthLimited >
@@ -75,22 +75,22 @@ test('Renders ExpandableSection indented', () => {
7575 expect ( asFragment ( ) ) . toMatchSnapshot ( ) ;
7676} ) ;
7777
78- test ( ' Does not render with pf-m- truncate class when variant is not passed' , ( ) => {
78+ test ( ` Does not render with ${ styles . modifiers . truncate } class when variant is not passed` , ( ) => {
7979 render ( < ExpandableSection > test</ ExpandableSection > ) ;
8080
81- expect ( screen . getByText ( 'test' ) . parentElement ) . not . toHaveClass ( 'pf-m- truncate' ) ;
81+ expect ( screen . getByText ( 'test' ) . parentElement ) . not . toHaveClass ( styles . modifiers . truncate ) ;
8282} ) ;
8383
84- test ( ' Does not render with pf-m- truncate class when variant is not truncate' , ( ) => {
84+ test ( ` Does not render with ${ styles . modifiers . truncate } class when variant is not truncate` , ( ) => {
8585 render ( < ExpandableSection variant = { ExpandableSectionVariant . default } > test</ ExpandableSection > ) ;
8686
87- expect ( screen . getByText ( 'test' ) . parentElement ) . not . toHaveClass ( 'pf-m- truncate' ) ;
87+ expect ( screen . getByText ( 'test' ) . parentElement ) . not . toHaveClass ( styles . modifiers . truncate ) ;
8888} ) ;
8989
90- test ( ' Renders with pf-m- truncate class when variant is truncate' , ( ) => {
90+ test ( ` Renders with ${ styles . modifiers . truncate } class when variant is truncate` , ( ) => {
9191 render ( < ExpandableSection variant = { ExpandableSectionVariant . truncate } > test</ ExpandableSection > ) ;
9292
93- expect ( screen . getByText ( 'test' ) . parentElement ) . toHaveClass ( 'pf-m- truncate' ) ;
93+ expect ( screen . getByText ( 'test' ) . parentElement ) . toHaveClass ( styles . modifiers . truncate ) ;
9494} ) ;
9595
9696test ( 'Renders with value passed to contentId' , ( ) => {
@@ -129,3 +129,61 @@ test('Renders with ARIA attributes when contentId and toggleId are passed', () =
129129 expect ( wrapper ) . toContainHTML ( 'aria-labelledby="toggle-id"' ) ;
130130 expect ( wrapper ) . toContainHTML ( 'aria-controls="content-id"' ) ;
131131} ) ;
132+
133+ test ( `Does not render with class pf-m-detached by default` , ( ) => {
134+ render ( < ExpandableSection > Test content</ ExpandableSection > ) ;
135+
136+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-detached' ) ;
137+ } ) ;
138+
139+ test ( `Renders with class pf-m-detached when isDetached is true` , ( ) => {
140+ render ( < ExpandableSection isDetached > Test content</ ExpandableSection > ) ;
141+
142+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . toHaveClass ( 'pf-m-detached' ) ;
143+ } ) ;
144+
145+ test ( `Does not render with classes pf-m-expand-top nor pf-m-expand-bottom by default` , ( ) => {
146+ render ( < ExpandableSection > Test content</ ExpandableSection > ) ;
147+
148+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-expand-top' ) ;
149+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-expand-bottom' ) ;
150+ } ) ;
151+
152+ test ( `Does not render with classes pf-m-expand-top nor pf-m-expand-bottom when only isDetached is true` , ( ) => {
153+ render ( < ExpandableSection isDetached > Test content</ ExpandableSection > ) ;
154+
155+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-expand-top' ) ;
156+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-expand-bottom' ) ;
157+ } ) ;
158+
159+ test ( `Does not render with class pf-m-expand-top when only direction="up"` , ( ) => {
160+ render ( < ExpandableSection direction = "up" > Test content</ ExpandableSection > ) ;
161+
162+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-expand-top' ) ;
163+ } ) ;
164+
165+ test ( `Does not render with class pf-m-expand-bottom when only direction="down"` , ( ) => {
166+ render ( < ExpandableSection direction = "down" > Test content</ ExpandableSection > ) ;
167+
168+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . not . toHaveClass ( 'pf-m-expand-bottom' ) ;
169+ } ) ;
170+
171+ test ( `Renders with class pf-m-expand-top when isDetached is true and direction="up"` , ( ) => {
172+ render (
173+ < ExpandableSection isDetached direction = "up" >
174+ Test content
175+ </ ExpandableSection >
176+ ) ;
177+
178+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . toHaveClass ( 'pf-m-expand-top' ) ;
179+ } ) ;
180+
181+ test ( `Renders with class pf-m-expand-bottom when isDetached is true and direction="down"` , ( ) => {
182+ render (
183+ < ExpandableSection isDetached direction = "down" >
184+ Test content
185+ </ ExpandableSection >
186+ ) ;
187+
188+ expect ( screen . getByText ( 'Test content' ) . parentElement ) . toHaveClass ( 'pf-m-expand-bottom' ) ;
189+ } ) ;
0 commit comments