lundi 1 mars 2021

Testing Library; limit the search for specific HTML elements under a DOM parent node

That's from React Testing Library docs:

getByText(
  // If you're using `screen`, then skip the container argument:
  container: HTMLElement,
  text: TextMatch,
  options?: {
    selector?: string = '*',
    exact?: boolean = true,
    ignore?: string|boolean = 'script, style',
    normalizer?: NormalizerFn,
  }): HTMLElement

The above will search for all elements that have a text node with textContent matching the given TextMatch.

My question is how may I limit that search under a specific DOM node. For example,

screen.getByText(/about/i)

will search the whole DOM. Do not want that.

Aucun commentaire:

Enregistrer un commentaire