numberInput.spec.tsx 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import NumberInput from 'sentry/components/numberInput';
  3. describe('NumberInput', function () {
  4. it('renders input', function () {
  5. render(<NumberInput value={5} aria-label="Test" />);
  6. expect(screen.getByRole('textbox')).toHaveValue('5');
  7. });
  8. it('responds to key presses', async function () {
  9. render(<NumberInput defaultValue={5} aria-label="Test" />);
  10. const input = screen.getByRole('textbox');
  11. await userEvent.type(input, '{ArrowDown}');
  12. expect(input).toHaveValue('4');
  13. await userEvent.type(input, '{ArrowUp>2}');
  14. expect(input).toHaveValue('6');
  15. });
  16. it('responds to increment/decrement button clicks', async function () {
  17. render(<NumberInput defaultValue={5} aria-label="Test" />);
  18. const input = screen.getByRole('textbox');
  19. await userEvent.click(screen.getByRole('button', {name: 'Decrease Test'}));
  20. expect(input).toHaveValue('4');
  21. await userEvent.click(screen.getByRole('button', {name: 'Increase Test'}));
  22. expect(input).toHaveValue('5');
  23. });
  24. it('forces min/max values', async function () {
  25. render(<NumberInput min={0} max={10} defaultValue={5} aria-label="Test" />);
  26. const input = screen.getByRole('textbox');
  27. // Should not be able to press arrow down to get below 0
  28. await userEvent.type(input, '{ArrowDown>5}');
  29. expect(input).toHaveValue('0');
  30. await userEvent.type(input, '{ArrowDown}');
  31. expect(input).toHaveValue('0');
  32. // Should not be able to press arrow up to get above 10
  33. await userEvent.type(input, '{ArrowUp>10}');
  34. expect(input).toHaveValue('10');
  35. await userEvent.type(input, '{ArrowUp}');
  36. expect(input).toHaveValue('10');
  37. // Type "100"
  38. await userEvent.type(input, '0');
  39. expect(input).toHaveValue('100');
  40. // Click outside the input, the value should be clamped down to 10
  41. await userEvent.click(document.body);
  42. expect(input).toHaveValue('10');
  43. });
  44. });