import {fireEvent, render, screen} from 'sentry-test/reactTestingLibrary'; import RangeSlider from 'sentry/components/forms/controls/rangeSlider'; describe('RangeSlider', function () { it('changes value / has right label', function () { render( {}} />); expect(screen.getByRole('slider', {name: '5'})).toBeInTheDocument(); fireEvent.change(screen.getByRole('slider'), {target: {value: '7'}}); expect(screen.getByRole('slider', {name: '7'})).toBeInTheDocument(); }); it('can use formatLabel', function () { render( (
{value === 7 ? 'SEVEN!' : Number(value) + 1}
)} onChange={() => {}} /> ); expect(screen.getByTestId('test')).toHaveTextContent('6'); fireEvent.change(screen.getByRole('slider'), {target: {value: '7'}}); expect(screen.getByTestId('test')).toHaveTextContent('SEVEN!'); }); it('calls onChange', function () { const onChange = jest.fn(); render(); expect(onChange).not.toHaveBeenCalled(); fireEvent.change(screen.getByRole('slider'), {target: {value: '7'}}); expect(onChange).toHaveBeenCalledWith(7, expect.anything()); expect(onChange).toHaveBeenCalledTimes(1); }); it('can provide a list of allowedValues', function () { const onChange = jest.fn(); render( ); // With `allowedValues` sliderValue will be the index to value in `allowedValues` expect(screen.getByRole('slider', {name: '1000'})).toBeInTheDocument(); fireEvent.change(screen.getByRole('slider'), {target: {value: '0'}}); expect(screen.getByRole('slider', {name: '0'})).toBeInTheDocument(); // onChange will callback with a value from `allowedValues` expect(onChange).toHaveBeenCalledWith(0, expect.anything()); }); it('handles invalid values', function () { const onChange = jest.fn(); render( ); fireEvent.change(screen.getByRole('slider'), {target: {value: '-2'}}); expect(screen.getByRole('slider', {name: '0'})).toBeInTheDocument(); // onChange will callback with a value from `allowedValues` expect(onChange).toHaveBeenCalledWith(0, expect.anything()); }); });