index.spec.jsx 2.2 KB

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