Skip to content

Commit

Permalink
chore: fix flaky toast tests (#1480)
Browse files Browse the repository at this point in the history
  • Loading branch information
scurker authored May 16, 2024
1 parent 83fae1b commit bbe8aaf
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/react/src/components/Toast/toast.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { setTimeout } from 'timers/promises';
import { render, screen, waitFor } from '@testing-library/react';
import Toast, { type ToastProps } from './';
import axe from '../../axe';
Expand Down Expand Up @@ -39,7 +40,7 @@ Object.entries(toastTypes).forEach(([key, value]) => {
</Toast>
);
const toast = screen.getByTestId('toast');
expect(toast).toHaveClass(`Toast Toast--${value} is--hidden`);
expect(toast).toHaveClass('Toast', `Toast--${value}`, 'is--hidden');
expect(toast).toHaveTextContent(testString);
});

Expand All @@ -51,7 +52,7 @@ Object.entries(toastTypes).forEach(([key, value]) => {
);
const toast = screen.getByTestId('toast');
expect(toast).toBeInTheDocument();
expect(toast).toHaveClass(`Toast Toast--${value} FadeIn--flex`);
expect(toast).toHaveClass('Toast', `Toast--${value}`, 'FadeIn--flex');
});

test('should transition from hidden to shown', async () => {
Expand All @@ -62,7 +63,9 @@ Object.entries(toastTypes).forEach(([key, value]) => {
);

expect(screen.getByTestId('toast')).toHaveClass(
'Toast Toast--info is--hidden'
'Toast',
'Toast--info',
'is--hidden'
);

rerender(
Expand All @@ -84,7 +87,9 @@ Object.entries(toastTypes).forEach(([key, value]) => {
);

expect(screen.getByTestId('toast')).toHaveClass(
'Toast Toast--info FadeIn--flex'
'Toast',
'Toast--info',
'FadeIn--flex'
);

rerender(
Expand All @@ -93,11 +98,14 @@ Object.entries(toastTypes).forEach(([key, value]) => {
</Toast>
);

setTimeout(() => {
// wait for animation tiemouts / async setState calls
await setTimeout(undefined, () => {
expect(screen.getByTestId('toast')).toHaveClass(
'Toast Toast--info is--hidden'
'Toast',
'Toast--info',
'is--hidden'
);
}, 300);
});
});

test(`renders the correct icon for toast type="${value}"`, async () => {
Expand All @@ -109,7 +117,8 @@ Object.entries(toastTypes).forEach(([key, value]) => {
const toast = screen.getByTestId('toast');
expect(toast).toBeInTheDocument();
expect(toast.childNodes[0].firstChild).toHaveClass(
`Icon Icon--${toastIcons[key as keyof typeof toastTypes]}`
'Icon',
`Icon--${toastIcons[key as keyof typeof toastTypes]}`
);
});

Expand Down Expand Up @@ -259,10 +268,11 @@ test('deactivates aria isolate on unmount', async () => {

await user.click(screen.getByRole('button'));

setTimeout(() => {
// wait for animation timeouts / async setState calls
await setTimeout(undefined, () => {
expect(isolator).toHaveBeenCalledTimes(1);
expect(isolator).toHaveBeenCalledWith(false);
}, 100);
});
});

test('renders children within the "Toast__message-content" div', async () => {
Expand Down

0 comments on commit bbe8aaf

Please sign in to comment.