Skip to content

Commit eedd916

Browse files
author
Eric Olkowski
committed
Updated tests
1 parent e95546b commit eedd916

File tree

5 files changed

+61
-10
lines changed

5 files changed

+61
-10
lines changed

packages/react-core/src/components/Alert/AlertActionCloseButton.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export const AlertActionCloseButton: React.FunctionComponent<AlertActionCloseBut
3434
const handleOnClick = () => {
3535
if (hasAnimations) {
3636
getParentAlertGroupItem()?.classList.add(offstageRight);
37-
updateTransitionEnd(() => onClose());
37+
updateTransitionEnd(onClose);
3838
} else {
3939
onClose();
4040
}

packages/react-core/src/components/Alert/AlertGroupContext.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as React from 'react';
22

33
interface AlertGroupContext {
44
hasAnimations?: boolean;
5-
updateTransitionEnd?: (onTransitionEnd: React.Dispatch<React.SetStateAction<() => void>>) => void;
5+
updateTransitionEnd?: (onTransitionEnd: () => void) => void;
66
}
77

88
export const AlertGroupContext = React.createContext<AlertGroupContext>({

packages/react-core/src/components/Alert/AlertGroupInline.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export const AlertGroupInline: React.FunctionComponent<AlertGroupProps> = ({
1616
...props
1717
}: AlertGroupProps) => {
1818
const [handleTransitionEnd, setHandleTransitionEnd] = useState(() => () => {});
19-
const updateTransitionEnd = (onTransitionEnd: React.Dispatch<React.SetStateAction<() => void>>) => {
19+
const updateTransitionEnd = (onTransitionEnd: () => void) => {
2020
setHandleTransitionEnd(() => onTransitionEnd);
2121
};
2222
return (

packages/react-core/src/components/Alert/__tests__/AlertActionCloseButton.test.tsx

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { render, screen } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
3-
43
import { AlertActionCloseButton } from '../AlertActionCloseButton';
4+
import { AlertGroupContext } from '../AlertGroupContext';
55
import { AlertContext } from '../AlertContext';
66

77
jest.mock('../../Button');
@@ -73,6 +73,23 @@ test('Calls the callback provided via onClose when clicked', async () => {
7373
expect(onCloseMock).toHaveBeenCalledTimes(1);
7474
});
7575

76+
test('Calls updateTransitionEnd with onClose when animations are enabled', async () => {
77+
const onClose = jest.fn();
78+
const user = userEvent.setup();
79+
const updateMock = jest.fn();
80+
render(
81+
<AlertGroupContext.Provider value={{ hasAnimations: true, updateTransitionEnd: updateMock }}>
82+
<AlertContext.Provider value={{ title: 'title', variantLabel: 'variantLabel' }}>
83+
<AlertActionCloseButton onClose={onClose} />
84+
</AlertContext.Provider>
85+
</AlertGroupContext.Provider>
86+
);
87+
88+
await user.click(screen.getByRole('button'));
89+
expect(updateMock).toHaveBeenCalledWith(onClose);
90+
expect(updateMock).toHaveBeenCalledTimes(1);
91+
});
92+
7693
test('Renders with an aria label composed with the title and variantLabel provided via a context by default', () => {
7794
render(
7895
<AlertContext.Provider value={{ title: 'title', variantLabel: 'variantLabel' }}>

packages/react-core/src/components/Alert/__tests__/AlertGroup.test.tsx

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { render, screen, act, waitFor, fireEvent } from '@testing-library/react';
1+
import { render, screen, fireEvent } from '@testing-library/react';
22
import userEvent from '@testing-library/user-event';
33

44
import { 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

Comments
 (0)