Skip to content

Commit b8cf1a2

Browse files
committed
Create unit tests for Toast component.
1 parent 23739ce commit b8cf1a2

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-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: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import React from 'react';
2+
import {
3+
act,
4+
reduxRender,
5+
screen,
6+
fireEvent,
7+
waitFor
8+
} from '../../../test-utils';
9+
import { setToastText, showToast } from '../actions/toast';
10+
import Toast from './Toast';
11+
12+
// TODO: update action calls after reducer refactor is merged.
13+
14+
describe(`Toast`, () => {
15+
it('is hidden by default', () => {
16+
reduxRender(<Toast />);
17+
expect(screen.queryByRole('status')).not.toBeInTheDocument();
18+
});
19+
20+
it('opens when an action is dispatched', async () => {
21+
const { store } = reduxRender(<Toast />);
22+
act(() => {
23+
store.dispatch(showToast(1500));
24+
store.dispatch(setToastText('Toast.SketchSaved'));
25+
});
26+
27+
const toast = screen.queryByRole('status');
28+
expect(toast).toBeVisible();
29+
expect(toast).toHaveTextContent('Sketch saved.');
30+
});
31+
32+
it('closes automatically after time', async () => {
33+
const { store } = reduxRender(<Toast />);
34+
act(() => {
35+
store.dispatch(showToast(100));
36+
store.dispatch(setToastText('Toast.SketchSaved'));
37+
});
38+
39+
expect(screen.queryByRole('status')).toBeInTheDocument();
40+
41+
await waitFor(() => {
42+
expect(screen.queryByRole('status')).not.toBeInTheDocument();
43+
});
44+
});
45+
46+
it('closes when "X" button is pressed', () => {
47+
reduxRender(<Toast />, {
48+
initialState: { toast: { isVisible: true, text: 'Hello World' } }
49+
});
50+
const button = screen.getByRole('button');
51+
fireEvent.click(button);
52+
expect(screen.queryByRole('status')).not.toBeInTheDocument();
53+
});
54+
});

0 commit comments

Comments
 (0)