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

Update act #1106

Merged
merged 3 commits into from
Dec 30, 2024
Merged

Update act #1106

merged 3 commits into from
Dec 30, 2024

Conversation

rpiaggio
Copy link
Collaborator

@rpiaggio rpiaggio commented Dec 29, 2024

Starting in React 18.3, act has been moved to React and deprecated in ReactTestUtils.

Also, this PR implements 3 methods in ReactTestUtils2:

  • renderAsync, which invokes act(root.render(component)) asnychronouslly;
  • actAsync(=> A) which is a convenience method for async act without the need of wrapping the action in an async effect;
  • withRenderedAsync, which is an async version of withRendered.

This enables async testing with the pattern:

withRenderedAsync(comp()){ _ =>  // Mounts comp in root
  assert(...)
  for
    _ <- actAsync(Simulate.click(...))
    _ =  assert(...)
    _ <- actAsync(Simulate.click(...))
  yield assert(...)
}.map(_ => assert(...))          // After unmount

act will become fully async in the future. The current React docs read:

We recommend using act with await and an async function. Although the sync version works in many cases, it doesn’t work in all cases and due to the way React schedules updates internally, it’s difficult to predict when you can use the sync version.

We will deprecate and remove the sync version in the future.

Note: This PR subsumes #1105, since React 18.3 is needed.

@@ -113,12 +114,27 @@ trait ReactTestUtils2 extends japgolly.scalajs.react.test.internal.ReactTestUtil
WithDsl(newElement())(removeElement)

val withReactRoot: WithDsl[TestReactRoot, ImplicitUnit] =
withElement.mapResourse(TestReactRoot(_))(_.unmount())
withElement.mapResource(TestReactRoot(_))(_.unmount())
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice 😄

Copy link
Collaborator

@cquiroz cquiroz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@rpiaggio rpiaggio merged commit 79dddf0 into topic/react18 Dec 30, 2024
2 checks passed
@cquiroz cquiroz deleted the update-act branch December 30, 2024 19:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants