无法通过React中的测试用例模拟功能onClick()

问题描述 投票:0回答:1

我正在HackerRank上的项目上工作,除了模拟功能onClick或onSubmit外,我已经通过了所有测试用例。我不知道我要去哪里错了,因为我在参考文档后尝试了所有方法]

这是我的代码

Mail.component

export class Mail extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            activeMail: this.props.mail
        };
        this.handleCompose = this.handleCompose.bind(this);
    }

    handleCompose() {
    console.log("=========================Compose==================")
        this.props.storeComposeMail(this.props.inboxData);
    }

    render() {
         <div>
<NavLink
  className="composebtn"
  to="/composemail">

  <button onClick={this.handleCompose}
    className={
      "btn-success pull-right composebtn" +
      (this.props.folder == "draft" ? "" : " visible")}
 > Edit</button>

</NavLink>
    </div>

MytestCase

describe("without store", () => {
    let wrapper;
    const mockLoginfn1 = jest.fn();
    test("submit click event", () => {
        const spy = jest.spyOn(Mail.prototype, "handleCompose");
        var data = {
            id: "",
            from: "",
            to: "",
            subject: "",
            time: "",
            body: ""
        };
        const wrapper = shallow(
            <Mail
                storeComposeMail={mockLoginfn1}
                folder="inbox"
                inboxData={data}
                mail={1}
            />
        );

        const Button = wrapper.find("NavLink").at(0);
        Button.simulate("click");
        expect(spy).toHaveBeenCalledTimes(1);
    });

我遇到这样的错误

FAIL  src/test/Mail.spec.js

  ● without store › submit click event

    expect(jest.fn()).toHaveBeenCalledTimes(1)



    Expected mock function to have been called one time, but it was called zero times.



      at Object.test (src/test/Mail.spec.js:61:21)

          at new Promise (<anonymous>)

      at Promise.resolve.then.el (node_modules/p-map/index.js:46:16)

          at <anonymous>

请帮助我,因为我不知道功能甚至没有被触发任何帮助将不胜感激

我正在HackerRank上的项目上工作,除了模拟功能onClick或onSubmit外,我已经通过了所有测试用例。我不知道我要去哪里错了,因为我已经尝试了所有方法...

javascript reactjs testing jest
1个回答
0
投票
您在<NavLink />组件上没有按钮的单击事件,因此找到该按钮,然后模拟对该按钮的单击。这应该可以解决您的测试。
© www.soinside.com 2019 - 2024. All rights reserved.