Replies: 2 comments 6 replies
-
What if the let text = try sut.inspect().tree().first(Text.self, where: { try? $0.string() == "abc" }) This offers two benefits, the developer doesn't have to caste after getting the view back. And the algorithm can skip leaf-node views that aren't the specified type. |
Beta Was this translation helpful? Give feedback.
-
In my opinion, using first, filter, and last would be (test) code smells. There should only be one element on the screen matching the specific query. If there's more than one they should either be a) uniquely identified or b) the developer only cares how many there are. But that's more best practices and maybe doesn't translate directly to a framework! |
Beta Was this translation helpful? Give feedback.
-
One of the problems I'm still thinking of is the API for searching the elements. I'll give a short overview of what's technically available, and would love to hear your opinion / suggestions.
The number of SwiftUI view types is quite large and will only keep growing over time, so one of the requirements to the search API will be re-using existing inspection methods, such as
.hStack()
,.anyView()
or.text()
. The introduction of custom APIs, such as.texts()
, for each view type does not make sense to me.A way to make sure a certain view exists in the hierarchy could look like this:
If the search does not throw, the result will be an "anonymous" view (
ViewType.ClassifiedView
), because the specific type of the found view cannot be inferred from thewhere
closure.This means, if we wanted to operate with the found view, we'd need to extract it explicitly after searching:
It is possible to combine the search with unwrapping a specific type of view, but this can be available for simple intent such as "find me the first HStack in the hierarchy you stumble upon":
I want the search to be seamlessly integrated with the traditional inspection chain, so you could activate it at any step:
Another feature that I want to make available is moving outward in the hierarchy: from the found view back to parent views. This way you can quickly locate the right branch in the view tree by searching for a specific text, for example, and then step back to inspect the parent view, which is the true target of the test:
Although moving backward is a simple iteration along a linked list of "ancestor" views, I haven't decided if we need an API like
.findBack(where: { try? $0.hStack() != nil }
to find a predecessor view of typeHStack
.To sum up, below is the list of technically available features I need to design a convenient and concise API for:
Since we need to move inward and outward, I'm considering the following namings:
Lastly, I want to explore the option of making the
.find()
returning aBidirectionalCollection
. This way, it'd allow for more flexibility in tests, for example:but this would make the most common use case less convenient:
Beta Was this translation helpful? Give feedback.
All reactions