Issue
is it possible to write tests in jest for a component that is, as it were, inside? that is, I do not have the ability to make the data values incremental in the function, they are inside the function, just a simplified example
export default function App() {
let test1 = 10;
let test2 = 5;
if (test1 !== test2) {
console.log("1");
}
return (
<div className="App">
<h1>Hello CodeSandbox</h1>
<h2>Start editing to see some magic happen!</h2>
</div>
);
}
https://codesandbox.io/s/hungry-bouman-5x293t?file=/src/App.tsx:24-286
Solution
It is not possible to test for the private internals of functions.
The only way you could test it, would be by having it in separate function (just a simple util function) and test separately there. After that just import it anywhere you need.
This means doing something like this
const areTestValueMatching = (test1, test2) => {
if (test1 !== test2) {
return false;
}
return true
}
it("returns true if test values are matching", () => {
expect(areTestValueMatching(1, 2)).toBe(false)
})
And then importing areTestValueMatching function into your function component.
Answered By - MalwareMoon
Answer Checked By - - Marilyn (ReactFix Volunteer)