Build a JavaScript Calculator Test Suite does not play well with async nature of React

I have build the calculator using React. I am using Effect hook to handle the final calculation. The problem is that the test suite does expect the result to be on the spot, but the async nature of React is not allowing the value to be updated when the test suite expects it. Since the hooks are working with queues, you are supposed to program in a reactive way in order to have an app which is behaving properly.

So currently I am with a working calculator (all the test cases are working since I checked them manually), build appropriately with React, but the test suite is failing for the reason mentioned above.

Since this is a certification project, I am not sure if I should post the solution here.
Waiting for advice for this one…

I think this post should be in #support .

1 Like

Eh, this isn’t really a support issue. It’s more a request for #javascript help. This certainly is do-able as plenty of folks have completed this project.

I’d post your live link so others can help figure out what’s going on.

1 Like

Yes, yes, I am not stating that it is not doable at all :slight_smile:
I am sharing that it cannot be achieved using the use effect hook since it is async action and the test case expects the result right away.

Please post your live link so others can see what’s going on and provide advice.

1 Like

Actually I have been bashing my head with this for a while. Luckily the official docs did supplied a way around this. Since the async functionality of the useEffect was the issue for me to pass the final tests in the test suite, I saw that there is a synchronous version of it (useLayoutEffect) which fires up before the browser has a chance to re-render.

Cool. Nice work documentation sleuthing.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.