Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Provide description of how to browser/integration/acceptance test #4116

Closed
jackkinsella opened this issue Jul 9, 2020 · 7 comments
Closed

Comments

@jackkinsella
Copy link

jackkinsella commented Jul 9, 2020

Reproduction steps

Visit your homepage and interact with the component in the console. You can get a reference to one of them with:

var select = document.querySelector(".basic-single")

Now try interacting with it

select.click()
select.dispatchEvent(new Event("click"))

Neither work, which makes browser testing challenging.

Background

Most professional teams these days do some form of end-to-end testing, ours included. The only component in our stack that we haven't been able to figure out is react-select. I recognize that we're probably missing something obvious so your assistance would be appreciated.

Could you clarify how Selenium etc. (or just pure Javascript) can be used to drive the this select box from the outside? Many thanks

@ebonow
Copy link
Collaborator

ebonow commented Jul 9, 2020

Greetings @jackkinsella,

I agree that this behavior is not necessarily straight forward, so I will attempt to unravel what I have learned from exploring the source code a bit.

First, you are targeting the select wrapper itself. Your target node should be the Control component. The Control should always be first child component of container, but classNames would work too ('.select__control' in this case).

Secondly, the event listener on the Control is not a click event, but rather a mousedown event (presumably for performance reasons or to avoid overriding handlers, but I'm very curious myself why this is)

Borrowing some code from StackOverflow, we can accomplish a native mousedown event with the following:

function triggerMouseEvent (node, eventType) {
    var clickEvent = document.createEvent ('MouseEvents');
    clickEvent.initEvent (eventType, true, true);
    node.dispatchEvent (clickEvent);
}

Putting it all together,

var select = document.querySelector(".basic-single").children[0];
triggerMouseEvent(select, 'mousedown');

I'm sure there's several ways to encapsulate all of this for your testing suite and for testing various react-selects, but hopefully this is a good start.

@jackkinsella
Copy link
Author

jackkinsella commented Jul 10, 2020

Thanks for the response Eric. The use of another type of event internally helps explain why a standard click might not have worked.

Did the code you pasted above work for you to open up the select box on the React-Select homepage? Unfortunately it had no effect -- well at least no visible effect, for me in Chrome v83 (also no effect when I tried in Firefox). It's actually quite a mystery to me how I'm able to click it in the browser but can't control it via JS. I guess I've arrived at a gap in my understanding of how JS and the browser works.

An example overall goal in a testing workflow would be to select the value "Red" from the first select box, so let's work towards getting a snippet of JS that achieves that.

@ebonow
Copy link
Collaborator

ebonow commented Jul 10, 2020

@jackkinsella I was able to get the menu to open but through the use of a timeout since it would not work while I had focus on the console window. Maybe try setting a 3 second timeout and switch focus back to the page to see if that works for you.

@jackkinsella
Copy link
Author

jackkinsella commented Jul 11, 2020 via email

@ebonow
Copy link
Collaborator

ebonow commented Jul 11, 2020

One caveat would be that it won't work if the select already has focus. Also noticed that the DOM of the Select changes depending on whether the document is focused (some kind of aria tag) so this targeting won't work when the console has active focus.

The timeout is simply to ensure time so that the tester can return focus to the document window (debugee) and not the inspector window.

Agreed that none if this is ideal, but again hoping to get closer to something that can work

@bladey bladey added category/testing issue/reviewed Issue has recently been reviewed (mid-2020) labels Jul 16, 2020
@bladey bladey added issue/reviewed Issue has recently been reviewed (mid-2020) and removed issue/reviewed Issue has recently been reviewed (mid-2020) labels Aug 24, 2020
@sabinasiddiqi
Copy link

+1

@ebonow ebonow added the category/documentation Issues or PRs about documentation or the website itself label Dec 3, 2020
@ondrejsevcik
Copy link

ondrejsevcik commented Nov 11, 2021

Seems there is library that solves this issue https://github.com/romgain/react-select-event

It would be great if that repo would become part of this repo and make it official API for testing.

@dcousens dcousens removed category/documentation Issues or PRs about documentation or the website itself category/testing issue/reviewed Issue has recently been reviewed (mid-2020) labels Oct 31, 2022
Repository owner locked and limited conversation to collaborators Oct 31, 2022
@dcousens dcousens converted this issue into discussion #5449 Oct 31, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants