Skip to content

fix: ignore getBy inside of within for prefer-presence-queries on absence queries - #716

Closed
nathanmmiller wants to merge 1 commit into
testing-library:mainfrom
nathanmmiller:pr/handle-within-false-positive-in-prefer-presence-queries
Closed

nathanmmiller wants to merge 1 commit into
testing-library:mainfrom
nathanmmiller:pr/handle-within-false-positive-in-prefer-presence-queries

Conversation

@nathanmmiller

Copy link
Copy Markdown
Contributor

Checks

Changes

  • Ignore getBy* nodes inside of within() for prefer-presence-queries when checking absence queries

Context

Fixes #518 [Well, partially fixes it.]

@Belco90 Belco90 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Hi @nathanmmiller! Thanks for your PR. I left a comment asking for more tests, since I think your implementation is not covering all cases as expected.

expect(submitButton).not.toBeInTheDocument()
`,
`// checking absence on getBy* inside a within with queryBy* outside the within
expect(within(screen.getByRole("button")).queryByText("Hello")).not.toBeInTheDocument()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Could you add another test to check that chained queries from within are reported as expected tho?

expect(within(screen.getByRole("button")).getByText("Hello")).not.toBeInTheDocument()

The example above should be reported, but I don't think it will be by your implementation.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good call. I think maybe I need to rethink the way I am handling this - it's not sufficient to say "ignore everything inside a within." Lemme rethink and re-PR.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Actually I think this does correctly handle this use case. I've added the unit test as requested and will re-raise the PR - it correctly deals with this exact case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@Belco90 reopened at #717, with the additional test case

@nathanmmiller
nathanmmiller deleted the pr/handle-within-false-positive-in-prefer-presence-queries branch January 16, 2023 22:29
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.

False positives with testing-library/prefer-presence-queries

2 participants