You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey all. I have been needing to test React Select with Cypress lately, and until now I didn't have a great way to do so. Previously, I was selecting options by their automatic ID, which was entirely dependent on the ordering. I haven't been able to find any better solution here or online, so I figured I would share in case anyone else is having the same issues. If people find this useful, maybe it would be worthwhile to add a prop to inject test ID attributes to avoid hacky solutions like this one.
This is not ideal if you have dynamic content. The ordering of options can break your tests, which is not great :/
I put together this custom command that uses partial ID matching and inspects the text content of each option.
Cypress.Commands.add("getSelectOptionByText",(selectId,optionText)=>{returncy.get(`#${selectId}`).click().then(()=>{letfoundOption=null;// Iterate over partial matches for the option IDcy.get(`*[id^="react-select-${selectId}-option-"]`).each(($option)=>{if($option.text()===optionText){foundOption=$option;returnfalse;}}).then(()=>{returnfoundOption;});});});
I would appreciate any feedback on this solution, and I would be happy to assist with adding test IDs if we want to go down that path!
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Hey all. I have been needing to test React Select with Cypress lately, and until now I didn't have a great way to do so. Previously, I was selecting options by their automatic ID, which was entirely dependent on the ordering. I haven't been able to find any better solution here or online, so I figured I would share in case anyone else is having the same issues. If people find this useful, maybe it would be worthwhile to add a prop to inject test ID attributes to avoid hacky solutions like this one.
The old setup is shown below:
In the app:
In the test:
This is not ideal if you have dynamic content. The ordering of options can break your tests, which is not great :/
I put together this custom command that uses partial ID matching and inspects the text content of each option.
I would appreciate any feedback on this solution, and I would be happy to assist with adding test IDs if we want to go down that path!
Beta Was this translation helpful? Give feedback.
All reactions