Add testAsync example with ReactTestUtils actAsync usage#77
Add testAsync example with ReactTestUtils actAsync usage#77TristanCacqueray wants to merge 1 commit intoglennsl:masterfrom
Conversation
|
Not sure this README is the appropriate place for such example, but as it took me quite a while to figure out this usage, and assuming this is the right strategy, perhaps bs-jest could document how it can be used to test react hook? |
| testAsync("actAsync", finish => { | ||
| let container = getContainer(container); | ||
| // actAsync returns a promise for all effects evaluation | ||
| actAsync(_ => | ||
| // Convert ReactDOMRe.render to a promise | ||
| resolve(ReactDOMRe.render(effectfulComponent, container)) | ||
| ) |> then_(_ => | ||
| // Call finish with assertion on final container | ||
| finish(expect( | ||
| container | ||
| ->DOM.findBySelectorAndTextContent("p", "Hello async Jest") | ||
| ->Belt.Option.isSome) |> toBe(true)) |> resolve | ||
| ) |> ignore; | ||
| }); |
There was a problem hiding this comment.
It would be more appropriate to use testPromise here, I think. Then you should be able to do:
| testAsync("actAsync", finish => { | |
| let container = getContainer(container); | |
| // actAsync returns a promise for all effects evaluation | |
| actAsync(_ => | |
| // Convert ReactDOMRe.render to a promise | |
| resolve(ReactDOMRe.render(effectfulComponent, container)) | |
| ) |> then_(_ => | |
| // Call finish with assertion on final container | |
| finish(expect( | |
| container | |
| ->DOM.findBySelectorAndTextContent("p", "Hello async Jest") | |
| ->Belt.Option.isSome) |> toBe(true)) |> resolve | |
| ) |> ignore; | |
| }); | |
| testPromise("actAsync", () => { | |
| let container = getContainer(container); | |
| // actAsync returns a promise for all effects evaluation | |
| actAsync(_ => | |
| // Convert ReactDOMRe.render to a promise | |
| resolve(ReactDOMRe.render(effectfulComponent, container)) | |
| ) |> then_(_ => | |
| // Call finish with assertion on final container | |
| expect( | |
| container | |
| ->DOM.findBySelectorAndTextContent("p", "Hello async Jest") | |
| ->Belt.Option.isSome) |> toBe(true) |> resolve | |
| ); | |
| }); |
Slightly simpler and much safer.
There was a problem hiding this comment.
That looks much better, unfortunately that does not seems to work with actAsync, here is the error message:
TypeError: Cannot read property 'then' of undefined
at Object.<anonymous> (node_modules/@glennsl/bs-jest/src/jest.bs.js:249:47)
Could it be caused by the actAsync binding implementation ( https://github.com/reasonml/reason-react/blob/5ae20e8a6245a584a8f452bf797a2ac26c09d873/src/ReactTestUtils.re#L22-L28 ) ?
There was a problem hiding this comment.
For some reason, it seems like actAsync resulting promise can only be resolved once. e.g. actAsync(_ => () |> resolve) |> then_(_ => () |> resolve) is undefined.
There was a problem hiding this comment.
Huh, that's weird. They do seem to return a broken thenable. I can't imagine why they would do that without documenting it. Definitely seems like a bug, so perhaps it would be a good idea to open an issue on the react repo?
No description provided.