|
| 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