-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Components: Refactor Icon
tests to @testing-library/react
#44051
Conversation
1714311
to
1dc79b1
Compare
1dc79b1
to
fa8a107
Compare
Tooltip
tests to @testing-library/react
Icon
tests to @testing-library/react
Size Change: +1.42 kB (0%) Total Size: 1.25 MB
ℹ️ View Unchanged
|
@@ -111,7 +108,7 @@ describe( 'Icon', () => { | |||
[ 'svg element', { icon: svg } ], | |||
[ 'component', { icon: MyComponent } ], | |||
] )( '%s', ( label, props ) => { | |||
it( 'should pass through size', () => { | |||
it.skip( 'should pass through size', () => { |
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.
This test is tentatively skipped because it tests the full implementation and I was not sure how this could be replaced.
If you have a better approach to this test, I would like to hear your advice.
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 think we can delete this whole describe.each
call, I don't think it adds any value to the tests (especially since it used to check the value of the size
prop on the wrapper component, and not the prop's effects on its children)
Icon
tests to @testing-library/react
Icon
tests to @testing-library/react
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.
Thank you for working on this!
Could you add a CHANGELOG entry? 🙏
@@ -111,7 +108,7 @@ describe( 'Icon', () => { | |||
[ 'svg element', { icon: svg } ], | |||
[ 'component', { icon: MyComponent } ], | |||
] )( '%s', ( label, props ) => { | |||
it( 'should pass through size', () => { | |||
it.skip( 'should pass through size', () => { |
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 think we can delete this whole describe.each
call, I don't think it adds any value to the tests (especially since it used to check the value of the size
prop on the wrapper component, and not the prop's effects on its children)
@ciampo
|
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.
This is lovely to see, thanks @t-hamano 🙌
I left a few minor suggestions, but this looks great in any case 🚀
|
||
expect( wrapper.name() ).toBe( 'span' ); | ||
expect( screen.getByTestId( testId ) ).toBeInTheDocument(); |
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.
Does it make sense to use .toBeVisible()
which is a bit more specific than .toBeInTheDocument()
?
} ); | ||
|
||
it( 'renders an svg element with a default width and height of 24', () => { | ||
const wrapper = shallow( <Icon icon={ svg } /> ); | ||
render( <Icon data-testid={ testId } icon={ svg } /> ); | ||
const icon = screen.queryByTestId( testId ); |
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.
We should be able to use get*
instead of query*
here, see this for the rationale.
const icon = screen.queryByTestId( testId ); | |
const icon = screen.getByTestId( testId ); |
@@ -74,73 +70,26 @@ describe( 'Icon', () => { | |||
size={ 32 } | |||
/> | |||
); | |||
const icon = screen.queryByTestId( testId ); |
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.
const icon = screen.queryByTestId( testId ); | |
const icon = screen.getByTestId( testId ); |
} ); | ||
|
||
it( 'renders an svg element and does not override width and height if already specified', () => { | ||
const wrapper = shallow( <Icon icon={ svg } size={ 32 } /> ); | ||
render( <Icon data-testid={ testId } icon={ svg } size={ 32 } /> ); | ||
const icon = screen.queryByTestId( testId ); |
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.
const icon = screen.queryByTestId( testId ); | |
const icon = screen.getByTestId( testId ); |
Thank you for the review, @tyxla ! |
@@ -21,7 +21,7 @@ describe( 'Icon', () => { | |||
it( 'renders nothing when icon omitted', () => { | |||
render( <Icon data-testid={ testId } /> ); | |||
|
|||
expect( screen.queryByTestId( testId ) ).not.toBeInTheDocument(); | |||
expect( screen.queryByTestId( testId ) ).toBeNull(); |
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 think this additional fix is reasonable?
(.not.toBeVisible()
didn't work)
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.
No, I think .not.toBeInTheDocument()
is ideally expressing our goal here; .toBeNull()
makes the assertion vague and unclear, while what we have already is pretty transparent IMHO.
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.
.not.toBeVisible()
won't work by design, an element can't be visible if it doesn't exist in the DOM tree, that's why we check with .not.toBeInTheDocument()
.
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 see!
I have reverted this change and would like to merge it once the test passes.
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.
Thanks, @t-hamano 🙌
Thank you @tyxla for the additional review ❤️ |
What?
This PR refactors the
Icon
tests fromenzyme
to@testing-library/react
.Why?
To clarify the specification of the
Icon
component before refactoring to TypeScript in #35744.How?
I rewrote it according to two approaches:
testId
in almost all tests because there is no accesible text and roles.Testing Instructions
Confirm that all tests pass:
npm run test:unit packages/components/src/icon/test/index.js
Also, it would be easier to see the changes in a split view.