Skip to content

Commit

Permalink
Refactor the test for NumberControl component to reduce changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hbhalodia committed Nov 29, 2024
1 parent 6a7b449 commit 1e432c3
Showing 1 changed file with 18 additions and 30 deletions.
48 changes: 18 additions & 30 deletions packages/components/src/number-control/test/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,35 @@ import { useState } from '@wordpress/element';
/**
* Internal dependencies
*/
import NumberControl from '..';
import _NumberControl from '..';
import type { NumberControlProps } from '../types';

const NumberControl = (
props: React.ComponentProps< typeof _NumberControl >
) => <_NumberControl __next40pxDefaultSize { ...props } />;

function StatefulNumberControl( props: NumberControlProps ) {
const [ value, setValue ] = useState( props.value );
const handleOnChange = ( v: string | undefined ) => setValue( v );

return (
<NumberControl
{ ...props }
__next40pxDefaultSize
value={ value }
onChange={ handleOnChange }
/>
);
}

const ControlledNumberControl = ( props: NumberControlProps ) => {
return <NumberControl __next40pxDefaultSize { ...props } />;
};

describe( 'NumberControl', () => {
describe( 'Basic rendering', () => {
it( 'should render', () => {
render( <ControlledNumberControl /> );
render( <NumberControl /> );
expect( screen.getByRole( 'spinbutton' ) ).toBeVisible();
} );

it( 'should render custom className', () => {
render( <ControlledNumberControl className="hello" /> );
render( <NumberControl className="hello" /> );
expect( screen.getByRole( 'spinbutton' ) ).toBeVisible();
} );
} );
Expand All @@ -52,10 +51,7 @@ describe( 'NumberControl', () => {
const spy = jest.fn();

render(
<ControlledNumberControl
value={ 5 }
onChange={ ( v ) => spy( v ) }
/>
<NumberControl value={ 5 } onChange={ ( v ) => spy( v ) } />
);

const input = screen.getByRole( 'spinbutton' );
Expand All @@ -70,7 +66,7 @@ describe( 'NumberControl', () => {
const onChangeSpy = jest.fn();

render(
<ControlledNumberControl
<NumberControl
value={ 5 }
min={ 4 }
max={ 10 }
Expand Down Expand Up @@ -113,7 +109,7 @@ describe( 'NumberControl', () => {
const onChangeSpy = jest.fn();

render(
<ControlledNumberControl
<NumberControl
value={ 5 }
min={ 1 }
max={ 10 }
Expand Down Expand Up @@ -155,9 +151,7 @@ describe( 'NumberControl', () => {
it( 'should clamp value within range on ENTER keypress', async () => {
const user = userEvent.setup();

render(
<ControlledNumberControl value={ 5 } min={ 0 } max={ 10 } />
);
render( <NumberControl value={ 5 } min={ 0 } max={ 10 } /> );

const input = screen.getByRole( 'spinbutton' );

Expand All @@ -175,9 +169,7 @@ describe( 'NumberControl', () => {
it( 'should clamp value within range on blur', async () => {
const user = userEvent.setup();

render(
<ControlledNumberControl value={ 5 } min={ 0 } max={ 10 } />
);
render( <NumberControl value={ 5 } min={ 0 } max={ 10 } /> );

const input = screen.getByRole( 'spinbutton' );
await user.clear( input );
Expand All @@ -196,7 +188,7 @@ describe( 'NumberControl', () => {
it( 'should parse non-numeric values to a number on ENTER keypress when required', async () => {
const user = userEvent.setup();

render( <ControlledNumberControl value={ 5 } required /> );
render( <NumberControl value={ 5 } required /> );

const input = screen.getByRole( 'spinbutton' );
await user.clear( input );
Expand All @@ -209,9 +201,7 @@ describe( 'NumberControl', () => {
it( 'should parse non-numeric values to empty string on ENTER keypress when not required', async () => {
const user = userEvent.setup();

render(
<ControlledNumberControl value={ 5 } required={ false } />
);
render( <NumberControl value={ 5 } required={ false } /> );

const input = screen.getByRole( 'spinbutton' );
await user.clear( input );
Expand All @@ -230,7 +220,7 @@ describe( 'NumberControl', () => {
it( 'should not enforce numerical value for empty string when required is omitted', async () => {
const user = userEvent.setup();

render( <ControlledNumberControl value={ 5 } /> );
render( <NumberControl value={ 5 } /> );

const input = screen.getByRole( 'spinbutton' );
await user.clear( input );
Expand All @@ -245,7 +235,7 @@ describe( 'NumberControl', () => {
it( 'should enforce numerical value for empty string when required', async () => {
const user = userEvent.setup();

render( <ControlledNumberControl value={ 5 } required /> );
render( <NumberControl value={ 5 } required /> );

const input = screen.getByRole( 'spinbutton' );
await user.clear( input );
Expand Down Expand Up @@ -570,9 +560,7 @@ describe( 'NumberControl', () => {
] as NumberControlProps[ 'spinControls' ][] )(
'should not appear when spinControls = %s',
( spinControls ) => {
render(
<ControlledNumberControl spinControls={ spinControls } />
);
render( <NumberControl spinControls={ spinControls } /> );
expect(
screen.queryByLabelText( 'Increment' )
).not.toBeInTheDocument();
Expand All @@ -599,7 +587,7 @@ describe( 'NumberControl', () => {
const user = userEvent.setup();
const onChange = jest.fn();
render(
<ControlledNumberControl
<NumberControl
{ ...props }
spinControls="custom"
onChange={ onChange }
Expand Down

0 comments on commit 1e432c3

Please sign in to comment.