123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- import {Fragment} from 'react';
- import {
- render,
- renderGlobalModal,
- screen,
- userEvent,
- } from 'sentry-test/reactTestingLibrary';
- import ModalStore from 'sentry/stores/modalStore';
- import AdminConfirmationModal from 'admin/components/adminConfirmationModal';
- describe('Admin confirmation modal', function () {
- const mockOnConfirm = jest.fn();
- const mockOnCancel = jest.fn();
- beforeEach(() => {
- ModalStore.reset();
- jest.clearAllMocks();
- });
- const setTicketURL = async (url: string) => {
- await userEvent.clear(screen.getByRole('textbox', {name: 'TicketURL'}));
- await userEvent.type(screen.getByRole('textbox', {name: 'TicketURL'}), url);
- };
- const setNotes = async (notes: string) => {
- await userEvent.clear(screen.getByRole('textbox', {name: 'Notes'}));
- await userEvent.type(screen.getByRole('textbox', {name: 'Notes'}), notes);
- };
- it('renders default fields', async function () {
- render(
- <AdminConfirmationModal onConfirm={mockOnConfirm} onCancel={mockOnCancel}>
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- expect(screen.getByRole('textbox', {name: 'TicketURL'})).toBeInTheDocument();
- expect(screen.getByRole('textbox', {name: 'Notes'})).toBeInTheDocument();
- });
- it('obeys showAuditFields prop', async function () {
- render(
- <AdminConfirmationModal
- onConfirm={mockOnConfirm}
- onCancel={mockOnCancel}
- showAuditFields={false}
- >
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- expect(screen.queryByRole('textbox', {name: 'TicketURL'})).not.toBeInTheDocument();
- expect(screen.queryByRole('textbox', {name: 'Notes'})).not.toBeInTheDocument();
- });
- it('renders text content', async function () {
- render(
- <AdminConfirmationModal
- onConfirm={mockOnConfirm}
- onCancel={mockOnCancel}
- modalSpecificContent="random text"
- >
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- expect(screen.getByRole('dialog')).toHaveTextContent('random text');
- });
- it('renders jsx content', async function () {
- render(
- <AdminConfirmationModal
- onConfirm={mockOnConfirm}
- onCancel={mockOnCancel}
- modalSpecificContent={<div data-test-id="top-half-div">Hello</div>}
- >
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- expect(screen.getByTestId('top-half-div')).toBeInTheDocument();
- });
- it('renders content given by a function', async function () {
- const testFunc = (handlers: any) => (
- <Fragment>
- <button onClick={handlers.close}>close</button>
- <button onClick={handlers.confirm}>confirm</button>
- </Fragment>
- );
- render(
- <AdminConfirmationModal
- onConfirm={mockOnConfirm}
- onCancel={mockOnCancel}
- renderModalSpecificContent={testFunc}
- >
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- const {waitForModalToHide} = renderGlobalModal();
-
- await userEvent.click(screen.getByRole('button', {name: 'confirm'}));
- expect(mockOnConfirm).toHaveBeenCalled();
- await waitForModalToHide();
-
- await userEvent.click(screen.getByRole('button'));
-
- await userEvent.click(screen.getByRole('button', {name: 'close'}));
- await waitForModalToHide();
- });
- it('passes notes and ticket url to parent', async function () {
- render(
- <AdminConfirmationModal onConfirm={mockOnConfirm} onCancel={mockOnCancel}>
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- await setTicketURL('http://im.a.cool.website');
- await setNotes('notes');
- await userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
- expect(mockOnConfirm).toHaveBeenCalledWith({
- ticketURL: 'http://im.a.cool.website',
- notes: 'notes',
- });
- });
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- it('disables confirm button on initial render', async function () {
- render(
- <AdminConfirmationModal onConfirm={mockOnConfirm} onCancel={mockOnCancel}>
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- expect(screen.getByRole('button', {name: 'Confirm'})).toBeEnabled();
- });
- it('disables confirm button on URL error', async function () {
- render(
- <AdminConfirmationModal onConfirm={mockOnConfirm} onCancel={mockOnCancel}>
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- await setTicketURL('1invalid');
-
- await userEvent.click(screen.getByRole('textbox', {name: 'Notes'}));
- expect(screen.getByRole('button', {name: 'Confirm'})).toBeDisabled();
- });
- it('enables confirm button on valid URL input', async function () {
- render(
- <AdminConfirmationModal onConfirm={mockOnConfirm} onCancel={mockOnCancel}>
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- await setTicketURL('http://www.goodwebsite.com');
- expect(screen.getByRole('button', {name: 'Confirm'})).toBeEnabled();
- });
- it('handles url change', async function () {
- render(
- <AdminConfirmationModal onConfirm={mockOnConfirm} onCancel={mockOnCancel}>
- <button>Open Modal</button>
- </AdminConfirmationModal>
- );
- await userEvent.click(screen.getByRole('button'));
- renderGlobalModal();
- await setTicketURL('1invalid');
-
- await userEvent.click(screen.getByRole('textbox', {name: 'Notes'}));
- expect(screen.getByText('Invalid ticket URL')).toBeInTheDocument();
- await setTicketURL('https://sentry.zendesk.com/agent/tickets/1234');
-
- await userEvent.click(screen.getByRole('textbox', {name: 'Notes'}));
- expect(screen.queryByText('Invalid ticket URL')).not.toBeInTheDocument();
- });
- });
|