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

toHaveDisplayValue custom matcher #221

Closed
cloud-walker opened this issue Mar 24, 2020 · 7 comments
Closed

toHaveDisplayValue custom matcher #221

cloud-walker opened this issue Mar 24, 2020 · 7 comments

Comments

@cloud-walker
Copy link
Contributor

cloud-walker commented Mar 24, 2020

I'm testing a labeled select that I query with getByLabelText, I then want to assert that the select has a default display name, but I can't find a jest matcher for that, here a snippet example:

const select = screen.getByLabelText('Fruits')
expect(select).toBeVisible()
expect(select).toHaveDisplayValue('Select a fruit')

For now I think I should write a custom matcher myself, or a more complicated query 🤔

@gnapse
Copy link
Member

gnapse commented Mar 24, 2020

I'm sorry, not sure what you refer to with "default display name" of an element. Can you expand a bit?

@cloud-walker
Copy link
Contributor Author

cloud-walker commented Mar 24, 2020

Sure, I mean the currently selected item, so if I have:

<label htmlFor="fruit">
  Fruits
</label>
<select name="fruit" id="fruit">
  <option value="" disabled selected>Select a fruit...</option>
  <option value="apple">Apple</option>
  <option value="ananas">Ananas</option>
  <option value="cherry">Cherry</option>
</select>

I want to be able to query the select by label text, and use the matcher to check that the displayed text is the one I want:

const select = screen.getByLabelText('Fruits')
expect(select).toBeVisible()
expect(select).toHaveDisplayValue('Select a fruit...')

// And if I want I can use it again to test changed values, like
fireEvent.change(select, {target: {value: {'apple'}}}})

expect(select).toHaveDisplayValue('Apple') // with capital letter 😉 

@cloud-walker
Copy link
Contributor Author

There is already a testing-library query for that, but I want the matcher counterpart: https://testing-library.com/docs/dom-testing-library/api-queries#bydisplayvalue

@cloud-walker
Copy link
Contributor Author

Is more "display value" than "display name" sooorry!

@cloud-walker cloud-walker changed the title toHaveDisplayName custom matcher toHaveDisplayValue custom matcher Mar 24, 2020
@gnapse
Copy link
Member

gnapse commented Mar 24, 2020

Oh, I see. Makes sense. I'm all for it.

@cloud-walker
Copy link
Contributor Author

I'll see if I can make a PR

@afontcu
Copy link
Member

afontcu commented Apr 28, 2020

Solved in #223

@afontcu afontcu closed this as completed Apr 28, 2020
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

No branches or pull requests

3 participants