Skip to content

Commit 3778d18

Browse files
authored
Merge pull request #2343 from lindapaiste/chore/toast-tests
Create tests for `Toast` component
2 parents 723dc85 + b3c3566 commit 3778d18

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

client/modules/IDE/components/Toast.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function Toast() {
1313
return null;
1414
}
1515
return (
16-
<section className="toast">
16+
<section className="toast" role="status" aria-live="polite">
1717
<p>{t(text)}</p>
1818
<button
1919
className="toast__close"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import React from 'react';
2+
import {
3+
act,
4+
reduxRender,
5+
screen,
6+
fireEvent,
7+
waitFor
8+
} from '../../../test-utils';
9+
import { showToast } from '../actions/toast';
10+
import Toast from './Toast';
11+
12+
describe(`Toast`, () => {
13+
it('is hidden by default', () => {
14+
reduxRender(<Toast />);
15+
expect(screen.queryByRole('status')).not.toBeInTheDocument();
16+
});
17+
18+
it('opens when an action is dispatched', async () => {
19+
const { store } = reduxRender(<Toast />);
20+
act(() => {
21+
store.dispatch(showToast('Toast.SketchSaved'));
22+
});
23+
24+
const toast = screen.queryByRole('status');
25+
expect(toast).toBeVisible();
26+
expect(toast).toHaveTextContent('Sketch saved.');
27+
});
28+
29+
it('closes automatically after time', async () => {
30+
const { store } = reduxRender(<Toast />);
31+
act(() => {
32+
store.dispatch(showToast('Toast.SketchSaved', 100));
33+
});
34+
35+
expect(screen.queryByRole('status')).toBeInTheDocument();
36+
37+
await waitFor(() => {
38+
expect(screen.queryByRole('status')).not.toBeInTheDocument();
39+
});
40+
});
41+
42+
it('closes when "X" button is pressed', () => {
43+
reduxRender(<Toast />, {
44+
initialState: { toast: { isVisible: true, text: 'Hello World' } }
45+
});
46+
const button = screen.getByRole('button');
47+
fireEvent.click(button);
48+
expect(screen.queryByRole('status')).not.toBeInTheDocument();
49+
});
50+
});

0 commit comments

Comments
 (0)