嵌套在 redux-form 字段标记中时无法模拟酶测试单击 material ui 复选框
Can't simulate enzyme test click on a material ui Checkbox when nested in a redux-form Field tag
我正在尝试执行 enzyme/mocha/chai 测试,以模拟在 materialUI 复选框标记中将选中状态从 true 更改为 false,该标记由redux-form 字段。我能够模拟对嵌套在 Field 标签内的本机组件(复选框等)的点击,但在嵌套时无法模拟对 material UI 标签的点击。我能够访问 Checkbox 标记上的其他属性,但无法模拟事件。
UserForm.jsx
renderCheckbox(props) {
return (
<Checkbox
label={props.label}
value={props.input.value}
// working code in the app:
onChange={ event => {
this.props.actions.toggleSuperAdmin(props.input.value)}}
// test code, not able to simulate even this click in Enzyme,
// tried to break function down into simplest event I could think of,
// and still not functioning in the test:
onCheck={() => console.log("onClick in UserForm.jsx")}
{...props.input}
/>
)
}
在我的渲染函数中,有一段代码调用了 renderCheckbox
UserForm.jsx
render() {
return (
<Paper zDepth={0} rounded={false}>
<form id='user-form' onSubmit=
{this.props.handleSubmit(this.handleUserSubmit.bind(this))}>
<Field name='is_super_admin'
component={this.renderCheckbox}
label='Work Institute Employee / Super Admin'/>
</form>
</Paper>
这是我的测试 - 我什至还不担心 expect,我只是想获取在 UserForm.jsx 中触发的点击事件以注销到我的控制台。但是我包含了几行注释掉的代码,这样您就可以看到我最终尝试使用它的位置,一旦我可以在嵌套在 Field 中的 Checkbox 中创建单击事件以注销。我不确定问题是否出在酶、redux-form 或 material UI 上,但两者之间的相互作用不允许我在酶中模拟事件。
import ConnectedUserForm, { UserForm } from '../../../www/components/UserForm'
import { expect, shallow, render, React, sinon, mount } from '../../_utils'
import jwt from 'jsonwebtoken'
import getMuiTheme from 'material-ui/styles/getMuiTheme'
import { Field } from 'redux-form/immutable'
import Checkbox from 'material-ui/Checkbox';
import Paper from 'material-ui/Paper'
import { reducer as formReducer } from 'redux-form'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import jsdom from 'jsdom'
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.window = document.defaultView
it('toggleSuperAdmin function should fire when checkbox is checked', () => {
props.user.is_super_admin = 1
let store = createStore(combineReducers({ form: formReducer }))
const userForm = mount(
<Provider store={store}>
<ConnectedUserForm {...props} />
</Provider>, options)
const checkB = userForm.find(Checkbox)
checkB.simulate('check')
// console.log("checkB1", checkB.getNodes()[0].props);
// checkB.simulate('change', {target: { isInputChecked: true }})
// console.log("checkB2", checkB.getNodes()[0].props);
// expect(props.actions.toggleSuperAdmin.calledOnce).to.be.true
})
谢谢!
我 运行 遇到了同样的问题并在这里找到了答案:https://github.com/callemall/material-ui/blob/master/src/Checkbox/Checkbox.spec.js
const input = wrapper.find('input');
input.getDOMNode().checked = !input.getDOMNode().checked;
input.simulate('change');
我正在尝试执行 enzyme/mocha/chai 测试,以模拟在 materialUI 复选框标记中将选中状态从 true 更改为 false,该标记由redux-form 字段。我能够模拟对嵌套在 Field 标签内的本机组件(复选框等)的点击,但在嵌套时无法模拟对 material UI 标签的点击。我能够访问 Checkbox 标记上的其他属性,但无法模拟事件。
UserForm.jsx
renderCheckbox(props) {
return (
<Checkbox
label={props.label}
value={props.input.value}
// working code in the app:
onChange={ event => {
this.props.actions.toggleSuperAdmin(props.input.value)}}
// test code, not able to simulate even this click in Enzyme,
// tried to break function down into simplest event I could think of,
// and still not functioning in the test:
onCheck={() => console.log("onClick in UserForm.jsx")}
{...props.input}
/>
)
}
在我的渲染函数中,有一段代码调用了 renderCheckbox
UserForm.jsx
render() {
return (
<Paper zDepth={0} rounded={false}>
<form id='user-form' onSubmit=
{this.props.handleSubmit(this.handleUserSubmit.bind(this))}>
<Field name='is_super_admin'
component={this.renderCheckbox}
label='Work Institute Employee / Super Admin'/>
</form>
</Paper>
这是我的测试 - 我什至还不担心 expect,我只是想获取在 UserForm.jsx 中触发的点击事件以注销到我的控制台。但是我包含了几行注释掉的代码,这样您就可以看到我最终尝试使用它的位置,一旦我可以在嵌套在 Field 中的 Checkbox 中创建单击事件以注销。我不确定问题是否出在酶、redux-form 或 material UI 上,但两者之间的相互作用不允许我在酶中模拟事件。
import ConnectedUserForm, { UserForm } from '../../../www/components/UserForm'
import { expect, shallow, render, React, sinon, mount } from '../../_utils'
import jwt from 'jsonwebtoken'
import getMuiTheme from 'material-ui/styles/getMuiTheme'
import { Field } from 'redux-form/immutable'
import Checkbox from 'material-ui/Checkbox';
import Paper from 'material-ui/Paper'
import { reducer as formReducer } from 'redux-form'
import { createStore, combineReducers } from 'redux'
import { Provider } from 'react-redux'
import jsdom from 'jsdom'
global.document = jsdom.jsdom('<!doctype html><html><body></body></html>')
global.window = document.defaultView
it('toggleSuperAdmin function should fire when checkbox is checked', () => {
props.user.is_super_admin = 1
let store = createStore(combineReducers({ form: formReducer }))
const userForm = mount(
<Provider store={store}>
<ConnectedUserForm {...props} />
</Provider>, options)
const checkB = userForm.find(Checkbox)
checkB.simulate('check')
// console.log("checkB1", checkB.getNodes()[0].props);
// checkB.simulate('change', {target: { isInputChecked: true }})
// console.log("checkB2", checkB.getNodes()[0].props);
// expect(props.actions.toggleSuperAdmin.calledOnce).to.be.true
})
谢谢!
我 运行 遇到了同样的问题并在这里找到了答案:https://github.com/callemall/material-ui/blob/master/src/Checkbox/Checkbox.spec.js
const input = wrapper.find('input');
input.getDOMNode().checked = !input.getDOMNode().checked;
input.simulate('change');