1- import { render , screen , act , waitFor , fireEvent } from '@testing-library/react' ;
1+ import { render , screen , fireEvent } from '@testing-library/react' ;
22import userEvent from '@testing-library/user-event' ;
33
44import { Alert } from '../../Alert' ;
@@ -65,7 +65,38 @@ test('Toast Alert Group contains expected modifier class', () => {
6565
6666 expect ( screen . getByLabelText ( 'group label' ) ) . toHaveClass ( 'pf-m-toast' ) ;
6767} ) ;
68- test ( 'alertgroup closes when alerts are closed' , async ( ) => {
68+
69+ test ( 'Calls the callback set by updateTransitionEnd when transition ends and animations are enabled' , async ( ) => {
70+ window . matchMedia = ( query ) => ( {
71+ matches : false ,
72+ media : query ,
73+ onchange : null ,
74+ addListener : jest . fn ( ) , // deprecated
75+ removeListener : jest . fn ( ) , // deprecated
76+ addEventListener : jest . fn ( ) ,
77+ removeEventListener : jest . fn ( ) ,
78+ dispatchEvent : jest . fn ( )
79+ } ) ;
80+ const mockCallback = jest . fn ( ) ;
81+ const user = userEvent . setup ( ) ;
82+
83+ render (
84+ < AlertGroup isToast appendTo = { document . body } >
85+ < Alert
86+ isLiveRegion
87+ title = { 'Test Alert' }
88+ actionClose = { < AlertActionCloseButton aria-label = "Close" onClose = { mockCallback } /> }
89+ />
90+ </ AlertGroup >
91+ ) ;
92+
93+ await user . click ( screen . getByLabelText ( 'Close' ) ) ;
94+ expect ( mockCallback ) . not . toHaveBeenCalled ( ) ;
95+ fireEvent . transitionEnd ( screen . getByText ( 'Test Alert' ) . closest ( '.pf-v6-c-alert-group__item' ) as HTMLElement ) ;
96+ expect ( mockCallback ) . toHaveBeenCalled ( ) ;
97+ } ) ;
98+
99+ test ( 'Does not call the callback set by updateTransitionEnd when transition ends and animations are disabled' , async ( ) => {
69100 window . matchMedia = ( query ) => ( {
70101 matches : false ,
71102 media : query ,
@@ -76,19 +107,22 @@ test('alertgroup closes when alerts are closed', async () => {
76107 removeEventListener : jest . fn ( ) ,
77108 dispatchEvent : jest . fn ( )
78109 } ) ;
79- const onClose = jest . fn ( ) ;
110+ const mockCallback = jest . fn ( ) ;
80111 const user = userEvent . setup ( ) ;
112+
81113 render (
82- < AlertGroup className = "pf-v6-m-no-motion" isToast appendTo = { document . body } >
114+ < AlertGroup hasAnimations = { false } isToast appendTo = { document . body } >
83115 < Alert
84116 isLiveRegion
85117 title = { 'Test Alert' }
86- actionClose = { < AlertActionCloseButton aria-label = "Close" onClose = { onClose } /> }
118+ actionClose = { < AlertActionCloseButton aria-label = "Close" onClose = { mockCallback } /> }
87119 />
88120 </ AlertGroup >
89121 ) ;
90122
91123 await user . click ( screen . getByLabelText ( 'Close' ) ) ;
124+ expect ( mockCallback ) . toHaveBeenCalledTimes ( 1 ) ;
125+ // The transitionend event firing should not cause the callback to be called again
92126 fireEvent . transitionEnd ( screen . getByText ( 'Test Alert' ) . closest ( '.pf-v6-c-alert-group__item' ) as HTMLElement ) ;
93- expect ( onClose ) . toHaveBeenCalled ( ) ;
127+ expect ( mockCallback ) . toHaveBeenCalledTimes ( 1 ) ;
94128} ) ;
0 commit comments