diff --git a/docs/dom-testing-library/api-queries.mdx b/docs/dom-testing-library/api-queries.mdx index bd5da1eef..ff6ab1760 100644 --- a/docs/dom-testing-library/api-queries.mdx +++ b/docs/dom-testing-library/api-queries.mdx @@ -3,8 +3,8 @@ id: api-queries title: Queries --- -import Tabs from '@theme/Tabs'; -import TabItem from '@theme/TabItem'; +import Tabs from '@theme/Tabs' +import TabItem from '@theme/TabItem' ## Variants @@ -169,6 +169,7 @@ The example below will find the input node for the following DOM structures: label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -178,6 +179,7 @@ const inputNode = screen.getByLabelText('Username') + ```jsx import { render, screen } from '@testing-library/react' @@ -189,6 +191,7 @@ const inputNode = screen.getByLabelText('username') + ```js cy.findByLabelText('username').should('exist') ``` @@ -196,6 +199,7 @@ cy.findByLabelText('username').should('exist') + It will NOT find the input node for label text broken up by elements. You can use `getByRole('textbox', { name: 'Username' })` instead which is robust against switching to `aria-label` or `aria-labelledby`. @@ -263,6 +267,7 @@ matches the given [`TextMatch`](#textmatch). label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -272,6 +277,7 @@ const inputNode = screen.getByPlaceholderText('Username') + ```jsx import { render, screen } from '@testing-library/react' @@ -282,6 +288,7 @@ const inputNode = screen.getByPlaceholderText('Username') + ```js cy.findByPlaceholderText('Username').should('exist') ``` @@ -289,6 +296,7 @@ cy.findByPlaceholderText('Username').should('exist') + > **Note** > > A placeholder is not a good substitute for a label so you should generally use @@ -322,6 +330,7 @@ matching the given [`TextMatch`](#textmatch). label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -331,6 +340,7 @@ const aboutAnchorNode = screen.getByText(/about/i) + ```jsx import { render, screen } from '@testing-library/react' @@ -341,6 +351,7 @@ const aboutAnchorNode = screen.getByText(/about/i) + ```js cy.findByText(/about/i).should('exist') ``` @@ -348,6 +359,7 @@ cy.findByText(/about/i).should('exist') + It also works with `input`s whose `type` attribute is either `submit` or `button`: @@ -400,6 +412,7 @@ as it's deprecated). label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -409,6 +422,7 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i) + ```jsx import { render, screen } from '@testing-library/react' @@ -419,6 +433,7 @@ const incrediblesPosterImg = screen.getByAltText(/incredibles.*? poster/i) + ```js cy.findByAltText(/incredibles.*? poster/i).should('exist') ``` @@ -426,6 +441,7 @@ cy.findByAltText(/incredibles.*? poster/i).should('exist') + ### `ByTitle` > getByTitle, queryByTitle, getAllByTitle, queryAllByTitle, findByTitle, @@ -457,6 +473,7 @@ Will also find a `title` element within an SVG. label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -467,6 +484,7 @@ const closeElement = screen.getByTitle('Close') + ```jsx import { render, screen } from '@testing-library/react' @@ -478,6 +496,7 @@ const closeElement = screen.getByTitle('Close') + ```js cy.findByTitle('Delete').should('exist') cy.findByTitle('Close').should('exist') @@ -486,6 +505,7 @@ cy.findByTitle('Close').should('exist') + ### `ByDisplayValue` > getByDisplayValue, queryByDisplayValue, getAllByDisplayValue, @@ -518,6 +538,7 @@ document.getElementById('lastName').value = 'Norris' label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -527,6 +548,7 @@ const lastNameInput = screen.getByDisplayValue('Norris') + ```jsx import { render, screen } from '@testing-library/react' @@ -537,6 +559,7 @@ const lastNameInput = screen.getByDisplayValue('Norris') + ```js cy.findByDisplayValue('Norris').should('exist') ``` @@ -544,6 +567,7 @@ cy.findByDisplayValue('Norris').should('exist') + #### `textarea` ```html @@ -558,6 +582,7 @@ document.getElementById('messageTextArea').value = 'Hello World' label: 'React', value: 'react', }, { label: 'Cypress', value: 'cypress', }, ] }> + ```js import { screen } from '@testing-library/dom' @@ -567,6 +592,7 @@ const messageTextArea = screen.getByDisplayValue('Hello World') + ```jsx import { render, screen } from '@testing-library/react' @@ -577,6 +603,7 @@ const messageTextArea = screen.getByDisplayValue('Hello World') + ```js cy.findByDisplayValue('Hello World').should('exist') ``` @@ -584,6 +611,7 @@ cy.findByDisplayValue('Hello World').should('exist') + #### `select` In case of `select`, this will search for a `