Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

reactjs - Jest + Enzyme + ReduxForm unit testing render issue

I am working on getting some unit test around my redux form and running into a lot of issues with it. Currently my test looks like this

describe('testing ForgotPassword functionality', () => {
let ForgotPasswordForm;
let defaultProps = {
    handleSubmit: (fn) => fn
}
let shallow;
let props = {
    forgotPassword: jest.fn()
}
let store = createStore(combineReducers({ form: formReducer, auth: authReducer }));

beforeEach(() => {
    shallow = createShallow({ dive: true })
    ForgotPasswordForm = shallow(
        <Provider store={store}>
            <ForgotPassword {...defaultProps} {...props} />
        </Provider>
    );
});

it('ForgotPassword renders correctly with email', () => {
    expect(ForgotPasswordForm).toMatchSnapshot();
});

it('check event on `Submit` button', () => {
    console.log(ForgotPasswordForm.debug())
    const emailField = ForgotPasswordForm.find('input[name="email"]');
    emailField.simulate('change', { target: {value: '[email protected]'} });
    const submitButton = ForgotPasswordForm.find(Button).find('button');
    submitButton.simulate('click').simulate('submit');
    expect(defaultProps.handleSubmit).toHaveBeenCalledTimes(10);
});
})

I am using material ui hence the use of createShallow. When I debug the shallow render

<Connect(ReduxForm) handleSubmit={[Function: handleSubmit]}
forgotPassword={[Function: mockConstructor] { _isMockFunction: true, getMockImplementation:
   [Function (anonymous)], mock: { calls: [], instances: [], invocationCallOrder: [], 
   results: [] }, mockClear: [Function (anonymous)], 
   mockReset: [Function (anonymous)], mockRestore: [Function (anonymous)], 
   mockReturnValueOnce: [Function (anonymous)], mockResolvedValueOnce: 
   [Function (anonymous)], mockRejectedValueOnce: [Function (anonymous)], 
   mockReturnValue: [Function (anonymous)], 
   mockResolvedValue: [Function (anonymous)], 
   mockRejectedValue: [Function (anonymous)], 
   mockImplementationOnce: [Function (anonymous)], 
   mockImplementation: [Function (anonymous)], 
   mockReturnThis: [Function (anonymous)], 
   mockName: [Function (anonymous)], getMockName: [Function (anonymous)] }} />

This creates an issue an issue with me trying to do the find because it doesn't find the html. I must be missing something obvious here. I have tried to dive again but that throws an error

question from:https://stackoverflow.com/questions/65877553/jest-enzyme-reduxform-unit-testing-render-issue

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...