-
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: tidy up internal logic #63553
Changes from all commits
d9f7d87
eb5ae09
feb36c9
0e8f1c4
b0e91a0
dd5f9c3
c707d1d
a9b1953
30e5c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,29 @@ import userEvent from '@testing-library/user-event'; | |
*/ | ||
import FontSizePicker from '../'; | ||
import type { FontSize } from '../types'; | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState } from '@wordpress/element'; | ||
|
||
const ControlledFontSizePicker = ( { | ||
onChange, | ||
...props | ||
}: React.ComponentProps< typeof FontSizePicker > ) => { | ||
const [ fontSize, setFontSize ] = | ||
useState< typeof props.value >( undefined ); | ||
|
||
return ( | ||
<FontSizePicker | ||
{ ...props } | ||
value={ fontSize } | ||
onChange={ ( newFontSize ) => { | ||
setFontSize( newFontSize ); | ||
onChange?.( newFontSize ); | ||
} } | ||
/> | ||
); | ||
}; | ||
|
||
describe( 'FontSizePicker', () => { | ||
test.each( [ | ||
|
@@ -118,9 +141,7 @@ describe( 'FontSizePicker', () => { | |
render( | ||
<FontSizePicker fontSizes={ fontSizes } value={ value } /> | ||
); | ||
expect( | ||
screen.getByLabelText( expectedLabel ) | ||
).toBeInTheDocument(); | ||
expect( screen.getByLabelText( expectedLabel ) ).toBeVisible(); | ||
} | ||
); | ||
|
||
|
@@ -243,9 +264,7 @@ describe( 'FontSizePicker', () => { | |
render( | ||
<FontSizePicker fontSizes={ fontSizes } value={ value } /> | ||
); | ||
expect( | ||
screen.getByLabelText( expectedLabel ) | ||
).toBeInTheDocument(); | ||
expect( screen.getByLabelText( expectedLabel ) ).toBeVisible(); | ||
} | ||
); | ||
|
||
|
@@ -360,9 +379,7 @@ describe( 'FontSizePicker', () => { | |
render( | ||
<FontSizePicker fontSizes={ fontSizes } value={ value } /> | ||
); | ||
expect( | ||
screen.getByLabelText( expectedLabel ) | ||
).toBeInTheDocument(); | ||
expect( screen.getByLabelText( expectedLabel ) ).toBeVisible(); | ||
} | ||
); | ||
|
||
|
@@ -434,9 +451,7 @@ describe( 'FontSizePicker', () => { | |
render( | ||
<FontSizePicker fontSizes={ fontSizes } value={ value } /> | ||
); | ||
expect( | ||
screen.getByLabelText( expectedLabel ) | ||
).toBeInTheDocument(); | ||
expect( screen.getByLabelText( expectedLabel ) ).toBeVisible(); | ||
} | ||
); | ||
|
||
|
@@ -493,7 +508,7 @@ describe( 'FontSizePicker', () => { | |
render( | ||
<FontSizePicker fontSizes={ fontSizes } value={ value } /> | ||
); | ||
expect( screen.getByRole( 'radiogroup' ) ).toBeInTheDocument(); | ||
expect( screen.getByRole( 'radiogroup' ) ).toBeVisible(); | ||
expect( | ||
screen.queryByRole( 'radio', { checked: true } ) | ||
).not.toBeInTheDocument(); | ||
|
@@ -514,15 +529,48 @@ describe( 'FontSizePicker', () => { | |
await user.click( | ||
screen.getByRole( 'option', { name: 'Custom' } ) | ||
); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeInTheDocument(); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeVisible(); | ||
expect( onChange ).not.toHaveBeenCalled(); | ||
} ); | ||
} | ||
|
||
function commonTests( fontSizes: FontSize[] ) { | ||
it( 'shows custom input when value is unknown', () => { | ||
render( <FontSizePicker fontSizes={ fontSizes } value="3px" /> ); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeInTheDocument(); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeVisible(); | ||
} ); | ||
|
||
it( 'hides custom input when disableCustomFontSizes is set to `true` with a custom font size', () => { | ||
const { rerender } = render( | ||
<FontSizePicker fontSizes={ fontSizes } value="3px" /> | ||
); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeVisible(); | ||
|
||
rerender( | ||
<FontSizePicker | ||
disableCustomFontSizes | ||
fontSizes={ fontSizes } | ||
value="3px" | ||
/> | ||
); | ||
expect( | ||
screen.queryByLabelText( 'Custom' ) | ||
).not.toBeInTheDocument(); | ||
Comment on lines
+556
to
+558
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't write a more specific assertion on which UI is presented to the user since I initially wrote this unit test so that it would pass on Since the input UI presented to the user changes depending on the length of the |
||
} ); | ||
|
||
it( "doesn't hide custom input when the selected font size is a predef", () => { | ||
const { rerender } = render( | ||
<FontSizePicker fontSizes={ fontSizes } value="3px" /> | ||
); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeVisible(); | ||
|
||
rerender( | ||
<FontSizePicker | ||
fontSizes={ fontSizes } | ||
value={ fontSizes[ 0 ].size } | ||
/> | ||
); | ||
expect( screen.getByLabelText( 'Custom' ) ).toBeVisible(); | ||
} ); | ||
|
||
it( 'allows custom values by default', async () => { | ||
|
@@ -539,6 +587,26 @@ describe( 'FontSizePicker', () => { | |
expect( onChange ).toHaveBeenCalledWith( '80px' ); | ||
} ); | ||
|
||
it( 'allows switching between custom and predef inputs when pressing the dedicated toggle', async () => { | ||
const user = userEvent.setup(); | ||
|
||
render( <ControlledFontSizePicker fontSizes={ fontSizes } /> ); | ||
|
||
await user.click( | ||
screen.getByRole( 'button', { name: 'Set custom size' } ) | ||
); | ||
|
||
await user.type( screen.getByLabelText( 'Custom' ), '80' ); | ||
|
||
await user.click( | ||
screen.getByRole( 'button', { name: 'Use size preset' } ) | ||
); | ||
|
||
expect( | ||
screen.queryByLabelText( 'Custom' ) | ||
).not.toBeInTheDocument(); | ||
} ); | ||
|
||
it( 'does not allow custom values when disableCustomFontSizes is set', () => { | ||
render( | ||
<FontSizePicker | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,16 +37,17 @@ export type FontSizePickerProps = { | |
*/ | ||
value?: number | string; | ||
/** | ||
* If `true`, the UI will contain a slider, instead of a numeric text input | ||
* field. If `false`, no slider will be present. | ||
* If `true`, a slider will be displayed alongside the input field when a | ||
* custom font size is active. Has no effect when `disableCustomFontSizes` | ||
* is `true`. | ||
* | ||
* @default false | ||
*/ | ||
withSlider?: boolean; | ||
/** | ||
* If `true`, a reset button will be displayed alongside the input field | ||
* when a custom font size is active. Has no effect when | ||
* `disableCustomFontSizes` or `withSlider` is `true`. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, seems like this was wrong. |
||
* `disableCustomFontSizes` is `true`. | ||
* | ||
* @default true | ||
*/ | ||
|
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.
Swapped the previous
if
-based logic to aswitch
-based logic looking at the value ofcurrentPickerType
, which should be much simpler to follow