Search code examples
reactjsreact-reduxmaterial-uiredux-formenzyme

Can't simulate enzyme test click on a material ui Checkbox when nested in a redux-form Field tag


I am trying to perform an enzyme/mocha/chai test to simulate changing the state of checked from true to false in a materialUI Checkbox tag which was wrapped in and rendered by a redux-form Field. I am able to simulate clicks on native components (checkbox, etc) which are nested inside of the Field tag, but am unable to simulate a click on a material UI tag, when nested. I am able to access the other properties on the Checkbox tag, but just unable to simulate events.

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

and inside of my render function, there is this block of code which calls 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>

and here is my test - I am not worrying about expect yet even, I just am trying to get the click event that is fired in UserForm.jsx to log out onto my console. But I included the few lines of commented out code so you can see where I am trying to go with it eventually once I can make the click event inside the Checkbox nested in Field to log out. I am not sure if the problem is with enzyme, redux-form or material UI, but the interaction between the two is not allowing me to simulate events in enzyme.

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

Thanks!


Solution

  • I ran into the same issue and found the answer here: 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');