Testing
Este conteúdo não está disponível em sua língua ainda.
All AsterUI components support the data-testid prop for end-to-end testing with tools like Playwright, Cypress, or Testing Library.
Basic Usage
Section titled “Basic Usage”Pass a data-testid prop to any component:
import { Button, Input, Modal } from 'asterui'
function LoginForm() { return ( <form> <Input data-testid="email-input" placeholder="Email" /> <Input data-testid="password-input" type="password" placeholder="Password" /> <Button data-testid="submit-btn" color="primary"> Sign In </Button> </form> )}Selecting Elements
Section titled “Selecting Elements”// Click the submit buttonawait page.getByTestId('submit-btn').click()
// Fill the email inputawait page.getByTestId('email-input').fill('user@example.com')
// Assert element is visibleawait expect(page.getByTestId('submit-btn')).toBeVisible()// Click the submit buttoncy.get('[data-testid="submit-btn"]').click()
// Fill the email inputcy.get('[data-testid="email-input"]').type('user@example.com')
// Assert element is visiblecy.get('[data-testid="submit-btn"]').should('be.visible')import { render, screen } from '@testing-library/react'
// Find by test IDconst button = screen.getByTestId('submit-btn')const input = screen.getByTestId('email-input')
// Assert element is in documentexpect(button).toBeInTheDocument()Complex Components
Section titled “Complex Components”Components with multiple interactive elements use a suffix pattern for nested test IDs. When you pass data-testid="my-id", child elements receive IDs like my-id-suffix.
<Input data-testid="search" prefix={<SearchIcon />} allowClear />| Element | Test ID |
|---|---|
| Wrapper | search |
| Input field | search-input |
| Prefix area | search-prefix |
| Suffix area | search-suffix |
| Clear button | search-clear |
<Modal data-testid="confirm-modal" title="Confirm" open={isOpen}> Are you sure?</Modal>| Element | Test ID |
|---|---|
| Modal container | confirm-modal |
| Title | confirm-modal-title |
| Content | confirm-modal-content |
| Backdrop | confirm-modal-backdrop |
Select
Section titled “Select”<Select data-testid="country-select" options={countries} />| Element | Test ID |
|---|---|
| Container | country-select |
| Trigger | country-select-trigger |
| Dropdown | country-select-dropdown |
| Option (by value) | country-select-option-{value} |
Data State Attributes
Section titled “Data State Attributes”Many components also expose data-state attributes that reflect their current state, useful for assertions:
// Button states<Button loading /> // data-state-loading="true"<Button disabled /> // data-state-disabled="true"<Button active /> // data-state-active="true"
// Toggle state<Toggle checked /> // data-state="checked"<Toggle /> // data-state="unchecked"
// Tooltip/Popover state<Tooltip open /> // data-state="open"<Tooltip /> // data-state="closed"Example assertions:
// Assert button is loadingawait expect(page.getByTestId('submit-btn')).toHaveAttribute('data-state-loading', 'true')
// Assert toggle is checkedawait expect(page.getByTestId('dark-mode')).toHaveAttribute('data-state', 'checked')// Assert button is loadingcy.get('[data-testid="submit-btn"]').should('have.attr', 'data-state-loading', 'true')
// Assert toggle is checkedcy.get('[data-testid="dark-mode"]').should('have.attr', 'data-state', 'checked')