confirm.spec.jsx 3.8 KB

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