confirm.spec.jsx 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. import React from 'react';
  2. import {shallow, mount} from 'enzyme';
  3. import Confirm from 'app/components/confirm';
  4. describe('Confirm', function() {
  5. it('renders', function() {
  6. let mock = jest.fn();
  7. let wrapper = shallow(
  8. <Confirm message="Are you sure?" onConfirm={mock}>
  9. <button>Confirm?</button>
  10. </Confirm>
  11. );
  12. expect(wrapper).toMatchSnapshot();
  13. });
  14. it('clicking action button opens Modal', function() {
  15. let mock = jest.fn();
  16. let wrapper = shallow(
  17. <Confirm message="Are you sure?" onConfirm={mock}>
  18. <button>Confirm?</button>
  19. </Confirm>
  20. );
  21. wrapper.find('button').simulate('click');
  22. expect(wrapper.find('Modal').prop('show')).toBe(true);
  23. });
  24. it('clicking action button twice causes Modal to end up closed', function() {
  25. let mock = jest.fn();
  26. let wrapper = shallow(
  27. <Confirm message="Are you sure?" onConfirm={mock}>
  28. <button>Confirm?</button>
  29. </Confirm>
  30. );
  31. let button = wrapper.find('button');
  32. button.simulate('click');
  33. button.simulate('click');
  34. expect(wrapper.find('Modal').prop('show')).toBe(false);
  35. });
  36. it('clicks Confirm in modal and calls `onConfirm` callback', function() {
  37. let mock = jest.fn();
  38. let wrapper = mount(
  39. <Confirm message="Are you sure?" onConfirm={mock}>
  40. <button>Confirm?</button>
  41. </Confirm>
  42. );
  43. expect(mock).not.toHaveBeenCalled();
  44. wrapper.find('button').simulate('click');
  45. wrapper.update();
  46. // Click "Confirm" button, should be last button
  47. wrapper
  48. .find('Button')
  49. .last()
  50. .simulate('click');
  51. expect(
  52. wrapper
  53. .find('Modal')
  54. .first()
  55. .prop('show')
  56. ).toBe(false);
  57. expect(mock).toHaveBeenCalled();
  58. expect(mock.mock.calls).toHaveLength(1);
  59. });
  60. });