Skip to content

Commit

Permalink
Make root args to query(All)ByTestId nullable, add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
greglittlefield-wf committed Mar 26, 2024
1 parent 7b1ce54 commit d2bfcf9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/src/over_react_test/react_util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,9 @@ Element? getComponentRootDomByTestId(dynamic root, String value, {String key = d
/// Returns the [Element] of the first descendant of [root] that has its [key] html attribute value set to a
/// space-delimited string containing [value].
///
/// Throws if [root] or `findDomNode(root)` is `null`. [root] is kept nullable for convenience, since the input
/// to this function is often a `dynamic` component instance value.
///
/// Setting [searchInShadowDom] to true will allow the query to search within ShadowRoots that use `mode:"open"`.
/// [shadowDepth] will limit how many layers of ShadowDOM will searched. By default it will search infinitely deep, and this
/// should only be needed if there are a lot of ShadowRoots within ShadowRoots.
Expand Down Expand Up @@ -450,7 +453,7 @@ Element? getComponentRootDomByTestId(dynamic root, String value, {String key = d
/// queryByTestId(renderedInstance, 'value'); // returns the `inner` `<div>`
///
/// Related: [queryAllByTestId], [getComponentRootDomByTestId].
Element? queryByTestId(Object root, String value, {String key = defaultTestIdKey, bool searchInShadowDom = false, int? shadowDepth}) {
Element? queryByTestId(dynamic root, String value, {String key = defaultTestIdKey, bool searchInShadowDom = false, int? shadowDepth}) {
ArgumentError.checkNotNull(root, 'root');

final node = findDomNode(root);
Expand All @@ -464,6 +467,9 @@ Element? queryByTestId(Object root, String value, {String key = defaultTestIdKey

/// Returns all descendant [Element]s of [root] that has their [key] html attribute value set to [value].
///
/// Throws if [root] or `findDomNode(root)` is `null`. [root] is kept nullable for convenience, since the input
/// to this function is often a `dynamic` component instance value.
///
/// Setting [searchInShadowDom] to true will allow the query to search within ShadowRoots that use `mode:"open"`.
/// [shadowDepth] will limit how many layers of ShadowDOM will searched. By default it will search infinitely deep, and this
/// should only be needed if there are a lot of ShadowRoots within ShadowRoots.
Expand Down Expand Up @@ -497,7 +503,7 @@ Element? queryByTestId(Object root, String value, {String key = defaultTestIdKey
/// </div>
///
/// queryAllByTestId(renderedInstance, 'value'); // returns both `inner` `<div>`s
List<Element> queryAllByTestId(Object root, String value, {String key = defaultTestIdKey, bool searchInShadowDom = false, int? shadowDepth}) {
List<Element> queryAllByTestId(dynamic root, String value, {String key = defaultTestIdKey, bool searchInShadowDom = false, int? shadowDepth}) {
ArgumentError.checkNotNull(root, 'root');

final node = findDomNode(root);
Expand Down
8 changes: 8 additions & 0 deletions test/over_react_test/react_util_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,10 @@ main() {
});
});

test('throws when root is null', () {
expect(() => queryByTestId(null, 'unusedTestId'), throwsA(isA<ArgumentError>()));
});

test('throws a helpful error when findDomNode(root) is null', () {
var jacket = mount(RendersNothing()());
expect(
Expand Down Expand Up @@ -934,6 +938,10 @@ main() {
});
});

test('throws when root is null', () {
expect(() => queryAllByTestId(null, 'unusedTestId'), throwsA(isA<ArgumentError>()));
});

test('throws a helpful error when findDomNode(root) is null', () {
var jacket = mount(RendersNothing()());
expect(
Expand Down

0 comments on commit d2bfcf9

Please sign in to comment.