-
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
Adding support for defined IDs in TextControl
component
#52028
Changes from 1 commit
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 | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,46 @@ | ||||||||||
/** | ||||||||||
* External dependencies | ||||||||||
*/ | ||||||||||
import { render, screen } from '@testing-library/react'; | ||||||||||
|
||||||||||
/** | ||||||||||
* Internal dependencies | ||||||||||
*/ | ||||||||||
import TextControl from '..'; | ||||||||||
|
||||||||||
const noop = () => {}; | ||||||||||
|
||||||||||
describe( 'TextControl', () => { | ||||||||||
it( 'should generate an ID if not passed as a prop', () => { | ||||||||||
render( <TextControl onChange={ noop } value={ '' } /> ); | ||||||||||
|
||||||||||
expect( screen.getByRole( 'textbox' ) ).toHaveAttribute( | ||||||||||
'id', | ||||||||||
expect.stringMatching( /^inspector-text-control-/ ) | ||||||||||
); | ||||||||||
} ); | ||||||||||
|
||||||||||
it( 'should use the passed ID prop if provided', () => { | ||||||||||
const id = 'test-id'; | ||||||||||
render( <TextControl onChange={ noop } id={ id } value={ '' } /> ); | ||||||||||
|
||||||||||
expect( screen.getByRole( 'textbox' ) ).toHaveAttribute( 'id', id ); | ||||||||||
} ); | ||||||||||
|
||||||||||
it( 'should map the label and input together when given an ID', () => { | ||||||||||
const id = 'test-id'; | ||||||||||
const labelValue = 'Test Label'; | ||||||||||
render( | ||||||||||
<TextControl | ||||||||||
onChange={ noop } | ||||||||||
id={ id } | ||||||||||
label={ labelValue } | ||||||||||
value={ '' } | ||||||||||
/> | ||||||||||
); | ||||||||||
|
||||||||||
const textbox = screen.getByRole( 'textbox' ); | ||||||||||
const label = screen.getByText( labelValue ); | ||||||||||
expect( textbox ).toHaveAttribute( 'id', label.getAttribute( 'for' ) ); | ||||||||||
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. Nit: Not that this is inherently wrong, but I find it more idiomatic/robust within the testing-library context to simply assert that the
Suggested change
☝️ Something like that. What do you think? 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 don't have much experience with |
||||||||||
} ); | ||||||||||
} ); |
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.
FYI we can pass the id prefix and the
idProp
directly into theuseInstanceId()
function to avoid this custom code ✨gutenberg/packages/compose/src/hooks/use-instance-id/index.ts
Lines 40 to 48 in 10d927c
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.
I noticed this, and strongly considered using it. But the
useUniqueId
pattern seemed to be in quite a few places already, which made me second guess myself.I'm quite happy to apply
useInstanceId
"properly" here though, if that's actually the more appropriate direction.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.
Yes, I think it is. I have a feeling that the existing
useUniqueId
patterns were written beforeuseInstanceId
was expanded to take three arguments.