Skip to content

Make fuzzy matching opt-in instead of default #31

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 10 commits into from
May 6, 2018

Conversation

alexkrolick
Copy link
Collaborator

What:

Fixes #30

Why:

How:

Checklist:

  • Documentation
  • Tests
  • Ready to be merged
  • Added myself to contributors table

@alexkrolick alexkrolick requested a review from kentcdodds May 5, 2018 02:52
@alexkrolick alexkrolick force-pushed the no-fuzzy branch 2 times, most recently from fd08b01 to 425083f Compare May 5, 2018 05:12
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.

Looking great! I've got a few comments.

@@ -129,6 +129,114 @@ test('can get elements by data-testid attribute', () => {
expect(queryByTestId('first-name')).not.toBeInTheDOM()
})

test('queries passed strings match case-sensitive exact strings', () => {
Copy link
Member

Choose a reason for hiding this comment

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

For these two test cases, I think we could improve things slightly with jest-in-case. Could you look into that please?

src/matches.js Outdated
@@ -1,8 +1,10 @@
function matches(textToMatch, node, matcher) {
function fuzzyMatches(textToMatch, node, matcher, collapseWhitespace = true) {
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps for both of these the forth arg should be an options argument. This would have the benefit of being named so we don't need the constant in the queries file and also if we decide to allow forget configuration we could extend this with more options without trouble.

In addition, I wonder if we should allow users to configure this in their own calls to the queries by forwarding the options they pass to this 🤔 would that complicate things too much? If so, let's do it later if at all.

Copy link
Collaborator Author

@alexkrolick alexkrolick May 5, 2018

Choose a reason for hiding this comment

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

Ok, implemented. I also separated trim and collapseWhitespace - everything gets trimmed by default but only content is collapsed (not attributes).

@kentcdodds
Copy link
Member

Sorry, me merging #34 gave you some conflicts :-(

- Changes queries to default to exact string matching
- Can opt-in to fuzzy matches by passing { exact: true } as the last arg
- Queries that search inner text collapse whitespace
  (queryByText, queryByLabelText)

This is a breaking change!
@alexkrolick
Copy link
Collaborator Author

That's ok, rebased.

@alexkrolick
Copy link
Collaborator Author

I think it's ready now 🛫

README.md Outdated
* In most cases using a regex instead of a string gives you more control over
fuzzy matching and should be preferred over `{ exact: false }`.
* `trim`: Defaults to `true`; trim leading and trailing whitespace.
* `collapseWhitespace`: Defaults to `false` for attribute queries and `true` for content queries (i.e., `queryByLabelText`, `queryByText`). Collapses inner whitespace (newlines, tabs, repeated spaces) into a single space.
Copy link
Member

Choose a reason for hiding this comment

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

It just occurred to me. In what situations would not trimming of collapsing white space be desirable? The user never sees the text as anything but collapsed and trimmed right? Should we even have these options?

Copy link
Collaborator Author

@alexkrolick alexkrolick May 5, 2018

Choose a reason for hiding this comment

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

Line wrapping depends on CSS white-space and other properties. I'm leery of aggressively normalizing the DOM without a way to turn it off.

The defaults are fine in most cases though.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe they should default for true for everything...

This comment was marked as resolved.

This comment was marked as resolved.

This comment was marked as resolved.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done

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.

Yep, this is fantastic. Thanks!

@kentcdodds kentcdodds merged commit cde0cdf into testing-library:master May 6, 2018
@alexkrolick alexkrolick deleted the no-fuzzy branch May 6, 2018 03:17
@kentcdodds
Copy link
Member

🎉 This PR is included in version 2.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

Breaking change proposal: Use exact string match by default
2 participants