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

getByValue cannot get element by dynamically assigned value #158

Closed
eunjae-lee opened this issue Nov 22, 2018 · 2 comments · Fixed by #169
Closed

getByValue cannot get element by dynamically assigned value #158

eunjae-lee opened this issue Nov 22, 2018 · 2 comments · Fixed by #169
Labels
help wanted Extra attention is needed question Further information is requested released

Comments

@eunjae-lee
Copy link
Contributor

  • dom-testing-library version: 3.12.4
  • react version: not using with react
  • node version: 8.11.1
  • npm (or yarn) version: npm 5.6.0

Relevant code or config:

test('get element by its dynamically assigned value', () => {
  const {getByValue, queryByValue, getByTestId} = renderIntoDocument(`
  <div>
    <input placeholder="name" type="text" data-testid="name" />
  </div>
  `)
  getByTestId('name').value = 'Norris'
  expect(getByValue('Norris').placeholder).toEqual('name')
  expect(queryByValue('Norris').placeholder).toEqual('name')
})

What you did:

There's an input element (without initial value attribute). I put some value in it with JavaScript.

What happened:

getByValue or queryByValue cannot get the element.

  ● get element by its dynamically assigned value

    Unable to find an element with the value: Norris.

Reproduction:

Problem description:

According to queryAllByAttribute at src/query-helpers.js, it's executing some code like

container.querySelectorAll("[value]")

Since the input element didn't have any value attribute at the first place, this query cannot get it.

Suggested solution:

It should still get input element unless this behaviour is intended.
Querying it with [value] didn't work. Getting value with node.getAttribute("value") didn't work either. So I guess the following code could solve the problem.

Array.from(container.querySelectorAll("input")).filter(node => node.value === value)

Let me know what you think!

@kentcdodds kentcdodds added help wanted Extra attention is needed question Further information is requested labels Nov 22, 2018
@alexkrolick
Copy link
Collaborator

alexkrolick commented Nov 28, 2018

The .value setter/getter and the DOM attribute aren't necessarily in sync (see Codepen), so you'd probably need to concat the results of both the current querySelectorAll and the proposed filtered DOM traversal, and maybe dedupe the results with some documented hierarchy (probably preferring the dynamic .value).

@kentcdodds
Copy link
Member

🎉 This issue has been resolved in version 3.14.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
help wanted Extra attention is needed question Further information is requested released
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants