Jest:在Jest中Redux表单失败的测试用例

时间:2019-01-29 12:08:02

标签: reactjs redux

这是代码 index.js

import React, { Component } from 'react';
import { Field, reduxForm } from 'redux-form';
import { Input } from 'components/Form';
import { Button } from 'components';
import formStyles from 'components/Form/formStyles.scss';
import layoutStyles from 'modules/Configuration/components/ConfigLayout/ConfigPageLayout/style.scss';
import styles from './style.scss';

@reduxForm({
    form: 'bankDetails',
})
export default class BankDetails extends Component {
    componentDidMount() {
        const defaultValues = { status: 'active' };

        defaultValues.references = [{}];

        this.props.initialize(defaultValues);
    }

    render() {
        return (
            <form className={`${styles.financialYearInput} ${layoutStyles.schoolSetup}`}>
                <fieldset>
                    <div className={styles.primaryColWrap}>
                        <div className={styles.schoolInfoCol}>
                            <legend>Bank Details</legend>
                            <div className={formStyles.twoCol}>
                                <Field
                                    name="bankName"
                                    type="text"
                                    component={Input}
                                    label="Bank Name"
                                    placeholder="Enter Text"
                                    required
                                />
                                <Field
                                    name="branchName"
                                    type="text"
                                    component={Input}
                                    label="Branch Name"
                                    placeholder="Enter Text"
                                    required
                                />
                            </div>
                            <div className={formStyles.twoCol}>
                                <Field
                                    name="bankAccount"
                                    type="text"
                                    component={Input}
                                    label="Bank Account Name"
                                    placeholder="Enter Text"
                                    required
                                />
                                <Field
                                    name="accountNumber"
                                    type="text"
                                    component={Input}
                                    label="Account Number"
                                    placeholder="Enter Text"
                                    required
                                />
                            </div>
                            <div className={formStyles.twoCol}>
                                <Field
                                    name="ifscCode"
                                    type="text"
                                    component={Input}
                                    label="IFSC Code"
                                    placeholder="Enter Text"
                                    required
                                />
                                <Field
                                    name="micrCode"
                                    type="text"
                                    component={Input}
                                    label="MICR Code"
                                    placeholder="Enter Text"
                                    required
                                />
                            </div>
                        </div>
                    </div>
                </fieldset>
                <br />
                <br />
                <div className={styles.actWrap}>
                    <div className={styles.rightActWrap}>
                        <Button type="submit" btnType="primary">
                            <span>Save and continue</span>
                        </Button>
                        <Button type="button" btnType="primary">
                            <span>Save as Draft</span>
                        </Button>
                        <Button type="button">
                            <span>Cancel</span>
                        </Button>
                    </div>
                </div>
            </form>
        );
    }
}

我在反应测试用例中使用了笑话,但是我陷入了这个错误

TypeError:无法读取未定义的属性“替换”

这是 BankDetails.test.js 代码:

import React from 'react';
import { expect } from 'chai';
import Enzyme from 'enzyme';
import Adapter from 'enzyme-adapter-react-16';
import BankDetails from './index';

Enzyme.configure({ adapter: new Adapter() });

describe('<BankDetails>', () => {
    it('should have a button', () => {
        const wrapper = Enzyme.shallow(<BankDetails />);
        expect(wrapper.find('button')).to.have.length(1);
    });
});

我已经运行了此 sudo yarn run测试BankDetails.test.js ,并显示了此错误消息。

这是错误:

ses git:(feature/feathers-redux-implementation) ✗ sudo yarn test BankDetails.test.js
yarn run v1.12.3
warning package.json: No license field
$ jest BankDetails.test.js
TypeError: Cannot read property 'replace' of undefined
    at replacePathSepForGlob (/home/ldap/opses.dv15/ses/node_modules/jest-util/build/replacePathSepForGlob.js:17:15)
    at Array.map (<anonymous>)
    at Object.keys.reduce (/home/ldap/opses.dv15/ses/node_modules/jest-config/build/normalize.js:783:37)
    at Array.reduce (<anonymous>)
    at normalize (/home/ldap/opses.dv15/ses/node_modules/jest-config/build/normalize.js:609:24)
    at readConfig (/home/ldap/opses.dv15/ses/node_modules/jest-config/build/index.js:164:46)
    at readConfigs (/home/ldap/opses.dv15/ses/node_modules/jest-config/build/index.js:362:26)
    at /home/ldap/opses.dv15/ses/node_modules/jest-cli/build/cli/index.js:283:58
    at Generator.next (<anonymous>)
    at asyncGeneratorStep (/home/ldap/opses.dv15/ses/node_modules/jest-cli/build/cli/index.js:202:24)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

请帮助,谢谢。

0 个答案:

没有答案