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 · 3 comments
Open

React 18 useId() conflict with jQuery #27922

quantizor opened this issue Sep 27, 2023 · 3 comments
Labels
CT Issue related to component testing npm: @cypress/react18 @cypress/react18 package issues stale no activity on this issue for a long period

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

@cypress-app-bot
Copy link
Collaborator

This issue has not had any activity in 180 days. Cypress evolves quickly and the reported behavior should be tested on the latest version of Cypress to verify the behavior is still occurring. It will be closed in 14 days if no updates are provided.

@cypress-app-bot cypress-app-bot added the stale no activity on this issue for a long period label Nov 12, 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 stale no activity on this issue for a long period
Projects
None yet
Development

No branches or pull requests

5 participants