Skip to content

Commit

Permalink
Fix recursion detection breaking find for supplementary child views
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Naumov committed Jan 15, 2023
1 parent 724c7e3 commit a1df3bd
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 9 deletions.
4 changes: 3 additions & 1 deletion Sources/ViewInspector/ViewSearch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -493,8 +493,10 @@ private extension UnwrappedView {
func recursionAbsenceCheck() -> Bool {
guard content.isCustomView else { return true }
let typeRef = type(of: content.view)
var isDirectParent = true
return (try? findParent(condition: { parent in
return typeRef == type(of: parent.content.view) && parent.parentView != nil
defer { isDirectParent = false }
return typeRef == type(of: parent.content.view) && !isDirectParent
}, skipFound: 0)) == nil
}
}
Expand Down
40 changes: 32 additions & 8 deletions Tests/ViewInspectorTests/SwiftUI/NavigationLinkTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,14 @@ final class NavigationLinkTests: XCTestCase {
func testSearchWithBindings() throws {
let selection = Binding<String?>(wrappedValue: nil)
let sut = try TestViewBinding(selection: selection).inspect()
XCTAssertNoThrow(try sut.find(text: "GoTo 1"))
XCTAssertNoThrow(try sut.find(text: "GoTo 2"))
let notFoundError = "Search did not find a match"
XCTAssertThrows(try sut.find(text: "Screen 1"), notFoundError)
XCTAssertThrows(try sut.find(text: "Screen 2"), notFoundError)
try sut.navigationView().navigationLink(0).activate()
XCTAssertNoThrow(try sut.find(text: "GoTo 1"))
XCTAssertNoThrow(try sut.find(text: "GoTo 2"))
XCTAssertNoThrow(try sut.find(text: "Screen 1"))
XCTAssertThrows(try sut.find(text: "Screen 2"), notFoundError)
try sut.navigationView().navigationLink(1).activate()
Expand Down Expand Up @@ -196,12 +200,17 @@ final class NavigationLinkTests: XCTestCase {
"Search did not find a match")
XCTAssertThrows(try sut.find(ViewType.Text.self, traversal: .depthFirst, where: { _ in false }),
"Search did not find a match")
XCTAssertEqual(
try sut.find(text: "B to A").pathToRoot,
"""
view(TestRecursiveLinksView.self).navigationView().view(ViewAtoB.self)\
.navigationLink().view(ViewBtoA.self).navigationLink().labelView().text()
""")
XCTAssertNoThrow(try sut.find(text: "B to A"))
}

func testRecursiveGenericReferenceView() throws {
let view = TestRecursiveGenericView
.init(view: TestRecursiveGenericView
.init(view: TestRecursiveGenericView
.init(view: Text("test"))))
let container = "view(TestRecursiveGenericView<EmptyView>.self)."
XCTAssertEqual(try view.inspect().find(text: "test").pathToRoot,
container + container + container + "text()")
}
}

Expand Down Expand Up @@ -259,19 +268,34 @@ extension TestViewState {
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 7.0, *)
private struct TestRecursiveLinksView: View {

struct NavLabel: View {
let text: String
var body: some View {
Text(text)
}
}

struct ViewAtoB: View {
var body: some View {
NavigationLink(destination: ViewBtoA()) { Text("A to B") }
NavigationLink(destination: ViewBtoA()) { NavLabel(text: "A to B") }
}
}

struct ViewBtoA: View {
var body: some View {
NavigationLink(destination: ViewAtoB()) { Text("B to A") }
NavigationLink(destination: ViewAtoB()) { NavLabel(text: "B to A") }
}
}

var body: some View {
NavigationView { ViewAtoB() }
}
}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 7.0, *)
private struct TestRecursiveGenericView<T: View>: View {
let view: T
var body: some View {
view
}
}

0 comments on commit a1df3bd

Please sign in to comment.