Is there a way to test if an empty React prop has not been called?

Viewed 150

I have a React component that receives an optional onApply prop, like below:

export default function App({ onApply }) {
  const handleApply = () => {
    if (onApply) onApply();
  };

  return <button onClick={handleApply}>Apply</button>;
}

I'm trying to write a test that asserts if this prop has not been called if it has not been provided to the component. The following is my test's current code:

describe("<App />", () => {
  it("should not apply if no `onApply` prop provided", () => {
    const wrapper = mount(<App />);

    // expect ???;
  });
});

My question is: how can I test this behavior, since onApply must be a falsy value and so cannot be spied (as Simon's stub docs)?

My testing stack is Enzyme, Chai and Sinon.

Here is a working Codesandbox.

Also, I checked this answer, that is the most similar problem I've found, but the suggestion in that question is check if onApply, in this case, is falsy, what is redundant, because I know it is when I pass it via props.

0 Answers
Related