Skip to content

Commit

Permalink
Update reset-related tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ciampo committed Aug 17, 2024
1 parent ca92a21 commit 1872e89
Showing 1 changed file with 46 additions and 25 deletions.
71 changes: 46 additions & 25 deletions packages/components/src/range-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -297,50 +297,68 @@ describe( 'RangeControl', () => {
} );
} );

describe.only( 'reset', () => {
// TODO: behaviour to be tested, in controlled mode:
// - when `resetFallbackValue` is provided, the value is ultimately set to
// `resetFallbackValue`; `onChange()` is called with `resetFallbackValue`.
// - when `initialPosition` is provided, the value is ultimately set to
// `initialPosition`; `onChange()` is called with `undefined`.
// - when neither `resetFallbackValue` nor `initialPosition` are provided,
// the input values is cleared; `onChange()` is called with `undefined`.
// - in any case, when the current value is the same as the reset value
// (ie. the value that would be assigned when clicking the reset button),
// the reset button should be disabled;

it( 'should reset to a custom fallback value, defined by a parent component', () => {
describe( 'reset', () => {
it( 'should clear the input value when clicking the reset button', () => {
const spy = jest.fn();
render( <RangeControl allowReset onChange={ spy } /> );

const resetButton = getResetButton();
const rangeInput = getRangeInput();
const numberInput = getNumberInput();

fireChangeEvent( numberInput, '14' );

expect( rangeInput.value ).toBe( '14' );
expect( numberInput.value ).toBe( '14' );
expect( spy ).toHaveBeenCalledWith( 14 );

fireEvent.click( resetButton );

// range input resets to min + (max-min)/2
expect( rangeInput.value ).toBe( '50' );
expect( numberInput.value ).toBe( '' );
expect( spy ).toHaveBeenCalledWith( undefined );

expect( resetButton ).toHaveAttribute( 'aria-disabled', 'true' );
} );

it( 'should reset to the `initialPosition` value when clicking the reset button', () => {
const spy = jest.fn();
render(
<RangeControl
initialPosition={ 10 }
allowReset
initialPosition={ 23 }
onChange={ spy }
resetFallbackValue={ 33 }
/>
);

const resetButton = getResetButton();
const rangeInput = getRangeInput();
const numberInput = getNumberInput();

fireChangeEvent( numberInput, '14' );

expect( rangeInput.value ).toBe( '14' );
expect( numberInput.value ).toBe( '14' );
expect( spy ).toHaveBeenCalledWith( 14 );

fireEvent.click( resetButton );

expect( rangeInput.value ).toBe( '33' );
expect( numberInput.value ).toBe( '33' );
expect( spy ).toHaveBeenCalledWith( 33 );
expect( rangeInput.value ).toBe( '23' );
expect( numberInput.value ).toBe( '23' );
expect( spy ).toHaveBeenCalledWith( undefined );

expect( resetButton ).toHaveAttribute( 'aria-disabled', 'true' );
} );

it( 'should reset to a 50% of min/max value, of no initialPosition or value is defined', () => {
it( 'should reset to the `resetFallbackValue` value when clicking the reset button', () => {
const spy = jest.fn();
render(
<RangeControl
initialPosition={ undefined }
min={ 0 }
max={ 100 }
initialPosition={ 10 }
allowReset
resetFallbackValue={ undefined }
onChange={ spy }
resetFallbackValue={ 33 }
/>
);

Expand All @@ -350,8 +368,11 @@ describe( 'RangeControl', () => {

fireEvent.click( resetButton );

expect( rangeInput.value ).toBe( '50' );
expect( numberInput.value ).toBe( '' );
expect( rangeInput.value ).toBe( '33' );
expect( numberInput.value ).toBe( '33' );
expect( spy ).toHaveBeenCalledWith( 33 );

expect( resetButton ).toHaveAttribute( 'aria-disabled', 'true' );
} );
} );
} );

0 comments on commit 1872e89

Please sign in to comment.