Skip to content
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

chore: Migrate ByRole to TypeScript #1186

Merged
merged 22 commits into from
Feb 6, 2023

Conversation

DaniAcu
Copy link
Contributor

@DaniAcu DaniAcu commented Oct 28, 2022

What:
Migrate role.js to role.ts.

Why:

Because is the only file that is in js. I wold like to add more inteligence to ByRole, but for any other thing we need to add it first as ts file.

How:

Checklist:

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

@codesandbox-ci
Copy link

codesandbox-ci bot commented Oct 28, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit 9e5d4b4:

Sandbox Source
react-testing-library-examples Configuration

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For TS migration please don't change runtime behavior (yet). This makes reviewing harder than it needs to be.

If the type-checker has issues with things, we can use @ts-expect-error or as any for now and then slowly migrate to a more type-safe version.

@DaniAcu
Copy link
Contributor Author

DaniAcu commented Oct 29, 2022

@eps1lon Ohh okay, thanks. I didn't know that but that make sense. Let me update the PR to just have ts migration here. Thanks, again

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lint is failing. Is there anything else that's unclear to you from the CI logs?

CI needs to be green for this to be mergeable. If you're stuck at some point, feel free to ping me and we can go through the CI logs to see what went wrong.

@codecov
Copy link

codecov bot commented Oct 31, 2022

Codecov Report

Merging #1186 (d72f79a) into alpha (25dc8a9) will not change coverage.
The diff coverage is 100.00%.

❗ Current head d72f79a differs from pull request most recent head 9e5d4b4. Consider uploading reports for the commit 9e5d4b4 to get more accurate results

@@            Coverage Diff            @@
##             alpha     #1186   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           24        24           
  Lines          998      1000    +2     
  Branches       327       328    +1     
=========================================
+ Hits           998      1000    +2     
Flag Coverage Δ
node-12 100.00% <100.00%> (?)
node-14 100.00% <100.00%> (ø)
node-16 100.00% <100.00%> (ø)
node-18 ?

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/query-helpers.ts 100.00% <ø> (ø)
src/queries/role.ts 100.00% <100.00%> (ø)
src/helpers.ts 100.00% <0.00%> (ø)
src/queries/label-text.ts 100.00% <0.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@DaniAcu
Copy link
Contributor Author

DaniAcu commented Nov 1, 2022

@eps1lon I delayed more in make the fixes becuase lint is not working locally.
image

So, I used the github jobs to check and fix it.

Related to the changes, I think that the types are not completly clean or organized. I will try to get into it a little bit more now that roles are also in ts.

Thanks for awesome welcoming to contribute here, I'm happy to could do it.

@DaniAcu DaniAcu requested a review from eps1lon November 1, 2022 01:00
@DaniAcu
Copy link
Contributor Author

DaniAcu commented Nov 3, 2022

@eps1lon there anything more that i need to change? 🤔

@DaniAcu
Copy link
Contributor Author

DaniAcu commented Jan 10, 2023

@eps1lon I updated based on main and fix a error after merge. Could you re check it?

Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for letting this slide. This is going in the right direction.

