Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FontSizePicker: Use number values when the initial value is a number #33679

Merged
merged 5 commits into from
Jul 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/components/src/font-size-picker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,14 @@ function FontSizePicker(
if ( 0 === parseFloat( nextSize ) || ! nextSize ) {
onChange( undefined );
} else {
onChange( nextSize );
onChange(
hasUnits
? nextSize
: parseInt( nextSize, 10 )
);
}
} }
units={ units }
units={ hasUnits ? units : false }
/>
) }
<Button
Expand Down
136 changes: 136 additions & 0 deletions packages/components/src/font-size-picker/test/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/**
* External dependencies
*/
import { render, fireEvent, screen } from '@testing-library/react';

/**
* Internal dependencies
*/
import FontSizePicker from '../';

const getUnitSelect = () =>
document.body.querySelector( '.components-unit-control select' );
const getUnitLabel = () =>
document.body.querySelector( '.components-unit-control__unit-label' );

describe( 'FontSizePicker', () => {
describe( 'onChange values', () => {
it( 'should not use units when the initial value is a number', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check that the units input is not render or do you think it should be rendered in this case?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean units "switcher"? It's is still rendered but has no effect since we only use number values.

I think toggling between NumberControl and UnitControl based on has units value will introduce more complexity to the component.

CleanShot 2021-07-30 at 13 37 06

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think toggling between NumberControl and UnitControl based on has units value will introduce more complexity to the component.

Can't we just pass an empty array of units or something like that in that case? It feels weird for a user to select "em" but have pixels applied.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. It will now only display the "PX" unit label when there are no units.

let fontSize = 12;
const setFontSize = jest.fn(
( nextSize ) => ( fontSize = nextSize )
);

render(
<FontSizePicker value={ fontSize } onChange={ setFontSize } />
);

const unitSelect = getUnitSelect();
const unitLabel = getUnitLabel();
const input = screen.getByLabelText( 'Custom', {
selector: 'input',
} );

input.focus();
fireEvent.change( input, { target: { value: 16 } } );

expect( unitSelect ).toBeFalsy();
expect( unitLabel ).toBeTruthy();
expect( fontSize ).toBe( 16 );
} );

it( 'should use units when the initial value has a unit', () => {
let fontSize = '12px';
const setFontSize = jest.fn(
( nextSize ) => ( fontSize = nextSize )
);

render(
<FontSizePicker value={ fontSize } onChange={ setFontSize } />
);

const unitSelect = getUnitSelect();
const unitLabel = getUnitLabel();
const input = screen.getByLabelText( 'Custom', {
selector: 'input',
} );

input.focus();
fireEvent.change( input, { target: { value: 16 } } );

expect( unitSelect ).toBeTruthy();
expect( unitLabel ).toBeFalsy();
expect( fontSize ).toBe( '16px' );
} );

it( 'should not use units when fontSizes size values are numbers', () => {
let fontSize;
const fontSizes = [
{
name: 'Small',
slug: 'small',
size: 12,
},
];
const setFontSize = jest.fn(
( nextSize ) => ( fontSize = nextSize )
);

render(
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSize }
onChange={ setFontSize }
/>
);

const unitSelect = getUnitSelect();
const unitLabel = getUnitLabel();
const input = screen.getByLabelText( 'Custom', {
selector: 'input',
} );

input.focus();
fireEvent.change( input, { target: { value: 16 } } );

expect( unitSelect ).toBeFalsy();
expect( unitLabel ).toBeTruthy();
expect( fontSize ).toBe( 16 );
} );

it( 'should use units when fontSizes size values have units', () => {
let fontSize;
const fontSizes = [
{
name: 'Small',
slug: 'small',
size: '12px',
},
];
const setFontSize = jest.fn(
( nextSize ) => ( fontSize = nextSize )
);

render(
<FontSizePicker
fontSizes={ fontSizes }
value={ fontSize }
onChange={ setFontSize }
/>
);

const unitSelect = getUnitSelect();
const unitLabel = getUnitLabel();
const input = screen.getByLabelText( 'Custom', {
selector: 'input',
} );

input.focus();
fireEvent.change( input, { target: { value: 16 } } );

expect( unitSelect ).toBeTruthy();
expect( unitLabel ).toBeFalsy();
expect( fontSize ).toBe( '16px' );
} );
} );
} );