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 5 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
59 changes: 59 additions & 0 deletions src/__tests__/element-queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -565,4 +565,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 {
getByCurrentValue,
queryByCurrentValue,
getByTestId,
} = renderIntoDocument(`
<div>
<input placeholder="name" type="text" data-testid="name" value="Mercury" />
</div>
`)
expect(getByCurrentValue('Mercury').placeholder).toEqual('name')
expect(queryByCurrentValue('Mercury').placeholder).toEqual('name')

getByTestId('name').value = 'Norris'
expect(getByCurrentValue('Norris').placeholder).toEqual('name')
expect(queryByCurrentValue('Norris').placeholder).toEqual('name')
})

test('get/query select element by current value', () => {
const {
getByCurrentValue,
queryByCurrentValue,
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(getByCurrentValue('Alaska').id).toEqual('state-select')
expect(queryByCurrentValue('Alaska').id).toEqual('state-select')

getByTestId('state').value = 'AL'
expect(getByCurrentValue('Alabama').id).toEqual('state-select')
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if it's at all confusing that we are expected to set the value to AL but when we use the query we search for Alabama.

I realize that's just how things work (the value for a select has a display value), but I feel like this could be a point of confusion for people. My guess is they'd prefer to select by AL instead which isn't what is shown to the user but feels more intuitive here. I'm not sure of the best solution to this... Maybe allow for either the display value or the actual value?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense. To end-users Alabama is a value, but AL is the actual value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

However, selectElem.value could be either 1, AL, or whatever user cannot know. In a perspective of writing tests as user would use it, I'd rather choose to restrict it to display value.
What do you think?

Copy link
Collaborator

@alexkrolick alexkrolick Dec 10, 2018

Choose a reason for hiding this comment

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

Hmm. To really avoid confusion, this could be called getByDisplayValue().

Technically an aria-menu is also compliant with this API: https://w3c.github.io/aria/#menu, https://w3c.github.io/aria/#aria-selected. In the ARIA case there doesn't seem to be any way to have a different "secret form value" vs "display value". EDIT: out of scope

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, I think displayValue is probably a better mental model, though it may be a tiny confusing for input and textarea where the display value is actually the actual value but I think that's alright. Besides, people should normally be getting these elements by their label anyway, so I'm not too concerned about this slightly less-awesome API.

Let's go with getByDisplayValue 👍

expect(queryByCurrentValue('Alabama').id).toEqual('state-select')
})

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

expect(getByCurrentValue('Hello').id).toEqual('content-textarea')
expect(queryByCurrentValue('Hello').id).toEqual('content-textarea')

getByTestId('content').value = 'World'
expect(getByCurrentValue('World').id).toEqual('content-textarea')
expect(queryByCurrentValue('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 queryAllByCurrentValue(
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 queryByCurrentValue(...args) {
return firstResultOrNull(queryAllByCurrentValue, ...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 getAllByCurrentValue(container, value, ...rest) {
const els = queryAllByCurrentValue(container, value, ...rest)
if (!els.length) {
throw getElementError(
`Unable to find an element with the value: ${value}.`,
container,
)
}
return els
}

function getByCurrentValue(...args) {
return firstResultOrNull(getAllByCurrentValue, ...args)
}

export {
queryByPlaceholderText,
queryAllByPlaceholderText,
Expand Down Expand Up @@ -358,6 +400,10 @@ export {
queryAllByValue,
getByValue,
getAllByValue,
queryByCurrentValue,
queryAllByCurrentValue,
getByCurrentValue,
getAllByCurrentValue,
queryByRole,
queryAllByRole,
getAllByRole,
Expand Down