I'm learning to test React applications, right now I have a function that will run after a promise (innerFunction) in my App.jsx, like this:
function App() {
const [isReady, setIsReady] = useState(false);
const innerFunction = wrapperFunction();
useEffect(() => {
thirdPartyPkg.onReady().then(() => innerFunction(isReady, setIsReady));
}, []);
// ...
}
This is what the code on wrapperFunction.js looks like:
export function wrapperFunction() {
return function innerFunction(isReady, setIsReady) {
if (isReady) {
setIsReady(true);
}
};
}
And this is my App.test.jsx:
import * as Function from "./utils/wrapperFunction";
describe("testing App", () => {
it("should run innerFunction once after promise", () => {
const inner = jest.spyOn(Function, "wrapperFunction");
expect(inner).toBeCalledTimes(1);
});
});
I'm guessing that I need to wait to the promise to resolve, but I'm kind of blocked, do you guys have any suggestion to test if the innerFunction is called?