rangeSlider.spec.tsx 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import {fireEvent, render, screen} from 'sentry-test/reactTestingLibrary';
  2. import RangeSlider from 'sentry/components/forms/controls/rangeSlider';
  3. describe('RangeSlider', function () {
  4. it('changes value / has right label', function () {
  5. render(<RangeSlider name="test" value={5} min={0} max={10} onChange={() => {}} />);
  6. expect(screen.getByRole('slider', {name: '5'})).toBeInTheDocument();
  7. fireEvent.change(screen.getByRole('slider'), {target: {value: '7'}});
  8. expect(screen.getByRole('slider', {name: '7'})).toBeInTheDocument();
  9. });
  10. it('can use formatLabel', function () {
  11. render(
  12. <RangeSlider
  13. name="test"
  14. value={5}
  15. min={0}
  16. max={10}
  17. formatLabel={value => (
  18. <div data-test-id="test">{value === 7 ? 'SEVEN!' : Number(value) + 1}</div>
  19. )}
  20. onChange={() => {}}
  21. />
  22. );
  23. expect(screen.getByTestId('test')).toHaveTextContent('6');
  24. fireEvent.change(screen.getByRole('slider'), {target: {value: '7'}});
  25. expect(screen.getByTestId('test')).toHaveTextContent('SEVEN!');
  26. });
  27. it('calls onChange', function () {
  28. const onChange = jest.fn();
  29. render(<RangeSlider name="test" value={5} min={0} max={10} onChange={onChange} />);
  30. expect(onChange).not.toHaveBeenCalled();
  31. fireEvent.change(screen.getByRole('slider'), {target: {value: '7'}});
  32. expect(onChange).toHaveBeenCalledWith(7, expect.anything());
  33. expect(onChange).toHaveBeenCalledTimes(1);
  34. });
  35. it('can provide a list of allowedValues', function () {
  36. const onChange = jest.fn();
  37. render(
  38. <RangeSlider
  39. name="test"
  40. value={1000}
  41. min={0}
  42. max={10}
  43. allowedValues={[0, 100, 1000, 10000, 20000]}
  44. onChange={onChange}
  45. />
  46. );
  47. // With `allowedValues` sliderValue will be the index to value in `allowedValues`
  48. expect(screen.getByRole('slider', {name: '1000'})).toBeInTheDocument();
  49. fireEvent.change(screen.getByRole('slider'), {target: {value: '0'}});
  50. expect(screen.getByRole('slider', {name: '0'})).toBeInTheDocument();
  51. // onChange will callback with a value from `allowedValues`
  52. expect(onChange).toHaveBeenCalledWith(0, expect.anything());
  53. });
  54. it('handles invalid values', function () {
  55. const onChange = jest.fn();
  56. render(
  57. <RangeSlider
  58. name="test"
  59. value={1000}
  60. min={0}
  61. max={10}
  62. allowedValues={[0, 100, 1000, 10000, 20000]} // support unsorted arrays?
  63. onChange={onChange}
  64. />
  65. );
  66. fireEvent.change(screen.getByRole('slider'), {target: {value: '-2'}});
  67. expect(screen.getByRole('slider', {name: '0'})).toBeInTheDocument();
  68. // onChange will callback with a value from `allowedValues`
  69. expect(onChange).toHaveBeenCalledWith(0, expect.anything());
  70. });
  71. });