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

React 18 useId() conflict with jQuery #27922

Open
quantizor opened this issue Sep 27, 2023 · 2 comments
Open

React 18 useId() conflict with jQuery #27922

quantizor opened this issue Sep 27, 2023 · 2 comments
Labels
CT Issue related to component testing npm: @cypress/react18 @cypress/react18 package issues

Comments

@quantizor
Copy link

quantizor commented Sep 27, 2023

Current behavior

When trying to interact with an element rendered by React that has htmlFor set to an ID returned by the useId() hook, the peculiar syntax React uses for IDs in React 18 actually causes an error:

Timed out retrying after 4000ms: Syntax error, unrecognized expression: #react-aria2038921877-:r0:

Note the :r0: portion (I know it says react-aria but under the hood it's using R18's useId API).

We don't want to escape htmlFor because that breaks platform functionality, jQuery should just handle/ignore it.

Interestingly the selector used in Cypress didn't even target that attribute:

cy.get('[data-subcomponent="file-picker-dropzone"]').selectFile('something.pdf')

Desired behavior

No response

Test code to reproduce

Example React code:

import * as React from 'react';

const Component = () => {
  const id = React.useId();

  return (
    <div>
      <label htmlFor={id}>Label</label>
      <input id={id} type="file" />
    </div>
  );
}

Then example Cypress test code:

cy.get('label').selectFile('something.pdf')

Cypress Version

12.17.4

Node version

20

Operating System

macOS latest

Debug Logs

No response

Other

No response

@Marco-Daniel
Copy link

I'm running into the same issue when using useId in my forms, any update on this?

@AriPerkkio
Copy link

I have similar issues with custom query that gets elements based on another element's aria-describedby attributes. IDs in those fields are populated using React.useId().

As work-around I had to refactor the selector a bit:

const id = subject.attr('aria-describedby'); // e.g. ":r3:-error"

- return cy.get(`#${id}-error`); // Fails with "unrecognized expression: #:r3:-error"
+ return cy.get(`[id="${id}-error"]`); // Works, generates selector '[id=":r3:-error"]' which works fine

@jennifer-shehane jennifer-shehane added CT Issue related to component testing npm: @cypress/react18 @cypress/react18 package issues labels May 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CT Issue related to component testing npm: @cypress/react18 @cypress/react18 package issues
Projects
None yet
Development

No branches or pull requests

4 participants