Skip to content

Add support for UICollectionView introspection #169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 10, 2023
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 Introspect/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,16 @@ extension View {
introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
}

/// Finds a `UICollectionView` from a `SwiftUI.List`, or `SwiftUI.List` child.
public func introspectCollectionView(customize: @escaping (UICollectionView) -> ()) -> some View {
introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
}

/// Finds a `UICollectionView` from a `SwiftUI.List`, or `SwiftUI.List` child. You can attach this directly to the element inside the list.
public func introspectCollectionViewCell(customize: @escaping (UICollectionViewCell) -> ()) -> some View {
introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
}

/// Finds a `UIScrollView` from a `SwiftUI.ScrollView`, or `SwiftUI.ScrollView` child.
public func introspectScrollView(customize: @escaping (UIScrollView) -> ()) -> some View {
if #available(iOS 14.0, tvOS 14.0, macOS 11.0, *) {
Expand Down
52 changes: 36 additions & 16 deletions IntrospectTests/UIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,42 @@ private struct ListTestView: View {
let spyCell2: () -> Void

var body: some View {
List {
Text("Item 1")
Text("Item 2")
.introspectTableView { tableView in
self.spy2()
}
.introspectTableViewCell { cell in
self.spyCell2()
}

}
.introspectTableView { tableView in
self.spy1()
}
.introspectTableViewCell { cell in
self.spyCell1()
if #available(iOS 16, tvOS 16, macOS 13, *) {
List {
Text("Item 1")
Text("Item 2")
.introspectCollectionView { tableView in
self.spy2()
}
.introspectCollectionViewCell { cell in
self.spyCell2()
}

}
.introspectCollectionView { tableView in
self.spy1()
}
.introspectCollectionViewCell { cell in
self.spyCell1()
}
} else {
List {
Text("Item 1")
Text("Item 2")
.introspectTableView { tableView in
self.spy2()
}
.introspectTableViewCell { cell in
self.spyCell2()
}

}
.introspectTableView { tableView in
self.spy1()
}
.introspectTableViewCell { cell in
self.spyCell1()
}
}
}
}
Expand Down