confirm.spec.jsx 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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).toMatchSnapshot();
  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
  52. .find('Button')
  53. .last()
  54. .simulate('click');
  55. expect(
  56. wrapper
  57. .find('Modal')
  58. .first()
  59. .prop('show')
  60. ).toBe(false);
  61. expect(mock).toHaveBeenCalled();
  62. expect(mock.mock.calls).toHaveLength(1);
  63. });
  64. it('can stop propagation on the event', function() {
  65. const mock = jest.fn();
  66. const wrapper = mountWithTheme(
  67. <Confirm message="Are you sure?" onConfirm={mock} stopPropagation>
  68. <button>Confirm?</button>
  69. </Confirm>,
  70. TestStubs.routerContext()
  71. );
  72. expect(mock).not.toHaveBeenCalled();
  73. const event = {
  74. stopPropagation: jest.fn(),
  75. };
  76. wrapper.find('button').simulate('click', event);
  77. wrapper.update();
  78. expect(event.stopPropagation).toHaveBeenCalledTimes(1);
  79. });
  80. });