confirm.spec.jsx 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import React from 'react';
  2. import {shallow, mountWithTheme} from 'sentry-test/enzyme';
  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('clicking action button opens Modal', function () {
  16. const mock = jest.fn();
  17. const wrapper = shallow(
  18. <Confirm message="Are you sure?" onConfirm={mock}>
  19. <button>Confirm?</button>
  20. </Confirm>,
  21. TestStubs.routerContext()
  22. );
  23. wrapper.find('button').simulate('click');
  24. expect(wrapper.find('Modal').prop('show')).toBe(true);
  25. });
  26. it('clicking action button twice causes Modal to end up closed', function () {
  27. const mock = jest.fn();
  28. const wrapper = shallow(
  29. <Confirm message="Are you sure?" onConfirm={mock}>
  30. <button>Confirm?</button>
  31. </Confirm>,
  32. TestStubs.routerContext()
  33. );
  34. const button = wrapper.find('button');
  35. button.simulate('click');
  36. button.simulate('click');
  37. expect(wrapper.find('Modal').prop('show')).toBe(false);
  38. });
  39. it('clicks Confirm in modal and calls `onConfirm` callback', function () {
  40. const mock = jest.fn();
  41. const wrapper = mountWithTheme(
  42. <Confirm message="Are you sure?" onConfirm={mock}>
  43. <button>Confirm?</button>
  44. </Confirm>,
  45. TestStubs.routerContext()
  46. );
  47. expect(mock).not.toHaveBeenCalled();
  48. wrapper.find('button').simulate('click');
  49. wrapper.update();
  50. // Click "Confirm" button, should be last button
  51. wrapper.find('Button').last().simulate('click');
  52. expect(wrapper.find('Modal').first().prop('show')).toBe(false);
  53. expect(mock).toHaveBeenCalled();
  54. expect(mock.mock.calls).toHaveLength(1);
  55. });
  56. it('can stop propagation on the event', function () {
  57. const mock = jest.fn();
  58. const wrapper = shallow(
  59. <Confirm message="Are you sure?" onConfirm={mock} stopPropagation>
  60. <button>Confirm?</button>
  61. </Confirm>,
  62. TestStubs.routerContext()
  63. );
  64. expect(mock).not.toHaveBeenCalled();
  65. const event = {
  66. stopPropagation: jest.fn(),
  67. };
  68. wrapper.find('button').simulate('click', event);
  69. wrapper.update();
  70. expect(event.stopPropagation).toHaveBeenCalledTimes(1);
  71. });
  72. });