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

toHaveSelectionValue #289

Closed
diegohaz opened this issue Aug 23, 2020 · 9 comments · Fixed by #637
Closed

toHaveSelectionValue #289

diegohaz opened this issue Aug 23, 2020 · 9 comments · Fixed by #637
Labels
enhancement New feature or request

Comments

@diegohaz
Copy link
Contributor

diegohaz commented Aug 23, 2020

Describe the feature you'd like:

I'm testing a combobox element with a behavior that is quite similar to the address bar in the browser. That is, when you start typing, it automatically completes the text with the first suggestion and highlights the completion string.

I would like to test this selection behavior.

Suggested implementation:

function toHaveSelectionValue(element, value) {
  const { selectionStart, selectionEnd } = element;
  const selectionValue = element.value.slice(selectionStart, selectionEnd);
  return {
    pass: selectionValue === value,
    ...
  };
}

Describe alternatives you've considered:

I can check the selectionStart and selectionEnd properties on the HTMLInputElement, but this is not as easy to read. And I also have to explicitly cast the element if I'm using TypeScript.

const input = getByLabelText("Fruits") as HTMLInputElement;
// Expect "pple" to be selected in "Apple"
expect(input.selectionStart).toBe(1);
expect(input.selectionEnd).toBe(5);

So, for now, I'm using an expectSelectionValue util function in my app.

Teachability, Documentation, Adoption, Migration Strategy:

toHaveSelectionValue

toHaveSelectionValue(value: string)

This allows you to check whether the given textbox element has the specified selected (highlighted) value. It accepts <input type="text"> and <textarea> elements.

Examples

<input type="text" value="text" data-testid="input" />
Using DOM Testing Library
const input = getByTestId('input')

input.setSelectionRange(1, 3)

expect(input).toHaveSelectionValue('ex')
@gnapse
Copy link
Member

gnapse commented Aug 24, 2020

This could be a nice addition indeed.

One thing I'd suggest is discuss the name a bit. toHaveSelectionValue can suggest it being about what's the currently selected value in a list or something like that. How about something like toHaveSelectedText instead?

@nickserv
Copy link
Member

I'd be fine with just toHaveSelection.

@nickserv nickserv added the enhancement New feature or request label Aug 24, 2020
@diegohaz
Copy link
Contributor Author

I think selection (as opposed to selected) makes more sense because that's the term used by the DOM (element.selectionStart, element.selectionEnd, element.setSelectionRange, window.getSelection()).

I also thought about toHaveSelection, but I would expect it to work also on elements other than text fields. Since my proposal is only for text fields, value matches the DOM attribute. But if we agree that it should work for other elements as well, I would go for toHaveSelection or toHaveSelectionText. 👍

@gnapse
Copy link
Member

gnapse commented Aug 24, 2020

Sure. Selection makes more sense than selected. toHaveSelection is great.

@pwolaq
Copy link
Contributor

pwolaq commented Oct 15, 2021

Hello, I would like to try to implement this one if it is still needed.

@silviuaavram
Copy link
Contributor

Hey @pwolaq @gnapse I encountered a use case for testing the selection in a work project, and while I managed to perform the testing with attributes such .selectionStart and .selectionEnd, I found this issue and the PR from a few years back, and decided to revive it and finish it so we can merge it to the library.

Could you help me out with a review, please? Let me know if you need more changes or info. Thanks!

@gnapse
Copy link
Member

gnapse commented Oct 16, 2024

This new selector is about to be released (see #637). Thanks to @pwolaq and @silviuaavram! And thanks, @diegohaz, for the proposal.

@gnapse
Copy link
Member

gnapse commented Oct 16, 2024

@all-contributors please add @diegohaz for ideas

Copy link
Contributor

@gnapse

I've put up a pull request to add @diegohaz! 🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants