-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add testAsync example with ReactTestUtils actAsync usage #77
base: master
Are you sure you want to change the base?
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? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! I don't know where else to put this, so I think this is fine. It would probably be better to use testPromise
though, as shown below, if you could verify that this works?
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.