mardi 20 octobre 2020

React Testing onClick Console.log

I am very lost. I need to confirm that the onClick is being fired when the button is clicked. It logs "Click" in the console but I think I need to make sure the "click" logs in the console in order to assert in the test that it is functional. I am sure the expect is wrong but can't seem to find the right answer through my normal means. Any help would be much appreciated.

index.js

ReactDOM.render(
    <Button onClick={() => console.log('Click')} />,
    document.getElementById('root')
);

Button.js

import React from 'react';

export default function Button(props) {
    return <button onClick={() => props.onClick()}>Click me!</button>;
}

Button.test.js

import React from "react";
import { shallow } from "enzyme";

import Button from "./button";

describe("<Button />", () => {
  it("Should call props.onClick when the button is clicked", () => {
    const wrapper = shallow(<Button onClick={() => console.log("Click")} />);
    wrapper.find("button").simulate("click");
    expect(console.log).expect.stringContaining("Click");
  });
});

Aucun commentaire:

Enregistrer un commentaire