Comment on lines 108 to 110
// guard against using `level` option with any role other than `heading`
if (level !== undefined && role !== 'heading') {
throw new Error(`Role "${role}" cannot have "level" property.`)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The double nesting is intentional to be consistent with the other option. It allows folding away options in your code editor you're not interested in. Please revert this change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohh! It's to be cosisten with the other queries right? Sounds good.

Comment on lines 116 to 143
const roleValue = node.getAttribute('role')
/* istanbul ignore next */
const roleValue = node.getAttribute('role') ?? ''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use a non-nullable assertion instead. Generally, let's not change any runtime in these migrations. We can do them in a follow-up.

We don't need the fallback since roleValue will always be defined since we just confirmed that node.hasAttribute('role')

@@ -129,7 +156,7 @@ function queryAllByRole(
return matcher(firstWord, node, role, matchNormalizer)
}

const implicitRoles = getImplicitAriaRoles(node)
const implicitRoles = getImplicitAriaRoles(node) as string[]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: what would the type be without the cast`

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

role-helpers need migration to typescript so it return any[]

Array.from(container.children).forEach(childElement => {
Array.from(
/* istanbul ignore next */
container?.children ?? [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Let's not change runtime and use type casting/assertions instead.

@@ -178,15 +178,9 @@ export async function testByRole() {
console.assert(queryByRole(element, 'button', {name: /^Log/}) === null)
console.assert(
queryByRole(element, 'button', {
name: (name, el) => name === 'Login' && el.hasAttribute('disabled'),
name: (name, el) => name === 'Login' && !!el?.hasAttribute('disabled'),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why does this need to change?

It's also pretty hard to grok what this is doing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Name require a boolean value. el variable could be undefined based on typing. The boolean casting is because type result could be udefined by el or boolean by hasAttribute

Maybe is more readable like that

name: (name, el) => name === 'Login' && !!el && el.hasAttribute('disabled'),

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is el nullable? Shouldn't that always be defined?

console.assert(queryByRole(element, 'foo') === null)
console.assert(queryByRole(element, /foo/) === null)
console.assert(screen.queryByRole('foo') === null)
console.assert(screen.queryByRole(/foo/) === null)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove these?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because with the right typing it not be allowed to do it. If we want to continue using it, we could skip typescript in ths couple of lines

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this not allowed? query* can return a nullable value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because "foo" is not a valid role according to types

| RegExp
| string
| ((accessibleName: string, element: Element) => boolean)
name?: RegExp | string | MatcherFunction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original signature included useful type hints (e.g. accessibleName). These are lost with a generic MatcherFunction.

Could we instead use a concrete NameMatcherFunction to get type hints and keep the improved union?

role: ByRoleMatcher,
options?: ByRoleOptions,
) => T[]
export type AllByRole = QueryMethod<
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. By using the generic QueryMethod we loose type hints. Let's type it out properly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh okay, I think QueryMethod was the right way. Do you know what are the type hints that we lose that you mention.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to take in care when I work in that

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In editor suggestions:
original:
Screenshot 2023-01-10 at 17 46 40

your change:

Screenshot 2023-01-10 at 17 46 53

Notice the name of the parameters

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ohhh!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@DaniAcu
Copy link
Contributor Author

DaniAcu commented Jan 16, 2023

@eps1lon hi, i keep the changes simple and i dismiss any change that affect run time. There some ts comments, but I think it's fine for the migration. There others things that need to be change in general, but for the proposal of this PR, i think that with this changes is enough.

Let me know if there any thing that you look weird in this changes.

And thanks in advance for the time that you take to revisit it

@DaniAcu DaniAcu requested a review from eps1lon January 16, 2023 02:54
@eps1lon eps1lon changed the base branch from main to alpha February 6, 2023 17:55
@eps1lon eps1lon force-pushed the fix/migrate-role-to-ts branch from d72f79a to c88c915 Compare February 6, 2023 18:15
@eps1lon eps1lon force-pushed the fix/migrate-role-to-ts branch from c88c915 to 9e5d4b4 Compare February 6, 2023 18:19
@eps1lon eps1lon changed the title fix: migrate role to ts chore: migrate role to ts Feb 6, 2023
Copy link
Member

@eps1lon eps1lon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some finishing touches. Thank you for sticking with it!

@eps1lon eps1lon changed the title chore: migrate role to ts chore: Migrate ByRole to TypeScript Feb 6, 2023
@eps1lon eps1lon merged commit 42809fe into testing-library:alpha Feb 6, 2023
@eps1lon
Copy link
Member

eps1lon commented Feb 6, 2023

@all-contributors add @DaniAcu for code

@allcontributors
Copy link
Contributor

@eps1lon

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants