Oma*_*mar 3 state invoke reactjs redux cypress
我正在使用 redux 进行反应并使用 cypress 进行测试,我能够使用 cy.window().its('store').invoke('getState').then((state) => {} 但是我如何访问组件的本地状态而不是应用程序商店?
我试过
cy.get('.simple-component').its('getState')
Run Code Online (Sandbox Code Playgroud)
或者
cy.get('.simple-component').invoke('getState')
Run Code Online (Sandbox Code Playgroud)
但赛普拉斯正在返回“赛普拉斯错误:重试超时:cy.invoke() 出错,因为属性:'getState' 在您的主题上不存在”并且在赛普拉斯控制台上(在 chrome 中)它正在发出:
Yielded:
<div class="simple-component" getstate="[object Object]"></div>
Run Code Online (Sandbox Code Playgroud)
这似乎是由 React 从 DOM 中删除方法造成的,所以我需要在 React 中而不是在 DOM 中访问它?
import React, { Component } from 'react';
class simpleComponent extends Component {
constructor(props) {
super(props)
this.state = {
sample: "hello"
}
}
// getState() just for testing on cypress
getState() {
return this.state
}
render(){
return <div className="simple-component" getState={this.getState()}></div>
}
}
Run Code Online (Sandbox Code Playgroud)
作为替代方案,我可以使用 window.store 在简单组件的末尾导出本地组件状态吗?
有一个 Cypress 插件,称为react-unit-test. 它使您能够React直接挂载组件(添加cy.mount()命令)并提供对组件内部状态的访问。
这是 repo 的自述文件中的一个示例:
// load Cypress TypeScript definitions for IntelliSense
/// <reference types="cypress" />
// import the component you want to test
import { HelloState } from '../../src/hello-x.jsx'
import React from 'react'
describe('HelloState component', () => {
it('works', () => {
// mount the component under test
cy.mount(<HelloState />)
// start testing!
cy.contains('Hello Spider-man!')
// mounted component can be selected via its name, function, or JSX
// e.g. '@HelloState', HelloState, or <HelloState />
cy.get(HelloState)
.invoke('setState', { name: 'React' })
cy.get(HelloState)
.its('state')
.should('deep.equal', { name: 'React' })
// check if GUI has rerendered
cy.contains('Hello React!')
})
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6303 次 |
| 最近记录: |