confirm.spec.jsx 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import {
  2. createEvent,
  3. fireEvent,
  4. render,
  5. renderGlobalModal,
  6. screen,
  7. userEvent,
  8. } from 'sentry-test/reactTestingLibrary';
  9. import Confirm from 'sentry/components/confirm';
  10. import ModalStore from 'sentry/stores/modalStore';
  11. describe('Confirm', function () {
  12. afterEach(() => {
  13. ModalStore.reset();
  14. });
  15. it('renders', function () {
  16. const mock = jest.fn();
  17. const wrapper = render(
  18. <Confirm message="Are you sure?" onConfirm={mock}>
  19. <button>Confirm?</button>
  20. </Confirm>
  21. );
  22. expect(wrapper.container).toSnapshot();
  23. });
  24. it('renders custom confirm button & callbacks work', function () {
  25. const mock = jest.fn();
  26. render(
  27. <Confirm
  28. message="Are you sure?"
  29. onConfirm={mock}
  30. renderConfirmButton={({defaultOnClick}) => (
  31. <button data-test-id="confirm-btn" onClick={defaultOnClick}>
  32. Confirm Button
  33. </button>
  34. )}
  35. >
  36. <button data-test-id="trigger-btn">Confirm?</button>
  37. </Confirm>
  38. );
  39. renderGlobalModal();
  40. userEvent.click(screen.getByTestId('trigger-btn'));
  41. const confirmBtn = screen.getByTestId('confirm-btn');
  42. expect(confirmBtn).toBeInTheDocument();
  43. expect(mock).not.toHaveBeenCalled();
  44. userEvent.click(confirmBtn);
  45. expect(mock).toHaveBeenCalled();
  46. });
  47. it('renders custom cancel button & callbacks work', function () {
  48. const mock = jest.fn();
  49. render(
  50. <Confirm
  51. message="Are you sure?"
  52. onCancel={mock}
  53. renderCancelButton={({defaultOnClick}) => (
  54. <button data-test-id="cancel-btn" onClick={defaultOnClick}>
  55. Cancel Button
  56. </button>
  57. )}
  58. >
  59. <button data-test-id="trigger-btn">Confirm?</button>
  60. </Confirm>
  61. );
  62. renderGlobalModal();
  63. userEvent.click(screen.getByTestId('trigger-btn'));
  64. const cancelBtn = screen.getByTestId('cancel-btn');
  65. expect(cancelBtn).toBeInTheDocument();
  66. expect(mock).not.toHaveBeenCalled();
  67. userEvent.click(cancelBtn);
  68. expect(mock).toHaveBeenCalled();
  69. });
  70. it('clicking action button opens Modal', function () {
  71. const mock = jest.fn();
  72. render(
  73. <Confirm message="Are you sure?" onConfirm={mock}>
  74. <button>Confirm?</button>
  75. </Confirm>
  76. );
  77. renderGlobalModal();
  78. userEvent.click(screen.getByRole('button'));
  79. expect(screen.getByRole('dialog')).toBeInTheDocument();
  80. });
  81. it('clicks Confirm in modal and calls `onConfirm` callback', function () {
  82. const mock = jest.fn();
  83. render(
  84. <Confirm message="Are you sure?" onConfirm={mock}>
  85. <button>Confirm?</button>
  86. </Confirm>
  87. );
  88. renderGlobalModal();
  89. expect(mock).not.toHaveBeenCalled();
  90. userEvent.click(screen.getByRole('button'));
  91. // Click "Confirm" button, should be last button
  92. userEvent.click(screen.getByText('Confirm'));
  93. expect(screen.getByRole('dialog')).toBeInTheDocument();
  94. expect(mock).toHaveBeenCalled();
  95. expect(mock.mock.calls).toHaveLength(1);
  96. });
  97. it('can stop propagation on the event', function () {
  98. const mock = jest.fn();
  99. render(
  100. <Confirm message="Are you sure?" onConfirm={mock} stopPropagation>
  101. <button>Confirm?</button>
  102. </Confirm>
  103. );
  104. expect(mock).not.toHaveBeenCalled();
  105. const button = screen.getByRole('button');
  106. const clickEvent = createEvent.click(button);
  107. clickEvent.stopPropagation = jest.fn();
  108. fireEvent(button, clickEvent);
  109. expect(clickEvent.stopPropagation).toHaveBeenCalled();
  110. });
  111. });