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