vendredi 7 février 2020

Unable to pass test case in React saying simulate a function onClick()

I am working on Project on HackerRank where I have passed all the testcases except the simulate a function onClick or onSubmit.I dont know where I am going wrong because I have tried all the approach after refering documentation

Here is my code

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);
    });

I am getting error like this

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>

Kindly help me As I dont know function is not even getting triggered Any help will be appreceiated

Aucun commentaire:

Enregistrer un commentaire