globalModal.spec.jsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. import {
  2. render,
  3. renderGlobalModal,
  4. screen,
  5. userEvent,
  6. waitForElementToBeRemoved,
  7. } from 'sentry-test/reactTestingLibrary';
  8. import {closeModal, openModal} from 'sentry/actionCreators/modal';
  9. import GlobalModal from 'sentry/components/globalModal';
  10. import ModalStore from 'sentry/stores/modalStore';
  11. describe('GlobalModal', function () {
  12. beforeEach(() => {
  13. ModalStore.reset();
  14. });
  15. afterEach(() => {
  16. ModalStore.teardown();
  17. });
  18. it('uses actionCreators to open and close Modal', async function () {
  19. renderGlobalModal();
  20. openModal(() => <div data-test-id="modal-test">Hi</div>);
  21. expect(screen.getByTestId('modal-test')).toBeInTheDocument();
  22. closeModal();
  23. await waitForElementToBeRemoved(screen.queryByTestId('modal-test'));
  24. expect(screen.queryByTestId('modal-test')).not.toBeInTheDocument();
  25. });
  26. it('calls onClose handler when modal is clicked out of', function () {
  27. render(<GlobalModal />);
  28. const closeSpy = jest.fn();
  29. openModal(
  30. ({Header}) => (
  31. <div id="modal-test">
  32. <Header closeButton>Header</Header>Hi
  33. </div>
  34. ),
  35. {onClose: closeSpy}
  36. );
  37. userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  38. expect(closeSpy).toHaveBeenCalled();
  39. });
  40. it('calls onClose handler when closeModal prop is called', function () {
  41. render(<GlobalModal />);
  42. const closeSpy = jest.fn();
  43. openModal(({closeModal: cm}) => <button onClick={cm}>Yo</button>, {
  44. onClose: closeSpy,
  45. });
  46. userEvent.click(screen.getByRole('button', {name: 'Yo'}));
  47. expect(closeSpy).toHaveBeenCalled();
  48. });
  49. it('calls ignores click out when the allowClickClose option is false', async function () {
  50. render(
  51. <div>
  52. <div data-test-id="outside-test">Hello</div>
  53. <GlobalModal />
  54. </div>
  55. );
  56. openModal(
  57. ({Header}) => (
  58. <div data-test-id="modal-test">
  59. <Header closeButton>Header</Header>Hi
  60. </div>
  61. ),
  62. {allowClickClose: false}
  63. );
  64. expect(screen.getByTestId('modal-test')).toBeInTheDocument();
  65. userEvent.click(screen.getByTestId('outside-test'));
  66. expect(screen.getByTestId('modal-test')).toBeInTheDocument();
  67. userEvent.click(screen.getByRole('button', {name: 'Close Modal'}));
  68. await waitForElementToBeRemoved(screen.queryByTestId('modal-test'));
  69. expect(screen.queryByTestId('modal-test')).not.toBeInTheDocument();
  70. });
  71. });