import { createEvent, fireEvent, render, renderGlobalModal, screen, userEvent, } from 'sentry-test/reactTestingLibrary'; import Confirm from 'sentry/components/confirm'; import ModalStore from 'sentry/stores/modalStore'; describe('Confirm', function () { afterEach(() => { ModalStore.reset(); }); it('renders', function () { const mock = jest.fn(); const wrapper = render( ); expect(wrapper.container).toSnapshot(); }); it('renders custom confirm button & callbacks work', function () { const mock = jest.fn(); render( ( )} > ); renderGlobalModal(); userEvent.click(screen.getByTestId('trigger-btn')); const confirmBtn = screen.getByTestId('confirm-btn'); expect(confirmBtn).toBeInTheDocument(); expect(mock).not.toHaveBeenCalled(); userEvent.click(confirmBtn); expect(mock).toHaveBeenCalled(); }); it('renders custom cancel button & callbacks work', function () { const mock = jest.fn(); render( ( )} > ); renderGlobalModal(); userEvent.click(screen.getByTestId('trigger-btn')); const cancelBtn = screen.getByTestId('cancel-btn'); expect(cancelBtn).toBeInTheDocument(); expect(mock).not.toHaveBeenCalled(); userEvent.click(cancelBtn); expect(mock).toHaveBeenCalled(); }); it('clicking action button opens Modal', function () { const mock = jest.fn(); render( ); renderGlobalModal(); userEvent.click(screen.getByRole('button')); expect(screen.getByRole('dialog')).toBeInTheDocument(); }); it('clicks Confirm in modal and calls `onConfirm` callback', function () { const mock = jest.fn(); render( ); renderGlobalModal(); expect(mock).not.toHaveBeenCalled(); userEvent.click(screen.getByRole('button')); // Click "Confirm" button, should be last button userEvent.click(screen.getByText('Confirm')); expect(screen.getByRole('dialog')).toBeInTheDocument(); expect(mock).toHaveBeenCalled(); expect(mock.mock.calls).toHaveLength(1); }); it('can stop propagation on the event', function () { const mock = jest.fn(); render( ); expect(mock).not.toHaveBeenCalled(); const button = screen.getByRole('button'); const clickEvent = createEvent.click(button); clickEvent.stopPropagation = jest.fn(); fireEvent(button, clickEvent); expect(clickEvent.stopPropagation).toHaveBeenCalled(); }); });