confirm.spec.jsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import {mountWithTheme, shallow} from 'sentry-test/enzyme';
  2. import {mountGlobalModal} from 'sentry-test/modal';
  3. import Confirm from 'sentry/components/confirm';
  4. describe('Confirm', function () {
  5. it('renders', function () {
  6. const mock = jest.fn();
  7. const wrapper = shallow(
  8. <Confirm message="Are you sure?" onConfirm={mock}>
  9. <button>Confirm?</button>
  10. </Confirm>
  11. );
  12. expect(wrapper).toSnapshot();
  13. });
  14. it('renders custom confirm button & callbacks work', async function () {
  15. const mock = jest.fn();
  16. const wrapper = mountWithTheme(
  17. <Confirm
  18. message="Are you sure?"
  19. onConfirm={mock}
  20. renderConfirmButton={({defaultOnClick}) => (
  21. <button data-test-id="confirm-btn" onClick={defaultOnClick}>
  22. Confirm Button
  23. </button>
  24. )}
  25. >
  26. <button data-test-id="trigger-btn">Confirm?</button>
  27. </Confirm>
  28. );
  29. wrapper.find('button[data-test-id="trigger-btn"]').simulate('click');
  30. const modal = await mountGlobalModal();
  31. const confirmBtn = modal.find('button[data-test-id="confirm-btn"]');
  32. expect(confirmBtn.exists()).toBe(true);
  33. expect(mock).not.toHaveBeenCalled();
  34. confirmBtn.simulate('click');
  35. expect(mock).toHaveBeenCalled();
  36. });
  37. it('renders custom cancel button & callbacks work', async function () {
  38. const mock = jest.fn();
  39. const wrapper = mountWithTheme(
  40. <Confirm
  41. message="Are you sure?"
  42. onCancel={mock}
  43. renderCancelButton={({defaultOnClick}) => (
  44. <button data-test-id="cancel-btn" onClick={defaultOnClick}>
  45. Cancel Button
  46. </button>
  47. )}
  48. >
  49. <button data-test-id="trigger-btn">Confirm?</button>
  50. </Confirm>
  51. );
  52. wrapper.find('button[data-test-id="trigger-btn"]').simulate('click');
  53. const modal = await mountGlobalModal();
  54. const cancelBtn = modal.find('button[data-test-id="cancel-btn"]');
  55. expect(cancelBtn.exists()).toBe(true);
  56. expect(mock).not.toHaveBeenCalled();
  57. cancelBtn.simulate('click');
  58. expect(mock).toHaveBeenCalled();
  59. });
  60. it('clicking action button opens Modal', async function () {
  61. const mock = jest.fn();
  62. const wrapper = shallow(
  63. <Confirm message="Are you sure?" onConfirm={mock}>
  64. <button>Confirm?</button>
  65. </Confirm>
  66. );
  67. wrapper.find('button').simulate('click');
  68. const modal = await mountGlobalModal();
  69. expect(modal.find('GlobalModal[visible=true]').exists()).toBe(true);
  70. });
  71. it('clicks Confirm in modal and calls `onConfirm` callback', async function () {
  72. const mock = jest.fn();
  73. const wrapper = mountWithTheme(
  74. <Confirm message="Are you sure?" onConfirm={mock}>
  75. <button>Confirm?</button>
  76. </Confirm>
  77. );
  78. expect(mock).not.toHaveBeenCalled();
  79. wrapper.find('button').simulate('click');
  80. const modal = await mountGlobalModal();
  81. // Click "Confirm" button, should be last button
  82. modal.find('Button').last().simulate('click');
  83. await tick();
  84. modal.update();
  85. expect(modal.find('GlobalModal[visible=true]').exists()).toBe(false);
  86. expect(mock).toHaveBeenCalled();
  87. expect(mock.mock.calls).toHaveLength(1);
  88. });
  89. it('can stop propagation on the event', function () {
  90. const mock = jest.fn();
  91. const wrapper = shallow(
  92. <Confirm message="Are you sure?" onConfirm={mock} stopPropagation>
  93. <button>Confirm?</button>
  94. </Confirm>
  95. );
  96. expect(mock).not.toHaveBeenCalled();
  97. const event = {
  98. stopPropagation: jest.fn(),
  99. };
  100. wrapper.find('button').simulate('click', event);
  101. wrapper.update();
  102. expect(event.stopPropagation).toHaveBeenCalledTimes(1);
  103. });
  104. });