Add test for TemporaryAlert (#81416)

This commit is contained in:
Fabrizio 2024-01-29 11:00:11 +01:00 committed by GitHub
parent 26fa921547
commit 20e2f3006b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 36 additions and 1 deletions

View File

@ -0,0 +1,28 @@
import { act, render, screen } from '@testing-library/react';
import React from 'react';
import { TemporaryAlert } from './TemporaryAlert';
describe('TemporaryAlert', () => {
beforeEach(() => {
jest.useFakeTimers();
});
afterEach(() => {
jest.runOnlyPendingTimers();
jest.useRealTimers();
});
it('full component life cycle', async () => {
render(<TemporaryAlert severity="error" text="" />);
expect(screen.queryByTestId('data-testid Alert error')).not.toBeInTheDocument();
render(<TemporaryAlert severity="error" text="Error message" />);
expect(screen.getByTestId('data-testid Alert error')).toBeInTheDocument();
expect(screen.getByText('Error message')).toBeInTheDocument();
act(() => jest.runAllTimers());
expect(screen.queryByTestId('data-testid Alert error')).not.toBeInTheDocument();
expect(screen.queryByText('Error message')).not.toBeInTheDocument();
});
});

View File

@ -27,7 +27,14 @@ const timeoutMap = {
['warning']: AlertTimeout.Warning,
};
export const TemporaryAlert = (props: { severity: AlertVariant; text: string }) => {
type AlertProps = {
// Severity of the alert. Controls the style of the alert (e.g., background color)
severity: AlertVariant;
// Displayed message. If set to empty string, the alert is not displayed
text: string;
};
export const TemporaryAlert = (props: AlertProps) => {
const style = getStyle(useTheme2());
const [visible, setVisible] = useState(false);
const [timer, setTimer] = useState<NodeJS.Timeout>();