Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,40 @@ describe("Expect.Operators", () => {

```

`testAsync` example with ReactTestUtils `actAsync` for effectfull component assertion:

```reason
open Jest;

describe("ReactTestUtils actAsync", () => {
open Expect;
open Js.Promise;
open ReactTestUtils;

let container = ref(None);
beforeEach(prepareContainer(container));
afterEach(cleanupContainer(container));

let effectfulComponent = <p>{"Hello async Jest" |> React.string}</p>

// use testAsync to get a `finish` callback for final assertion
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;
});
Comment on lines +64 to +77
Copy link
Owner

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:

Suggested change
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.

Copy link
Author

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 ) ?

Copy link
Author

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.

Copy link
Owner

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?

});
```

See [the tests](https://github.com/glennsl/bs-jest/tree/master/__tests__) for more examples.

## Installation
Expand Down