editableText.spec.tsx 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import EditableText from 'sentry/components/editableText';
  3. describe('EditableText', function () {
  4. it('edit value and click outside of the component', function () {
  5. const handleChange = jest.fn();
  6. render(<EditableText value="foo" onChange={handleChange} />);
  7. // Click on the input
  8. userEvent.click(screen.getByText('foo'));
  9. // Clear input value
  10. userEvent.clear(screen.getByRole('textbox'));
  11. // Edit input value
  12. userEvent.type(screen.getByRole('textbox'), 'bar');
  13. // Click outside of the component
  14. userEvent.click(document.body);
  15. expect(handleChange).toHaveBeenCalledWith('bar');
  16. expect(screen.queryByText('foo')).not.toBeInTheDocument();
  17. expect(screen.getByText('bar')).toBeInTheDocument();
  18. });
  19. it('clear value and show error message', function () {
  20. const handleChange = jest.fn();
  21. render(<EditableText value="foo" onChange={handleChange} />);
  22. // Click on the input
  23. userEvent.click(screen.getByText('foo'));
  24. // Clear input value
  25. userEvent.clear(screen.getByRole('textbox'));
  26. // Press enter to submit the value
  27. userEvent.keyboard('{enter}');
  28. expect(handleChange).toHaveBeenCalledTimes(0);
  29. });
  30. it('displays a disabled value', function () {
  31. render(<EditableText value="foo" onChange={jest.fn()} isDisabled />);
  32. // Click on the input
  33. userEvent.click(screen.getByText('foo'));
  34. expect(screen.queryByRole('textbox')).not.toBeInTheDocument();
  35. });
  36. it('edit value and press escape', function () {
  37. const handleChange = jest.fn();
  38. render(<EditableText value="foo" onChange={handleChange} />);
  39. // Click on the input
  40. userEvent.click(screen.getByText('foo'));
  41. // Edit input value
  42. userEvent.type(screen.getByRole('textbox'), '-bar');
  43. // Press escape to cancel the value
  44. userEvent.keyboard('{esc}');
  45. expect(handleChange).toHaveBeenCalledTimes(0);
  46. expect(screen.getByText('foo')).toBeInTheDocument();
  47. });
  48. it('enforces a max length if provided', function () {
  49. render(<EditableText value="foo" onChange={jest.fn()} maxLength={4} />);
  50. // Click on the input
  51. userEvent.click(screen.getByText('foo'));
  52. // Edit input value
  53. userEvent.type(screen.getByRole('textbox'), '-bar');
  54. // Display a text with the max length of 4 caracters
  55. expect(screen.getByText('foo-')).toBeInTheDocument();
  56. });
  57. });