Skip to content

feat: Add toHaveValue matcher #82 #90

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 1 commit into from
Jun 7, 2019
Merged

feat: Add toHaveValue matcher #82 #90

merged 1 commit into from
Jun 7, 2019

Conversation

lukaszfiszer
Copy link
Contributor

@lukaszfiszer lukaszfiszer commented Apr 1, 2019

What:
Add toHaveValue matcher

Why:
As discussed in #82

How:
Reused a lot of existing logic from toHaveFormValue and moved some internal functions to utils.js so that they can be shared between both modules.

I had a lot of thoughts on how to implement this matcher for <input type="checkbox" /> and <input type="radio" />, because those elements value can be different depending on other elements from the same group (sharing the same name). Finally I've decided to throw an error when a checkbox or radio input is passed, suggesting users to use toHaveFormValue instead. In the future, I think I would most make sense to implement toBeChecked matcher - a perfect pair with to toHaveValue for single form elements.

Checklist:

  • Documentation
  • Tests
  • Updated Type Definitions
  • Ready to be merged
  • Added myself to contributors table

Copy link
Member

@gnapse gnapse left a comment

Choose a reason for hiding this comment

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

I think I'm ok with all this, including what you decided about the checkbox and radio buttons. But I want to give it a second thought before approving, because right now I'm on a layover after missing a flight connection, and tired as hell. So just in case I'm not thinking clearly enough. In the mean time, I left a couple of comments on a small typo.

BTW, thanks for this!

@lukaszfiszer
Copy link
Contributor Author

lukaszfiszer commented Apr 6, 2019

@gnapse I have fix the typos.

Another idea that I had for checkbox and radio inputs was to allow the user to pass a parent element of those elements:

<div data-testid="contact">
   <label><input type="radio" name="contact" value="email">Email</label>
   <label><input type="radio" name="contact" value="phone">Phone</label>
   <label><input type="radio" name="contact" value="post">Post</label>
</div>
expect(getByTestId('contact')).toHaveValue('phone')

But then probably you could just use toHaveFormValues for that.

Let me know what you think.

BTW I see that one of the tests is failing (which is weird) - I will fix it once we agree on the final form of this PR

@kentcdodds
Copy link
Member

What do we need to do to help get this across the finish line?

@gnapse
Copy link
Member

gnapse commented May 17, 2019

I probably forgot to keep track of this and approve or request changes at the time. But now it seems there are conflicts, and also the build is failing for some reason (could it have something to do with the change of url of the github repo when moved to an org @kentcdodds? When I visit the travis details link above I get "We couldn't find the repository gnapse/jest-dom").

@lukaszfiszer sorry about not being on top of this. Can you please fix the conflicts? We'll figure out the travis issue in the mean time.

@lukaszfiszer
Copy link
Contributor Author

@gnapse No worries. I will fix the conflicts over the weekend.

@lukaszfiszer
Copy link
Contributor Author

@gnapse rebased against master and fixed the broken test. ready to merge

@@ -20,5 +20,6 @@ declare namespace jest {
text: string | RegExp,
options?: {normalizeWhitespace: boolean},
): R
toHaveValue(value?: string | string[]): R
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should this be value?: string | string[] | number?

README.md Outdated
### `toHaveValue`

```typescript
toHaveValue(value: string | string[])
Copy link
Collaborator

Choose a reason for hiding this comment

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

And should this one be value?: string | string[] | number?

`)

expect(queryByTestId('with')).toHaveValue('foo')
expect(queryByTestId('with')).not.toHaveValue('bar')
Copy link
Collaborator

Choose a reason for hiding this comment

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

Would it make sense to add a test case like this?

expect(queryByTestId('with')).toHaveValue();

The corresponding .not.toHaveValue() case is already there for empty.

@lukaszfiszer
Copy link
Contributor Author

@jgoz Thanks for review. I've fixed the typings, added additional test cases and fixed the behaviour of .toHaveValue() (without argument) - it asserts if input/select has any value.

@jgoz
Copy link
Collaborator

jgoz commented Jun 5, 2019

@lukaszfiszer sorry for the delayed response, I missed the notification on this. Just two very minor issues that I can see, one of which is causing the CI failure.

@lukaszfiszer
Copy link
Contributor Author

@jgoz fixed issues and squash-rebased against master. Hope to get it merged!

Copy link
Collaborator

@jgoz jgoz left a comment

Choose a reason for hiding this comment

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

👍 looks good to me

README.md Outdated
```javascript
const {getByTestId} = render(/* Rendered HTML */)

const textInput = getByTestId('[data-testid="input-text"]')
Copy link
Member

Choose a reason for hiding this comment

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

I think these should be: getByTestId('input-text') and so-forth.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good catch! Fixed.

@gnapse gnapse merged commit fd747bf into testing-library:master Jun 7, 2019
@gnapse
Copy link
Member

gnapse commented Jun 8, 2019

🎉 This PR is included in version 3.5.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@gnapse gnapse added the released label Jun 8, 2019
@lukaszfiszer lukaszfiszer deleted the pr/to-have-value branch June 8, 2019 11:37
@connormeredith connormeredith mentioned this pull request Oct 8, 2019
4 tasks
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