-
Notifications
You must be signed in to change notification settings - Fork 469
Getroles logroles #285
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
Getroles logroles #285
Conversation
Added lodash.merge to handle deep merging in role-helpers.
Implements requirements of testing-library#282 Added getRoles and logRoles to a new file, role-helpers.js. Moved buildElementRoleList from src/queries/role.js to src/role-helpers.js This function was also needed in role-helpers, and it didn't seem proper to to expert it from queries/role with a bunch of queries. Makes more sense to be in the helper file i think.
I have code for a logRoles test that doesn't rely on snapshotting if that's easier for the CI (referencing the failed build).
Edit: Thinking about this further I think that test certainly needs some checking to make sure the string at least contains the expected node and role names. Updated test, no snapshot. |
Thinking it over, using the snapshot didn't make much sense either. So Now this test just checks loosely that the output has the expected labels followed by expected element names
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 for this! It's pretty hard to visualize what this would look like. I think a snapshot would be appropriate for the logRoles
function test.
Also, a screenshot would be helpful!
Thanks for this!
Quick screenshot from npm test with a console log I'll spin it up in the browser in a bit when I make the other changes and take a look at / screencap the output there. I can of course make any changes to the formatting. I didn't match the suggested implementation exactly, but can if that's wanted. |
Looks pretty good to me!
|
I actually had a similar thought. I didn't pursue it because that would be more of a change to debugDOM I think? We'd need to add a parameter to tell it not to print all the children (assuming it can't already do that, i haven't looked at it extensively). The current implementation is just debugDom(node), which in the case of a large node like the article node, prints out all the children. In any event, i don't think it'd be very difficult to handle. |
My thinking is that before we hand it to |
Oh, I hadn't considered that. That's not a bad idea, I'll give it a shot and update this (hopefully tonight, time permitting) with that and the other suggested changes. |
No longer shows child nodes in output cleaerer distinction between groups
Fixed bug in getRoles where deeply nested children could overwrtie each other getRoles is now simlper The tests for getRoles and logRoles now use a more realistic DOM tree
buildElementRoleList doesn't need to be exported at this time. Previously, src/queries/roles used that function to create elementRoleList variable, but since role-helpers needed that as well it's just created in there and only elementRoleList is exported
Lodash.merge is no longer required with the getRoles refactor
So I've made most of the discussed changes. This is what the output of logRoles looks like now I also fixed a bug with getRoles, and refactored it to clean it up and simplify things. logRoles uses a snapshot test again. The regex only worked if the html under test is very simple, and it a tad more 'real world'. So rather than putting an ugly regex in the test i went back to a snapshot. Lodash.merge dependency is gone. |
There was a leftover -1 on a calculation that calculates the number of '#'s for the role title in logRoles
Nodes that required greater specificity (ie input[type="radio"] is role radio, not the default textbox) where not getting their roles set correctly. Moved getImplicitAriaRole from src/queries/role to src/role-helpers
Also gives 100% branch coverage
elementRoleMap was useful before getRoles was refactored, but is no longer being used.
Only getRoles and logRoles are exported to the public api
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! Would love another reviewer on this though.
These functions are proposed in dom-testing-library PR#285 testing-library/dom-testing-library#285
I've attempted to add the typescript definitions but I seem to be doing something wrong. I'm not particularly fluent with typescript. I'd imagine the definitions, in a file named typings/role-helpers.d.ts, should look something like:
But unfortunately that fails linting (kcd-scripts) on build with:
After some experimentation, I can only get it to pass if I add an implementation. For debugging purposes, I changed it to:
Which fails in the same way. But:
passes linting fine. I am confuse. But it's probably just some assumption I'm making because of lack of TS experience. Full Error output:
|
Yeah, honestly I'm not sure what's up with that 😞 we should probably just disable linting on that file. This always happens to me when I change that file so I just commit that file with --no-verify 😬 I think things look pretty good. I'd love another review though! |
Oh, and it looks like there's a merge conflict 😬 |
Added the typescript definitions. Whoever else reviews these changes should pay particular attention to them, since I don't have complete confidence they're correct. |
src/role-helpers.js
Outdated
|
||
return Object.entries(roles) | ||
.map(([role, elements]) => { | ||
const numDelimeters = 42 - role.length // 42 is arbitrary |
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.
As far as I know there aren't any aria-roles greater than 42 chars, but I would really hate for a formatting error to break someone's test. Can we be conservative here and do a Math.min(0, numDelimiters) or move the delimiter characters to a fixed length line?
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.
That's a good point. I think it's very unlikely there will ever be a role greater than 42 characters long... there's also no real downside to protecting against that. Maybe I was being too clever with embedding the name in the delimiters. It read easier (to my eyes) with just a single heading/spacer element, but it probably makes more sense to just implement it like Kent originally had it and avoid any confusion, extra code, or bugs (however unlikely).
I'll make that change this evening unless I hear an objection.
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 593c33f
Check the snapshot for output format.
I would have liked to indent the elements themselves to further distinguish them from the headings, but unfortunately I couldn't find a decent way to do that with the way debugDOM->prettyDOM->prettyFormat works. They inject their own newlines into the output, so if I try to indent it I can only indent the first line. I'd need to split the output of debugDOM up by lines and indent them individually... which seemed messy for a minor visual improvement.
I also purposefully left out the space between the delimiter bar ('-----') and the role name label. With the space it looks a lot louder visually (imo).
--------------------------------------------------
navigation:
<nav
data-testid="a-nav"
/>
--------------------------------------------------
heading:
<h1
data-testid="a-h1"
/>
vs
--------------------------------------------------
navigation:
<nav
data-testid="a-nav"
/>
--------------------------------------------------
heading:
<h1
data-testid="a-h1"
/>
Switched formatting back to initial proposal to avoid potential problems.
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 looks awesome. Thank you!
@all-contributors please add @michaellasky for code, tests, and docs |
I've put up a pull request to add @michaellasky! 🎉 |
🎉 This PR is included in version 5.3.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the |
Thanks Kent, you're making open source a lot of fun. I'll continue to watch and contribute. |
* Added docs for getRoles and logRoles These functions are proposed in dom-testing-library PR#285 testing-library/dom-testing-library#285 * Updated logRoles output example to match new format * Changed import to @testing-library/dom Co-Authored-By: Adrià Fontcuberta <[email protected]> * Changed 'aria' to 'ARIA' in api-helpers * Gave example HTML ul/li elements valid attributes name is not a valid attribute for ul/li, changed to type/value respectively * Updated another instance wrong 'dom-testing-library' import * Added link to ARIA in HTML under getRoles * Updated getRoles and logRoles examples After discussion we decided to pull attributes off the example elements. #155
* Added docs for getRoles and logRoles These functions are proposed in dom-testing-library PR#285 testing-library/dom-testing-library#285 * Updated logRoles output example to match new format * Changed import to @testing-library/dom Co-Authored-By: Adrià Fontcuberta <[email protected]> * Changed 'aria' to 'ARIA' in api-helpers * Gave example HTML ul/li elements valid attributes name is not a valid attribute for ul/li, changed to type/value respectively * Updated another instance wrong 'dom-testing-library' import * Added link to ARIA in HTML under getRoles * Updated getRoles and logRoles examples After discussion we decided to pull attributes off the example elements. testing-library/testing-library-docs#155
* Added docs for getRoles and logRoles These functions are proposed in dom-testing-library PR#285 testing-library/dom-testing-library#285 * Updated logRoles output example to match new format * Changed import to @testing-library/dom Co-Authored-By: Adrià Fontcuberta <[email protected]> * Changed 'aria' to 'ARIA' in api-helpers * Gave example HTML ul/li elements valid attributes name is not a valid attribute for ul/li, changed to type/value respectively * Updated another instance wrong 'dom-testing-library' import * Added link to ARIA in HTML under getRoles * Updated getRoles and logRoles examples After discussion we decided to pull attributes off the example elements. testing-library/testing-library-docs#155
This is a draft implementation for the functionality requested in #282
The intention is to give a jumping off point for discussion.
I've added a file named role-helpers.js which implements the two requested functions, and also moved the function buildElementRoleList from src/queries/role.js into role-helpers.js. Since role-helpers needed similar functionality, and it didn't seem to make sense to export it from query/role with a bunch of query functions.
I also added a dependency on lodash.merge for a deep merge in getRoles. Maybe this could be removed because the merge happening there isn't all that complicated. But using the premade merge() provided by lodash allowed me to keep the initial PR code cleaner for discussion.
Checklist:
docs site
I'm still pretty new to this particular code base, so I don't think it's ready to be merged without some significant review and iteration from more experienced members.