confirmDelete.spec.jsx 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import {
  2. render,
  3. renderGlobalModal,
  4. screen,
  5. userEvent,
  6. } from 'sentry-test/reactTestingLibrary';
  7. import ConfirmDelete from 'sentry/components/confirmDelete';
  8. import ModalStore from 'sentry/stores/modalStore';
  9. describe('ConfirmDelete', function () {
  10. afterEach(() => {
  11. ModalStore.reset();
  12. });
  13. it('renders', function () {
  14. const mock = jest.fn();
  15. render(
  16. <ConfirmDelete message="Are you sure?" onConfirm={mock} confirmInput="CoolOrg">
  17. <button>Confirm?</button>
  18. </ConfirmDelete>
  19. );
  20. const globalModal = renderGlobalModal();
  21. userEvent.click(screen.getByRole('button'));
  22. // jest had an issue rendering root component snapshot so using ModalDialog instead
  23. expect(globalModal.container).toSnapshot();
  24. });
  25. it('confirm button is disabled and bypass prop is false when modal opens', function () {
  26. const mock = jest.fn();
  27. render(
  28. <ConfirmDelete message="Are you sure?" onConfirm={mock} confirmInput="CoolOrg">
  29. <button>Confirm?</button>
  30. </ConfirmDelete>
  31. );
  32. renderGlobalModal();
  33. userEvent.click(screen.getByRole('button'));
  34. expect(screen.getByRole('button', {name: 'Confirm'})).toBeDisabled();
  35. });
  36. it('confirm button stays disabled with non-matching input', function () {
  37. const mock = jest.fn();
  38. render(
  39. <ConfirmDelete message="Are you sure?" onConfirm={mock} confirmInput="CoolOrg">
  40. <button>Confirm?</button>
  41. </ConfirmDelete>
  42. );
  43. renderGlobalModal();
  44. userEvent.click(screen.getByRole('button'));
  45. userEvent.type(screen.getByPlaceholderText('CoolOrg'), 'Cool');
  46. expect(screen.getByRole('button', {name: 'Confirm'})).toBeDisabled();
  47. });
  48. it('confirm button is enabled when confirm input matches', function () {
  49. const mock = jest.fn();
  50. render(
  51. <ConfirmDelete message="Are you sure?" onConfirm={mock} confirmInput="CoolOrg">
  52. <button>Confirm?</button>
  53. </ConfirmDelete>
  54. );
  55. renderGlobalModal();
  56. userEvent.click(screen.getByRole('button'));
  57. userEvent.type(screen.getByPlaceholderText('CoolOrg'), 'CoolOrg');
  58. expect(screen.getByRole('button', {name: 'Confirm'})).toBeEnabled();
  59. userEvent.click(screen.getByRole('button', {name: 'Confirm'}));
  60. expect(mock).toHaveBeenCalled();
  61. expect(mock.mock.calls).toHaveLength(1);
  62. });
  63. });