Skip to content

Commit fda6f24

Browse files
author
Eric Olkowski
committed
Updated tests
1 parent 8208226 commit fda6f24

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
@@ -35,7 +35,7 @@ export const AlertActionCloseButton: React.FunctionComponent<AlertActionCloseBut
3535
const handleOnClick = () => {
3636
if (hasAnimations) {
3737
getParentAlertGroupItem()?.classList.add(offstageRight);
38-
updateTransitionEnd(() => onClose());
38+
updateTransitionEnd(onClose);
3939
} else {
4040
onClose();
4141
}

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] = React.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,8 +1,8 @@
11
import React from 'react';
22
import { render, screen } from '@testing-library/react';
33
import userEvent from '@testing-library/user-event';
4-
54
import { AlertActionCloseButton } from '../AlertActionCloseButton';
5+
import { AlertGroupContext } from '../AlertGroupContext';
66
import { AlertContext } from '../AlertContext';
77

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

77+
test('Calls updateTransitionEnd with onClose when animations are enabled', async () => {
78+
const onClose = jest.fn();
79+
const user = userEvent.setup();
80+
const updateMock = jest.fn();
81+
render(
82+
<AlertGroupContext.Provider value={{ hasAnimations: true, updateTransitionEnd: updateMock }}>
83+
<AlertContext.Provider value={{ title: 'title', variantLabel: 'variantLabel' }}>
84+
<AlertActionCloseButton onClose={onClose} />
85+
</AlertContext.Provider>
86+
</AlertGroupContext.Provider>
87+
);
88+
89+
await user.click(screen.getByRole('button'));
90+
expect(updateMock).toHaveBeenCalledWith(onClose);
91+
expect(updateMock).toHaveBeenCalledTimes(1);
92+
});
93+
7794
test('Renders with an aria label composed with the title and variantLabel provided via a context by default', () => {
7895
render(
7996
<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,6 +1,6 @@
11
import * 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';
44
import userEvent from '@testing-library/user-event';
55

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

Comments
 (0)