Skip to content

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

Merged
merged 34 commits into from
Jun 24, 2019
Merged

Conversation

michaellasky
Copy link
Collaborator

@michaellasky michaellasky commented Jun 14, 2019

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:

  • Documentation added to the
    docs site
  • Typescript definitions updated
  • Tests
  • Ready to be merged

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.

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.
@michaellasky
Copy link
Collaborator Author

michaellasky commented Jun 14, 2019

I have code for a logRoles test that doesn't rely on snapshotting if that's easier for the CI (referencing the failed build). I'm using a snapshot because the test shouldn't necessarily fail (beyond needing to update the snapshot) if the formatting (magic that makes crazy console colors) from debugDOM changes. If I hardcode the output from debugDOM into a test case, then the code would need to be updated if debugDOM formatting changes.

A snapshot makes sense as well since logRoles just returns a string that is formatted to loosely defined, visually pleasing standards. IMO we don't need to test every little aspect of that (where every newline or space is, etc) It's self evident if it doesn't look nice in everyday usage.

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
Copy link
Member

@kentcdodds kentcdodds left a 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!

@michaellasky
Copy link
Collaborator Author

Also, a screenshot would be helpful!

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.

@kentcdodds
Copy link
Member

Looks pretty good to me!

  1. It would probably be helpful to indent the DOM output so the sections are more visible
  2. It would probably be helpful to have a separator (even just a single line) between each DOM output so people don't think they represent the same node or something.
  3. That <article> node has a bunch of stuff in it and if it were really big, that output could be VERY extensive. I wonder if we should just clip it to <article /> instead of <article>all the contents</article> Thoughts?

@michaellasky
Copy link
Collaborator Author

3. That `<article>` node has a bunch of stuff in it and if it were really big, that output could be VERY extensive. I wonder if we should just clip it to `<article />` instead of `<article>all the contents</article>` Thoughts?

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.

@kentcdodds
Copy link
Member

My thinking is that before we hand it to debugDOM we could create a copy of the DOM node (via something similar to asFragment) and then remove all the children.

@michaellasky
Copy link
Collaborator Author

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
@michaellasky
Copy link
Collaborator Author

michaellasky commented Jun 15, 2019

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
kentcdodds
kentcdodds previously approved these changes Jun 18, 2019
Copy link
Member

@kentcdodds kentcdodds left a 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.

michaellasky added a commit to michaellasky/testing-library-docs that referenced this pull request Jun 20, 2019
These functions are proposed in dom-testing-library PR#285

testing-library/dom-testing-library#285
@michaellasky
Copy link
Collaborator Author

michaellasky commented Jun 20, 2019

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:

export function logRoles(container: HTMLElement): string
export function getRoles(
    container: HTMLElement,
 ): {[index: string]: HTMLElement[]}

But unfortunately that fails linting (kcd-scripts) on build with:

2:1  error  Parsing error: Unexpected token, expected "{"

After some experimentation, I can only get it to pass if I add an implementation. For debugging purposes, I changed it to:

export function logRoles(container: HTMLElement): string
export function getRoles(container: HTMLElement): string

Which fails in the same way. But:

export function logRoles(container: HTMLElement): string { return container? '1': '2' }
export function getRoles(container: HTMLElement): string { return container? '1': '2' }

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:

✖ kcd-scripts lint found some errors. Please fix them and try committing again.

/home/mlasky/dev/dom-testing-library/typings/role-helpers.d.ts
2:1  error  Parsing error: Unexpected token, expected "{"


  1 | export function logRoles(container: HTMLElement): string
> 2 | export function getRoles(
    | ^
  3 |   container: HTMLElement,
  4 | ): {[index: string]: HTMLElement[]}
  5 | 

 ✖ 1 problem (1 error, 0 warnings)

@kentcdodds
Copy link
Member

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!

@kentcdodds
Copy link
Member

Oh, and it looks like there's a merge conflict 😬

@michaellasky
Copy link
Collaborator Author

michaellasky commented Jun 23, 2019

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.

93f9530


return Object.entries(roles)
.map(([role, elements]) => {
const numDelimeters = 42 - role.length // 42 is arbitrary
Copy link
Collaborator

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?

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

@michaellasky michaellasky Jun 24, 2019

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.
Copy link
Member

@kentcdodds kentcdodds left a 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!

@kentcdodds kentcdodds merged commit 1a5251c into testing-library:master Jun 24, 2019
@kentcdodds
Copy link
Member

@all-contributors please add @michaellasky for code, tests, and docs

@allcontributors
Copy link
Contributor

@kentcdodds

I've put up a pull request to add @michaellasky! 🎉

@kentcdodds
Copy link
Member

🎉 This PR is included in version 5.3.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@kentcdodds
Copy link
Member

Thanks so much for your help! I've added you as a collaborator on the project. Please make sure that you review the other/MAINTAINING.md and CONTRIBUTING.md files (specifically the bit about the commit messages and the git hooks) and familiarize yourself with the code of conduct (we're using the contributor covenant). You might also want to watch the repo to be notified when someone files an issue/PR. Please continue to make PRs as you feel the need (you can make your branches directly on the repo rather than your fork if you want). Thanks! And welcome to the team :)

@michaellasky
Copy link
Collaborator Author

Thanks Kent, you're making open source a lot of fun. I'll continue to watch and contribute.

kentcdodds pushed a commit to testing-library/testing-library-docs that referenced this pull request Jul 7, 2019
* 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
superT999 added a commit to superT999/testing-library-docs that referenced this pull request Feb 13, 2023
* 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
web3gru pushed a commit to web3gru/testing-library-docs that referenced this pull request May 13, 2023
* 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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants