Skip to content

get/query by value #35

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 2 commits into from
May 6, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ when a real user uses it.
* [`getByText`](#getbytext)
* [`getByAltText`](#getbyalttext)
* [`getByTitle`](#getbytitle)
* [`getByValue`](#getbyvalue)
* [`getByTestId`](#getbytestid)
* [`wait`](#wait)
* [`waitForElement`](#waitforelement)
Expand Down Expand Up @@ -320,6 +321,26 @@ Returns the element that has the matching `title` attribute.
const deleteElement = getByTitle(container, 'Delete')
```

### `getByValue`

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

Returns the element that has the matching value.

```javascript
// <input type="text" id="lastName" defaultValue="Norris" />
const lastNameInput = getByValue('Norris')
```

### `getByTestId`

```typescript
Expand Down
8 changes: 8 additions & 0 deletions src/__tests__/__snapshots__/element-queries.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ exports[`get throws a useful error message 6`] = `
</div>"
`;

exports[`get throws a useful error message 7`] = `
"Unable to find an element with the value: LucyRicardo.

<div>
<div />
</div>"
`;

exports[`label with no form control 1`] = `
"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.

Expand Down
15 changes: 15 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ test('get throws a useful error message', () => {
getByTestId,
getByAltText,
getByTitle,
getByValue,
} = render('<div />')
expect(() => getByLabelText('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() =>
Expand All @@ -33,6 +34,7 @@ test('get throws a useful error message', () => {
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingSnapshot()
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingSnapshot()
})

test('can get elements by matching their text content', () => {
Expand Down Expand Up @@ -132,6 +134,19 @@ test('query/get element by its title', () => {
expect(queryByTitle('Delete').id).toEqual('2')
})

test('query/get element by its value', () => {
const {getByValue, queryByValue} = render(`
<div>
<input placeholder="name" type="text"/>
<input placeholder="lastname" type="text" value="Norris"/>
<input placeholder="email" type="text"/>
</div>
`)

expect(queryByValue('Norris').placeholder).toEqual('lastname')
expect(getByValue('Norris').placeholder).toEqual('lastname')
})

test('can get elements by data-testid attribute', () => {
const {queryByTestId} = render(`<div data-testid="firstName"></div>`)
expect(queryByTestId('firstName')).toBeInTheDOM()
Expand Down
22 changes: 22 additions & 0 deletions src/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ const queryByTestId = queryByAttribute.bind(null, 'data-testid')
const queryAllByTestId = queryAllByAttribute.bind(null, 'data-testid')
const queryByTitle = queryByAttribute.bind(null, 'title')
const queryAllByTitle = queryAllByAttribute.bind(null, 'title')
const queryByValue = queryByAttribute.bind(null, 'value')
const queryAllByValue = queryAllByAttribute.bind(null, 'value')

function queryAllByAltText(
container,
Expand Down Expand Up @@ -166,6 +168,22 @@ function getByTitle(...args) {
return firstResultOrNull(getAllByTitle, ...args)
}

function getAllByValue(container, value, ...rest) {
const els = queryAllByValue(container, value, ...rest)
if (!els.length) {
throw new Error(
`Unable to find an element with the value: ${value}. \n\n${debugDOM(
container,
)}`,
)
}
return els
}

function getByValue(...args) {
return firstResultOrNull(getAllByValue, ...args)
}

function getAllByPlaceholderText(container, text, ...rest) {
const els = queryAllByPlaceholderText(container, text, ...rest)
if (!els.length) {
Expand Down Expand Up @@ -264,6 +282,10 @@ export {
queryAllByTitle,
getByTitle,
getAllByTitle,
queryByValue,
queryAllByValue,
getByValue,
getAllByValue,
}

/* eslint complexity:["error", 14] */