React API
方法
mount
import { mount } from 'cypress/react'
描述 | 将React组件挂载到DOM中。 |
签名 | mount(jsx: React.ReactNode, options?: MountOptions, rerenderKey?: string): Cypress.Chainable<MountReturn> |
返回值 | Cypress.Chainable<MountReturn> |
名称 | 类型 | 描述 |
jsx | React.JSX.Element | 要挂载的React组件。 |
options | MountOptions (可选) | 挂载组件的选项 |
rerenderKey | string (可选) | 用于强制重新渲染的键。 |
示例
import { mount } from '@cypress/react'
import { Stepper } from './Stepper'
it('挂载测试', () => {
mount(<StepperComponent />)
cy.get('[data-cy=increment]').click()
cy.get('[data-cy=counter]').should('have.text', '1')
}
getContainerEl
描述 | 获取用于挂载组件的根元素。 |
签名 | () => HTMLElement |
返回值 | HTMLElement |
接口
MountOptions
名称 | 类型 | 描述 |
alias | string (可选) | |
ReactDom | MountReactComponentOptions.ReactDom (可选) | |
log | boolean (可选) | 将挂载命令记录到Cypress命令日志中,默认为true |
strict | boolean (可选) | 在React 严格模式 下渲染组件,这会激活对子组件的额外检查和警告。 |
MountReturn
mount
函数返回的类型
名称 | 类型 | 描述 |
component | React.ReactNode | 在React 严格模式 下渲染组件,这会激活对子组件的额外检查和警告。 |
rerender | (component: React.ReactNode) => globalThis.Cypress.Chainable<MountReturn> | 已渲染的组件。 |