-
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
Avoid coupling LinkControl
test to css class
#20190
Avoid coupling LinkControl
test to css class
#20190
Conversation
); | ||
const editButton = Array.from( | ||
container.querySelectorAll( 'button' ) | ||
).find( ( button ) => button.innerHTML.includes( 'Edit' ) ); |
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.
Searching by the element content seems to be a little bit fragile as well, but I guess we don't any other way to get it, such as id
, a data-
attribute, or whatever.
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 understand how this might appear. However, my take on this is that this is precisely when I'd want my test to break.
If the text changes to Flibble Wibble
then I want the test to say "Whoooooaaa hold on there Mr!, you changed a crucial piece of UI text". This would force me as a developer to acknowledge my change and update the test to reflect it (ie: it couldn't happen by accident).
Contrast this with any of the other approaches mentioned. Changing an id
- user can't see it, makes no difference to them but the test fails. Test is coupled to an implementation detail.
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.
LGTM. So let's wait for tests passing ;-)
This is a good example of how the What do you think about this approach? https://kentcdodds.com/blog/making-your-ui-tests-resilient-to-change |
I exactly meant to this! Thanks, Bart for the information. 👏 |
Really appreciate the feedback from both @retrofox and @WunderBart 👍 @WunderBart See #20190 (comment) I'm aware of the
All of these are user perceivable UI which we can use to select the correct element. By making our tests conform as closely to how a user interacts with our component we achieve less brittle tests which better capture the intent of the component's functionality. You've referenced the exact article I would have done. Here's a good quote:
Have you used RTL? If you have you know they prefer a certain order of matches. Granted my approach here is less optimal then |
This seems like a fine change. The main concern with this sort of approach is that the developer writing the test should be wary to not make the selection too broad that it risks choosing an unintended element. For example, there are probably many buttons on the editor screen whose HTML includes "Edit". I think this was accounted for sufficiently here (since the
There's some truth to this in that it might not have failed if it was targeting a label. I also think it's worth highlighting the issue that #19738 should have been rebased before it was merged, since it was running tests on a stale codebase. If it had been rebased, the build would have failed, and the issue would have been avoided. |
As we saw here coupling this test to a CSS selector makes it more prone to error.
In general, it's my opinion that we should try to test user perceivable UI rather than developer-centric implementation details.
This PR updates the test to check for text content to identify the
Edit
button rather than rely on the CSS class implementation detail. It's a minor change but I feel it's worthwhile.This should make the test more resilient to change, as changing text is a user perceivable change and will, therefore, be more easily identifiable as a cause of test regression.
PS: also sneaking in removal of legacy comments I sholuld have removed in #20182
How has this been tested?
Checking all existing unit tests pass both on Travis and on local
Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist: