Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# React Testing Library Changelog

## 3.1.0
* [#86](https://github.com/Workiva/react_testing_library/pull/86) Add Dart bindings for React `act`

## 3.0.3
* [#84](https://github.com/Workiva/react_testing_library/pull/84) React 18 dual support

## 3.0.2
* [#76](https://github.com/Workiva/react_testing_library/pull/76) GHA OSS changes
* [#77](https://github.com/Workiva/react_testing_library/pull/77) Workiva analysis options v2

## 3.0.1
* [#75](https://github.com/Workiva/react_testing_library/pull/75) Update changelog for 3.0.0 Release

Expand Down
1 change: 1 addition & 0 deletions lib/react/react.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
library rtl.react;

export '../src/react/render/render.dart' show render, RenderResult;
export '../src/react/act.dart' show act;
58 changes: 58 additions & 0 deletions lib/src/react/act.dart
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]);
}
87 changes: 87 additions & 0 deletions test/unit/react/act_test.dart
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);
});
Copy link
Contributor

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 throwErrorFromJS or something).

Could do something like this (I stumbled across that when looking through code trying to remember the name of throwErrorFromJS):

expect(() => rtl.waitFor(() => throw ExceptionForTesting(), container: view.container),
throwsA(isA<ExceptionForTesting>()));


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!'
]);
});