vendredi 2 septembre 2016

Redux-form 6.0.1 unit test issue

I just upgraded Redux-Form from 5.3.2 to 6.0.1, but the unit test that works in 5.3.2 failed in 6.0.1.

/* MyForm.jsx */
...
import { Field, reduxForm } from 'redux-form';

class MyForm extends Component {
    ...
    <form onSubmit={handleSubmit(...)}>
    ...
}

export default reduxForm({
    form: 'myForm'
})(MyForm);

I mounted the form reducer before render the form:

import {reducer as formReducer} from 'redux-form';
const myReducer = combineReducers({
    ...
    form: formReducer
});

Here is the store, created in top level component:

const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(myReducer);

My test case (Karma + jasmine), which works in 5.3.2, but failed in 6.0.1

/* form.test.js */
import React, { PropTypes } from 'react';
import TestUtils from 'react/lib/ReactTestUtils';
import findDOMNode from 'react/lib/findDOMNode';

import { Provider } from 'react-redux';
...
import MyForm from '../MyForm';

const createStoreWithMiddleware = applyMiddleware(thunkMiddleware)(createStore);
const store = createStoreWithMiddleware(rootReducer);

describe('MyForm', () => {
    beforeAll(function() {
        this.props = {
            ...
            store: store
        }
    });

    it('should render', function() {
        const element = TestUtils.renderIntoDocument(
            <MyForm {...this.props} />
        );
        expect(element).toBeTruthy();
    });

error: Invariant Violation: Could not find "store" in either the context or props of "Connect(ConnectedField)". Either wrap the root component in a <Provider>, or explicitly pass "store" as a prop to "Connect(ConnectedField)".

If I use Provider to pass in store, will get another error:

it('should render', function() {
    const element = TestUtils.renderIntoDocument(
       <Provider store={ store }>
           { () => <MyForm {...this.props} />}
       </Provider>
    );
    expect(element).toBeTruthy();
});

*

error: Invariant Violation: onlyChild must be passed a children with exactly one child.
ERROR: 'Warning: Failed propType: Invalid prop `children` supplied to `Provider`, expected a single ReactElement.'

*

Any ideas why the test failed? I searched online but could not find information that specific for this topic.

Thanks,

Aucun commentaire:

Enregistrer un commentaire