2017-05-05 6 views
0

materialUIでtrue/falseのチェック状態を変更するシミュレーションを行う酵素/モカ/チャイテストを実行しようとしています。 redux-formフィールドでラップされレンダリングされたチェックボックスタグ。 Fieldタグ内にネストされたネイティブコンポーネント(チェックボックスなど)のクリックをシミュレートできますが、ネストされたときにMaterial UIタグのクリックをシミュレートできません。私はCheckboxタグの他のプロパティにアクセスできますが、イベントをシミュレートすることはできません。酵素テストをシミュレートすることはできません材料をクリックしてくださいredux-formフィールドタグ内にネストされている場合チェックボックス

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> 

を呼び出し、ここに私のテストでこのコードブロックがあります - 私は気にせていないです私はちょうどしようとしています、でもまだ期待UserForm.jsxで発生したクリックイベントを取得して、自分のコンソールにログアウトします。しかし、コメントアウトされたコードの数行を含めたので、FieldにネストされたCheckbox内のclickイベントをログアウトすると、最終的にどこに行きたいのか分かります。私は問題が酵素、還元型または物質UIであるかどうかはわかりませんが、2つの間の相互作用によって酵素の事象をシミュレートすることができません。

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 
    }) 

ありがとうございます!

答えて

2

私は同じ問題に遭遇し、ここに答えが見つかりました:これは私のために働いたhttps://github.com/callemall/material-ui/blob/master/src/Checkbox/Checkbox.spec.js

const input = wrapper.find('input'); 
    input.node.checked = !input.node.checked; 
    input.simulate('change'); 
+1

を。しかし、なぜ 'input.node.checked =!input.node.checked;'が必要なのでしょうか?ラップされた 'Checkbox'とネイティブチェックボックスの間に違いはありますか? – wangzi6147

関連する問題