index.spec.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';
  2. import {Slider} from 'sentry/components/slider';
  3. describe('Slider', function () {
  4. it('renders', function () {
  5. render(<Slider label="Test" min={0} max={10} step={1} defaultValue={5} />);
  6. expect(screen.getByRole('group', {name: 'Test'})).toBeInTheDocument();
  7. expect(screen.getByRole('status')).toHaveTextContent('5'); // <output /> element
  8. const slider = screen.getByRole('slider', {name: 'Test'});
  9. expect(slider).toBeInTheDocument();
  10. expect(slider).toHaveValue('5');
  11. expect(slider).toHaveAttribute('min', '0');
  12. expect(slider).toHaveAttribute('max', '10');
  13. });
  14. it('renders without label/output', function () {
  15. render(<Slider aria-label="Test" min={0} max={10} step={1} defaultValue={5} />);
  16. expect(screen.queryByRole('status')).not.toBeInTheDocument();
  17. });
  18. it('calls onChange/onChangeEnd', async function () {
  19. const onChangeMock = jest.fn();
  20. const onChangeEndMock = jest.fn();
  21. render(
  22. <Slider
  23. label="Test"
  24. min={5}
  25. max={10}
  26. defaultValue={5}
  27. onChange={onChangeMock}
  28. onChangeEnd={onChangeEndMock}
  29. />
  30. );
  31. // To focus on the slider, we should call the focus() method. The slider input element
  32. // is visually hidden and only rendered for screen-reader & keyboard accessibility —
  33. // users can't actually click on it.
  34. screen.getByRole('slider', {name: 'Test'}).focus();
  35. await userEvent.keyboard('{ArrowRight}');
  36. expect(onChangeMock).toHaveBeenCalledWith(6);
  37. // onChangeEnd is called after the user stops dragging, but we can't simulate mouse
  38. // drags with RTL. Here we're just checking that it's called after a key press.
  39. expect(onChangeEndMock).toHaveBeenCalledWith(6);
  40. });
  41. it('works with larger step size', async function () {
  42. const onChangeEndMock = jest.fn();
  43. render(
  44. <Slider
  45. label="Test"
  46. min={0}
  47. max={10}
  48. step={5}
  49. defaultValue={5}
  50. onChangeEnd={onChangeEndMock}
  51. />
  52. );
  53. // To focus on the slider, we should call the focus() method. The slider input element
  54. // is visually hidden and only rendered for screen-reader & keyboard accessibility —
  55. // users can't actually click on it.
  56. screen.getByRole('slider', {name: 'Test'}).focus();
  57. await userEvent.keyboard('{ArrowRight}');
  58. expect(onChangeEndMock).toHaveBeenCalledWith(10);
  59. });
  60. it('supports advanced keyboard navigation', async function () {
  61. const onChangeEndMock = jest.fn();
  62. render(
  63. <Slider
  64. label="Test"
  65. min={5}
  66. max={100}
  67. defaultValue={5}
  68. onChangeEnd={onChangeEndMock}
  69. />
  70. );
  71. // To focus on the slider, we should call the focus() method. The slider input element
  72. // is visually hidden and only rendered for screen-reader & keyboard accessibility —
  73. // users can't actually click on it.
  74. screen.getByRole('slider', {name: 'Test'}).focus();
  75. // Pressing Arrow Right/Left increases/decreases value by 1
  76. await userEvent.keyboard('{ArrowRight}');
  77. expect(onChangeEndMock).toHaveBeenCalledWith(6);
  78. await userEvent.keyboard('{ArrowLeft}');
  79. expect(onChangeEndMock).toHaveBeenCalledWith(5);
  80. // Pressing Arrow Right/Left while holding Shift increases/decreases value by 10
  81. await userEvent.keyboard('{Shift>}{ArrowRight}{/Shift}');
  82. expect(onChangeEndMock).toHaveBeenCalledWith(15);
  83. await userEvent.keyboard('{Shift>}{ArrowLeft}{/Shift}');
  84. expect(onChangeEndMock).toHaveBeenCalledWith(5);
  85. // Pressing Page Up/Down increases/decreases value by 10
  86. await userEvent.keyboard('{PageUp}');
  87. expect(onChangeEndMock).toHaveBeenCalledWith(6);
  88. await userEvent.keyboard('{PageDown}');
  89. expect(onChangeEndMock).toHaveBeenCalledWith(5);
  90. // Pressing Home/End moves value to the min/max position
  91. await userEvent.keyboard('{Home}');
  92. expect(onChangeEndMock).toHaveBeenCalledWith(5);
  93. await userEvent.keyboard('{End}');
  94. expect(onChangeEndMock).toHaveBeenCalledWith(100);
  95. });
  96. it('works with two thumbs', async function () {
  97. const onChangeEndMock = jest.fn();
  98. render(
  99. <Slider
  100. label="Test"
  101. min={5}
  102. max={10}
  103. defaultValue={[6, 8]}
  104. onChangeEnd={onChangeEndMock}
  105. />
  106. );
  107. const sliders = screen.getAllByRole('slider', {name: 'Test'});
  108. // First slider
  109. await userEvent.tab();
  110. expect(sliders[0]).toHaveFocus();
  111. expect(sliders[0]).toHaveValue('6');
  112. expect(sliders[0]).toHaveAttribute('min', '5');
  113. expect(sliders[0]).toHaveAttribute('max', '8'); // can't go above second slider's value
  114. await userEvent.keyboard('{ArrowRight}');
  115. expect(onChangeEndMock).toHaveBeenCalledWith([7, 8]);
  116. // Second slider
  117. await userEvent.tab();
  118. expect(sliders[1]).toHaveFocus();
  119. expect(sliders[1]).toHaveValue('8');
  120. expect(sliders[1]).toHaveAttribute('min', '7'); // can't go below first slider's value
  121. expect(sliders[1]).toHaveAttribute('max', '10');
  122. await userEvent.keyboard('{ArrowRight}');
  123. expect(onChangeEndMock).toHaveBeenCalledWith([7, 9]);
  124. });
  125. });