Skip to content

Commit 855d346

Browse files
committed
fix(alertgroup): fix tests
1 parent 175512d commit 855d346

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useEffect, useRef } from 'react';
1+
import { useEffect, useRef, useState } from 'react';
22
import * as ReactDOM from 'react-dom';
33
import { canUseDOM } from '../../helpers';
44
import { AlertGroupInline } from './AlertGroupInline';
@@ -41,6 +41,7 @@ export const AlertGroup: React.FunctionComponent<AlertGroupProps> = ({
4141
...props
4242
}: AlertGroupProps) => {
4343
const containerRef = useRef<HTMLElement | null>(null);
44+
const [isContainerReady, setIsContainerReady] = useState(false);
4445
const hasAnimations = useHasAnimations(localHasAnimations);
4546

4647
const getTargetElement = () => {
@@ -51,16 +52,19 @@ export const AlertGroup: React.FunctionComponent<AlertGroupProps> = ({
5152
};
5253

5354
useEffect(() => {
54-
if (isToast) {
55+
if (isToast && canUseDOM) {
5556
const container = document.createElement('div');
5657
const target = getTargetElement();
5758
containerRef.current = container;
5859
target.appendChild(container);
60+
setIsContainerReady(true);
5961

6062
return () => {
6163
if (containerRef.current) {
6264
target.removeChild(containerRef.current);
65+
containerRef.current = null;
6366
}
67+
setIsContainerReady(false);
6468
};
6569
}
6670
}, [isToast, appendTo]);
@@ -86,7 +90,7 @@ export const AlertGroup: React.FunctionComponent<AlertGroupProps> = ({
8690

8791
const container = containerRef.current;
8892

89-
if (!canUseDOM || !container) {
93+
if (!canUseDOM || !container || !isContainerReady) {
9094
return null;
9195
}
9296

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ test('Standard Alert Group is not a toast alert group', () => {
5656
expect(screen.getByText('alert title').parentElement).not.toHaveClass('pf-m-toast');
5757
});
5858

59-
test('Toast Alert Group contains expected modifier class', () => {
59+
test('Toast Alert Group contains expected modifier class', async () => {
6060
render(
6161
<AlertGroup isToast aria-label="group label">
6262
<Alert variant="warning" title="alert title" />
6363
</AlertGroup>
6464
);
6565

66-
expect(screen.getByLabelText('group label')).toHaveClass('pf-m-toast');
66+
// Wait for the portal to be created and rendered
67+
const alertGroup = await screen.findByLabelText('group label');
68+
expect(alertGroup).toHaveClass('pf-m-toast');
6769
});
6870

6971
test('Calls the callback set by updateTransitionEnd when transition ends and animations are enabled', async () => {
@@ -90,7 +92,8 @@ test('Calls the callback set by updateTransitionEnd when transition ends and ani
9092
</AlertGroup>
9193
);
9294

93-
await user.click(screen.getByLabelText('Close'));
95+
const closeButton = await screen.findByLabelText('Close');
96+
await user.click(closeButton);
9497
expect(mockCallback).not.toHaveBeenCalled();
9598
fireEvent.transitionEnd(screen.getByText('Test Alert').closest('.pf-v6-c-alert-group__item') as HTMLElement);
9699
expect(mockCallback).toHaveBeenCalled();
@@ -120,7 +123,8 @@ test('Does not call the callback set by updateTransitionEnd when transition ends
120123
</AlertGroup>
121124
);
122125

123-
await user.click(screen.getByLabelText('Close'));
126+
const closeButton = await screen.findByLabelText('Close');
127+
await user.click(closeButton);
124128
expect(mockCallback).toHaveBeenCalledTimes(1);
125129
// The transitionend event firing should not cause the callback to be called again
126130
fireEvent.transitionEnd(screen.getByText('Test Alert').closest('.pf-v6-c-alert-group__item') as HTMLElement);

0 commit comments

Comments
 (0)