-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
5ea6028
withFontSizes: Parse units into a numbers
Mamaduka c9b2bc0
Revert "withFontSizes: Parse units into a numbers"
Mamaduka ccd9547
FontSizePicker: Don't use units if the value is a number
Mamaduka 7cce2f2
Add unit tests
Mamaduka 7e184be
Disable units when we have number values
Mamaduka File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', () => { | ||
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' ); | ||
} ); | ||
} ); | ||
} ); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
andUnitControl
based on has units value will introduce more complexity to the component.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
There was a problem hiding this comment.
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.