Skip to content

feat(queryByCurrentValue) #169

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 16 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .all-contributorsrc
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,15 @@
"contributions": [
"code"
]
},
{
"login": "eunjae-lee",
"name": "Eunjae Lee",
"avatar_url": "https://avatars3.githubusercontent.com/u/499898?v=4",
"profile": "https://twitter.com/eunjae_lee",
"contributions": [
"code"
]
}
]
}
54 changes: 52 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
[![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-44-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-45-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs]
[![Code of Conduct][coc-badge]][coc]

Expand Down Expand Up @@ -86,6 +86,7 @@ when a real user uses it.
- [`getByAltText`](#getbyalttext)
- [`getByTitle`](#getbytitle)
- [`getByValue`](#getbyvalue)
Copy link
Member

Choose a reason for hiding this comment

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

Let's remove the docs for getByValue and getBySelectText because we want to encourage people to use the getByDisplayValue instead.

- [`getByDisplayValue`](#getbydisplayvalue)
- [`getByRole`](#getbyrole)
- [`getByTestId`](#getbytestid)
- [`wait`](#wait)
Expand Down Expand Up @@ -409,6 +410,55 @@ Returns the element that has the matching value.
const lastNameInput = getByValue('Norris')
```

### `getByDisplayValue`

```typescript
getByDisplayValue(
container: HTMLElement,
value: TextMatch,
options?: {
exact?: boolean = true,
collapseWhitespace?: boolean = false,
trim?: boolean = true,
}): HTMLElement
```

Returns the element that has the matching display value.
Copy link
Member

Choose a reason for hiding this comment

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

Returns the input, textarea, or select element that has the matching display value.


#### `input`

```javascript
// <input type="text" id="lastName" />
// document.getElementById('lastName').value = 'Norris'

const lastNameInput = getByDisplayValue('Norris')
Copy link
Member

Choose a reason for hiding this comment

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

const lastNameInput = getByDisplayValue(document.body, 'Norris')

```

#### `textarea`

```javascript
// <textarea id="messageTextArea"></textarea>
// document.getElementById('messageTextArea').value = 'Hello World'

const messageTextArea = getByDisplayValue('Hello World')
Copy link
Member

Choose a reason for hiding this comment

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

const messageTextArea = getByDisplayValue(document.body, 'Hello World')

```

#### `select`

```javascript
// <select id="state-select" data-testid="state">
// <option value="">State</option>
// <option value="AL">Alabama</option>
// <option selected value="AK" >Alaska</option>
// <option value="AZ">Arizona</option>
// </select>

const selectElement = getByDisplayName('Alaska')
console.log(selectElement.id)
Copy link
Member

Choose a reason for hiding this comment

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

Remove the console.log here (it's not necessary and it's nice to be consistent with the others). Then fix it to be:

const selectElement = getByDisplayName(document.body, 'Alaska')

```

In case of `select`, it gets an element by the display name of its selected option.

### `getByRole`

```typescript
Expand Down Expand Up @@ -1142,7 +1192,7 @@ Thanks goes to these people ([emoji key][emojis]):
| [<img src="https://avatars2.githubusercontent.com/u/21689428?v=4" width="100px;"/><br /><sub><b>Jonathan Stoye</b></sub>](http://jonathanstoye.de)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JonathanStoye "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4126644?v=4" width="100px;"/><br /><sub><b>Sanghyeon Lee</b></sub>](https://github.com/yongdamsh)<br />[💡](#example-yongdamsh "Examples") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba </b></sub>](https://github.com/Dajust)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=Dajust "Documentation") [🤔](#ideas-Dajust "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/340761?v=4" width="100px;"/><br /><sub><b>Wayne Crouch</b></sub>](https://github.com/wgcrouch)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wgcrouch "Code") | [<img src="https://avatars1.githubusercontent.com/u/4996462?v=4" width="100px;"/><br /><sub><b>Ben Elliott</b></sub>](http://benjaminelliott.co.uk)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=benelliott "Code") | [<img src="https://avatars3.githubusercontent.com/u/577921?v=4" width="100px;"/><br /><sub><b>Ruben Costa</b></sub>](http://nuances.co)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=rubencosta "Code") | [<img src="https://avatars2.githubusercontent.com/u/4982001?v=4" width="100px;"/><br /><sub><b>Robert Smith</b></sub>](http://rbrtsmith.com/)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3Arbrtsmith "Bug reports") [🤔](#ideas-rbrtsmith "Ideas, Planning, & Feedback") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=rbrtsmith "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=dadamssg "Code") | [<img src="https://avatars1.githubusercontent.com/u/186971?v=4" width="100px;"/><br /><sub><b>Neil Kistner</b></sub>](https://neilkistner.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=wyze "Code") | [<img src="https://avatars3.githubusercontent.com/u/1448597?v=4" width="100px;"/><br /><sub><b>Ben Chauvette</b></sub>](http://bdchauvette.net/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=bdchauvette "Code") | [<img src="https://avatars2.githubusercontent.com/u/777527?v=4" width="100px;"/><br /><sub><b>Jeff Baumgardt</b></sub>](https://github.com/JeffBaumgardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=JeffBaumgardt "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/4658208?v=4" width="100px;"/><br /><sub><b>Matan Kushner</b></sub>](http://matchai.me)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Documentation") [🤔](#ideas-matchai "Ideas, Planning, & Feedback") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=matchai "Tests") | [<img src="https://avatars2.githubusercontent.com/u/5779538?v=4" width="100px;"/><br /><sub><b>Alex Wendte</b></sub>](http://www.wendtedesigns.com/)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=themostcolm "Tests") | [<img src="https://avatars0.githubusercontent.com/u/2196208?v=4" width="100px;"/><br /><sub><b>Tamas Fodor</b></sub>](https://github.com/ruffle1986)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=ruffle1986 "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/14793495?v=4" width="100px;"/><br /><sub><b>Benjamin Eckardt</b></sub>](https://github.com/BenjaminEckardt)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=BenjaminEckardt "Code") | [<img src="https://avatars3.githubusercontent.com/u/205752?v=4" width="100px;"/><br /><sub><b>Ryan Campbell</b></sub>](https://github.com/campbellr)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=campbellr "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/1335519?v=4" width="100px;"/><br /><sub><b>Taylor Briggs</b></sub>](https://taylor-briggs.com)<br />[⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=TaylorBriggs "Tests") | [<img src="https://avatars2.githubusercontent.com/u/132233?v=4" width="100px;"/><br /><sub><b>John Gozde</b></sub>](https://github.com/jgoz)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=jgoz "Code") | [<img src="https://avatars2.githubusercontent.com/u/3382565?v=4" width="100px;"/><br /><sub><b>C. T. Lin</b></sub>](https://github.com/chentsulin)<br />[📖](https://github.com/kentcdodds/dom-testing-library/commits?author=chentsulin "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/5312329?v=4" width="100px;"/><br /><sub><b>Terrence Wong</b></sub>](http://terrencewwong.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=terrencewwong "Code") | [<img src="https://avatars0.githubusercontent.com/u/12230408?v=4" width="100px;"/><br /><sub><b>Soo Jae Hwang</b></sub>](https://www.ossfinder.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=misoguy "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/19773?v=4" width="100px;"/><br /><sub><b>Royston Shufflebotham</b></sub>](https://github.com/RoystonS)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3ARoystonS "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Tests") | [<img src="https://avatars0.githubusercontent.com/u/591673?v=4" width="100px;"/><br /><sub><b>Vadim Brodsky</b></sub>](http://www.vadimbrodsky.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=VadimBrodsky "Code") |
| [<img src="https://avatars0.githubusercontent.com/u/19773?v=4" width="100px;"/><br /><sub><b>Royston Shufflebotham</b></sub>](https://github.com/RoystonS)<br />[🐛](https://github.com/kentcdodds/dom-testing-library/issues?q=author%3ARoystonS "Bug reports") [💻](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Code") [📖](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Documentation") [⚠️](https://github.com/kentcdodds/dom-testing-library/commits?author=RoystonS "Tests") | [<img src="https://avatars0.githubusercontent.com/u/591673?v=4" width="100px;"/><br /><sub><b>Vadim Brodsky</b></sub>](http://www.vadimbrodsky.com)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=VadimBrodsky "Code") | [<img src="https://avatars3.githubusercontent.com/u/499898?v=4" width="100px;"/><br /><sub><b>Eunjae Lee</b></sub>](https://twitter.com/eunjae_lee)<br />[💻](https://github.com/kentcdodds/dom-testing-library/commits?author=eunjae-lee "Code") |

<!-- ALL-CONTRIBUTORS-LIST:END -->

Expand Down
61 changes: 61 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ test('getAll* matchers throw for 0 matches', () => {
getAllByPlaceholderText,
getAllByText,
getAllByRole,
getAllByDisplayValue,
} = render(`
<div role="container">
<label>No Matches Please</label>
Expand All @@ -361,6 +362,7 @@ test('getAll* matchers throw for 0 matches', () => {
expect(() => getAllByPlaceholderText('nope')).toThrow()
expect(() => getAllByText('nope')).toThrow()
expect(() => getAllByRole('nope')).toThrow()
expect(() => getAllByDisplayValue('nope')).toThrow()
})

test('queryAll* matchers return an array for 0 matches', () => {
Expand Down Expand Up @@ -565,4 +567,63 @@ test('getByText ignores script tags by default', () => {
expect(getAllByText(/hello/i, {ignore: false})).toHaveLength(3)
})

test('get/query input element by current value', () => {
const {
getByDisplayValue,
queryByDisplayValue,
getByTestId,
} = renderIntoDocument(`
<div>
<input placeholder="name" type="text" data-testid="name" value="Mercury" />
</div>
`)
expect(getByDisplayValue('Mercury').placeholder).toEqual('name')
expect(queryByDisplayValue('Mercury').placeholder).toEqual('name')

getByTestId('name').value = 'Norris'
expect(getByDisplayValue('Norris').placeholder).toEqual('name')
expect(queryByDisplayValue('Norris').placeholder).toEqual('name')
})

test('get/query select element by current value', () => {
const {
getByDisplayValue,
queryByDisplayValue,
getByTestId,
} = renderIntoDocument(`
<select id="state-select" data-testid="state">
<option value="">State</option>
<option value="AL">Alabama</option>
<option selected value="AK" >Alaska</option>
<option value="AZ">Arizona</option>
</select>
`)

expect(getByDisplayValue('Alaska').id).toEqual('state-select')
expect(queryByDisplayValue('Alaska').id).toEqual('state-select')

getByTestId('state').value = 'AL'
expect(getByDisplayValue('Alabama').id).toEqual('state-select')
expect(queryByDisplayValue('Alabama').id).toEqual('state-select')
})

test('get/query textarea element by current value', () => {
const {
getByDisplayValue,
queryByDisplayValue,
getByTestId,
} = renderIntoDocument(`
<textarea id="content-textarea" data-testid="content">
Hello
</textarea>
`)

expect(getByDisplayValue('Hello').id).toEqual('content-textarea')
expect(queryByDisplayValue('Hello').id).toEqual('content-textarea')

getByTestId('content').value = 'World'
expect(getByDisplayValue('World').id).toEqual('content-textarea')
expect(queryByDisplayValue('World').id).toEqual('content-textarea')
})

/* eslint jsx-a11y/label-has-for:0 */
46 changes: 46 additions & 0 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,33 @@ function queryByAltText(...args) {
return firstResultOrNull(queryAllByAltText, ...args)
}

function queryAllByDisplayValue(
container,
value,
{exact = true, collapseWhitespace = true, trim = true} = {},
) {
const matcher = exact ? matches : fuzzyMatches
const matchOpts = {collapseWhitespace, trim}
return Array.from(container.querySelectorAll(`input,textarea,select`)).filter(
node => {
if (node.tagName === 'SELECT') {
const selectedOptions = Array.from(node.options).filter(
option => option.selected,
)
return selectedOptions.some(optionNode =>
matcher(getNodeText(optionNode), optionNode, value, matchOpts),
)
} else {
return matcher(node.value, node, value, matchOpts)
}
},
)
}

function queryByDisplayValue(...args) {
return firstResultOrNull(queryAllByDisplayValue, ...args)
}

// getters
// the reason we're not dynamically generating these functions that look so similar:
// 1. The error messages are specific to each one and depend on arguments
Expand Down Expand Up @@ -325,6 +352,21 @@ function getBySelectText(...args) {
return firstResultOrNull(getAllBySelectText, ...args)
}

function getAllByDisplayValue(container, value, ...rest) {
const els = queryAllByDisplayValue(container, value, ...rest)
if (!els.length) {
throw getElementError(
`Unable to find an element with the value: ${value}.`,
container,
)
}
return els
}

function getByDisplayValue(...args) {
return firstResultOrNull(getAllByDisplayValue, ...args)
}

export {
queryByPlaceholderText,
queryAllByPlaceholderText,
Expand Down Expand Up @@ -358,6 +400,10 @@ export {
queryAllByValue,
getByValue,
getAllByValue,
queryByDisplayValue,
queryAllByDisplayValue,
getByDisplayValue,
getAllByDisplayValue,
queryByRole,
queryAllByRole,
getAllByRole,
Expand Down