-
Notifications
You must be signed in to change notification settings - Fork 2
FED-3659 Add Dart bindings for React act #86
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a7727b
Add act interop from ticket
sydneyjodon-wk 6c68538
Add a test
sydneyjodon-wk 3822e04
Check async return type
sydneyjodon-wk cba8c76
Fix to work for async return types
sydneyjodon-wk fa4412c
Add error throwing tests
sydneyjodon-wk b8d2b64
Format
sydneyjodon-wk 5b1726e
Update changelog
sydneyjodon-wk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| // Copyright 2025 Workiva Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| @JS() | ||
| library; | ||
|
|
||
| import 'dart:async'; | ||
| import 'dart:html'; | ||
|
|
||
| import 'package:js/js.dart'; | ||
|
|
||
| @JS('rtl.act') | ||
| external dynamic _act(void Function([dynamic, dynamic, dynamic]) callback); | ||
|
|
||
| /// A test helper to apply pending React updates before making assertions. | ||
| /// | ||
| /// This is RTL's version of React.act for convenience because it handles setting `IS_REACT_ACT_ENVIRONMENT` | ||
| /// to avoid https://react.dev/reference/react/act#error-the-current-testing-environment-is-not-configured-to-support-act. | ||
| /// | ||
| /// See RTL docs: https://testing-library.com/docs/react-testing-library/api/#act | ||
| /// See React docs: https://react.dev/reference/react/act | ||
| Future<void> act(FutureOr<void> Function() callback) async { | ||
| final callbackReturnValue = callback(); | ||
| final jsCallback = | ||
| ([_, __, ___]) => callbackReturnValue is Future ? futureToPromise(callbackReturnValue) : callbackReturnValue; | ||
| final promise = _act(allowInterop(jsCallback)) as Object; | ||
| await promiseToFuture<void>(promise); | ||
| } | ||
|
|
||
| // Copied from react-dart https://github.com/Workiva/react-dart/blob/489d86fa72ab9a4ff60972180cf9a46c6ca2cffd/lib/src/js_interop_util.dart#L24-L40 | ||
| /// Creates JS `Promise` which is resolved when [future] completes. | ||
| /// | ||
| /// See also: | ||
| /// - [promiseToFuture] | ||
| Promise futureToPromise<T>(Future<T> future) { | ||
| return Promise(allowInterop((resolve, reject) { | ||
| future.then((result) => resolve(result), onError: reject); | ||
| })); | ||
| } | ||
|
|
||
| @JS() | ||
| abstract class Promise { | ||
| external factory Promise( | ||
| Function(dynamic Function(dynamic value) resolve, dynamic Function(dynamic error) reject) executor); | ||
|
|
||
| external Promise then(dynamic Function(dynamic value) onFulfilled, [dynamic Function(dynamic error) onRejected]); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| // Copyright 2025 Workiva Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import 'dart:html'; | ||
|
|
||
| import 'package:react/hooks.dart'; | ||
| import 'package:react/react.dart' as react; | ||
| import 'package:react/react_client.dart'; | ||
| import 'package:react_testing_library/react_testing_library.dart' as rtl; | ||
| import 'package:test/test.dart'; | ||
|
|
||
| import '../util/exception.dart'; | ||
|
|
||
| void main() { | ||
| group('act', () { | ||
| setUp(() => useEffectCalls = 0); | ||
|
|
||
| test('useEffect call is missed when act is not used', () { | ||
| expect(useEffectCalls, 0); | ||
| final view = rtl.render(ActTest({})); | ||
| expect(useEffectCalls, 1); | ||
|
|
||
| final button = view.getByRole('button') as ButtonElement; | ||
| button.click(); | ||
| expect(useEffectCalls, 1); | ||
| }); | ||
|
|
||
| test('useEffect call is caught when act is used', () async { | ||
| expect(useEffectCalls, 0); | ||
| final view = rtl.render(ActTest({})); | ||
| expect(useEffectCalls, 1); | ||
|
|
||
| await rtl.act(() { | ||
| final button = view.getByRole('button') as ButtonElement; | ||
| button.click(); | ||
| }); | ||
| expect(useEffectCalls, 2); | ||
| }); | ||
|
|
||
| test('also works with an async callback', () async { | ||
| expect(useEffectCalls, 0); | ||
| final view = rtl.render(ActTest({})); | ||
| expect(useEffectCalls, 1); | ||
|
|
||
| Future<void> asyncCallback() async { | ||
| final button = await view.findByRole('button') as ButtonElement; | ||
| button.click(); | ||
| } | ||
|
|
||
| await rtl.act(asyncCallback); | ||
| expect(useEffectCalls, 2); | ||
| }); | ||
|
|
||
| test('errors thrown in sync callback propagate', () { | ||
| expect(() async => await rtl.act(() => throw ExceptionForTesting()), throwsA(isA<ExceptionForTesting>())); | ||
| }); | ||
|
|
||
| test('errors thrown in async callback propagate', () { | ||
| expect(() async => await rtl.act(() async => throw ExceptionForTesting()), throwsA(isA<ExceptionForTesting>())); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| var useEffectCalls = 0; | ||
|
|
||
| ReactDartFunctionComponentFactoryProxy ActTest = react.registerFunctionComponent((props) { | ||
| final text = useState('123'); | ||
| useEffect(() => useEffectCalls++); | ||
| return react.button({ | ||
| 'onClick': (e) { | ||
| text.set('abc'); | ||
| }, | ||
| }, [ | ||
| 'Click me!' | ||
| ]); | ||
| }); | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we also add some tests that verify that any errors in the callback successfully propagate, both for sync and async callbacks?
That way
a) we'll verify that the errors aren't swallowed and
b) we'll know whether anything weird happens to Dart exceptions in the JS interop that might require
throwErrorFromJSor something).Could do something like this (I stumbled across that when looking through code trying to remember the name of
throwErrorFromJS):react_testing_library/test/unit/dom/wait_for_test.dart
Lines 122 to 123 in 8288d6f