From d2bfcf981838c3d8b0725fe92c1905731324c6e8 Mon Sep 17 00:00:00 2001 From: Greg Littlefield Date: Tue, 26 Mar 2024 11:07:28 -0700 Subject: [PATCH] Make root args to query(All)ByTestId nullable, add tests --- lib/src/over_react_test/react_util.dart | 10 ++++++++-- test/over_react_test/react_util_test.dart | 8 ++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/src/over_react_test/react_util.dart b/lib/src/over_react_test/react_util.dart index f352b0f..3dd973d 100644 --- a/lib/src/over_react_test/react_util.dart +++ b/lib/src/over_react_test/react_util.dart @@ -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. @@ -450,7 +453,7 @@ Element? getComponentRootDomByTestId(dynamic root, String value, {String key = d /// queryByTestId(renderedInstance, 'value'); // returns the `inner` `
` /// /// 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); @@ -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. @@ -497,7 +503,7 @@ Element? queryByTestId(Object root, String value, {String key = defaultTestIdKey ///
/// /// queryAllByTestId(renderedInstance, 'value'); // returns both `inner` `
`s -List queryAllByTestId(Object root, String value, {String key = defaultTestIdKey, bool searchInShadowDom = false, int? shadowDepth}) { +List queryAllByTestId(dynamic root, String value, {String key = defaultTestIdKey, bool searchInShadowDom = false, int? shadowDepth}) { ArgumentError.checkNotNull(root, 'root'); final node = findDomNode(root); diff --git a/test/over_react_test/react_util_test.dart b/test/over_react_test/react_util_test.dart index b432cb9..93d6850 100644 --- a/test/over_react_test/react_util_test.dart +++ b/test/over_react_test/react_util_test.dart @@ -809,6 +809,10 @@ main() { }); }); + test('throws when root is null', () { + expect(() => queryByTestId(null, 'unusedTestId'), throwsA(isA())); + }); + test('throws a helpful error when findDomNode(root) is null', () { var jacket = mount(RendersNothing()()); expect( @@ -934,6 +938,10 @@ main() { }); }); + test('throws when root is null', () { + expect(() => queryAllByTestId(null, 'unusedTestId'), throwsA(isA())); + }); + test('throws a helpful error when findDomNode(root) is null', () { var jacket = mount(RendersNothing()()); expect(