inputGroup.spec.tsx 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {Button} from 'sentry/components/button';
  3. import {InputGroup} from 'sentry/components/inputGroup';
  4. describe('InputGroup', function () {
  5. it('renders input', function () {
  6. const {container} = render(
  7. <InputGroup>
  8. <InputGroup.Input value="Search" onChange={() => {}} />
  9. </InputGroup>
  10. );
  11. expect(screen.getByRole('textbox')).toBeInTheDocument();
  12. expect(screen.getByRole('textbox')).toHaveDisplayValue('Search');
  13. expect(container).toSnapshot();
  14. });
  15. it('renders disabled input', function () {
  16. const {container} = render(
  17. <InputGroup>
  18. <InputGroup.Input disabled />
  19. </InputGroup>
  20. );
  21. expect(screen.getByRole('textbox')).toBeDisabled();
  22. expect(container).toSnapshot();
  23. });
  24. it('renders leading elements', function () {
  25. const {container} = render(
  26. <InputGroup>
  27. <InputGroup.LeadingItems>
  28. <Button>Leading Button</Button>
  29. </InputGroup.LeadingItems>
  30. <InputGroup.Input />
  31. </InputGroup>
  32. );
  33. // Leading button is rendered
  34. expect(screen.getByTestId('input-leading-items')).toBeInTheDocument();
  35. expect(screen.getByRole('button', {name: 'Leading Button'})).toBeInTheDocument();
  36. // Focus moves first to leading button and then to input
  37. userEvent.tab();
  38. expect(screen.getByRole('button', {name: 'Leading Button'})).toHaveFocus();
  39. userEvent.tab();
  40. expect(screen.getByRole('textbox')).toHaveFocus();
  41. expect(container).toSnapshot();
  42. });
  43. it('renders trailing elements', function () {
  44. const {container} = render(
  45. <InputGroup>
  46. <InputGroup.Input />
  47. <InputGroup.TrailingItems>
  48. <Button>Trailing Button</Button>
  49. </InputGroup.TrailingItems>
  50. </InputGroup>
  51. );
  52. // Trailing button is rendered
  53. expect(screen.getByTestId('input-trailing-items')).toBeInTheDocument();
  54. expect(screen.getByRole('button', {name: 'Trailing Button'})).toBeInTheDocument();
  55. // Focus moves first to input and then to trailing button
  56. userEvent.tab();
  57. expect(screen.getByRole('textbox')).toHaveFocus();
  58. userEvent.tab();
  59. expect(screen.getByRole('button', {name: 'Trailing Button'})).toHaveFocus();
  60. expect(container).toSnapshot();
  61. });
  62. });