tylercollier / redux-form-test

Shows how to do unit tests and integration tests with Redux-Form
219 stars 26 forks source link

Example for integration test- change field value example #11

Closed hajdun closed 6 years ago

hajdun commented 6 years ago

I found it extremely hard to change field value in a jest test. I change the entire form looks based on some values, so I investigated and found this:

Setup

"jest": "^23.6.0", "enzyme": "^3.6.0", "enzyme-adapter-react-16": "^1.5.0", "chai": "^4.1.2", "chai-enzyme": "^1.0.0-beta.1", ............ import chai, { expect } from 'chai' import chaiEnzyme from 'chai-enzyme' import { Provider } from 'react-redux' import { reducer as formReducer } from 'redux-form' import { createStore, combineReducers } from 'redux' import { configure, mount } from 'enzyme'; import Adapter from 'enzyme-adapter-react-16';

configure({ adapter: new Adapter() }); chai.use(chaiEnzyme())

let store = createStore(combineReducers({ form: formReducer }))

let wrapper = mount(

); (....)

Test changing field value ....... const fields = wrapper.find('Field') let titleInput = fields.findWhere(n => n.prop('name') === 'title') const input = titleInput.find('input') input.simulate('change', { target: { value: "sometext" } }) wrapper.update() expect(store.getState().form.FORMNAME.values.title).to.equal("sometext")

I hope this example may be useful for someone later :)

tylercollier commented 6 years ago

Cool! Thanks! I didn't know about the findWhere function.