Skip to content

Commit

Permalink
Add an introspect method for UITableViewCell (#18)
Browse files Browse the repository at this point in the history
* Adds an introspect method for UITableViewCell
* Added NSTableCellView introspection
Added line to CHANGELOG
Added tests for UIKit and AppKit
* Removed additional spaces

Co-authored-by: SplittyDev <[email protected]>
  • Loading branch information
dmonagle and SplittyDev authored Mar 13, 2021
1 parent 868a7e8 commit f246b07
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Changelog

## master

- Added `introspectTableViewCell`
- Add Github Action
- Added `.introspectTextView()`.
- Update CircleCI config to use Xcode 12.4.0
Expand Down
12 changes: 11 additions & 1 deletion Introspect/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ extension View {
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
}

/// Finds a `UITableViewCell` from a `SwiftUI.List`, or `SwiftUI.List` child. You can attach this directly to the element inside the list.
public func introspectTableViewCell(customize: @escaping (UITableViewCell) -> ()) -> some View {
return 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 Expand Up @@ -144,7 +149,12 @@ extension View {
public func introspectTableView(customize: @escaping (NSTableView) -> ()) -> some View {
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
}


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

/// Finds a `NSScrollView` from a `SwiftUI.ScrollView`, or `SwiftUI.ScrollView` child.
public func introspectScrollView(customize: @escaping (NSScrollView) -> ()) -> some View {
return introspect(selector: TargetViewSelector.ancestorOrSiblingContaining, customize: customize)
Expand Down
23 changes: 18 additions & 5 deletions IntrospectTests/AppKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,27 @@ private struct ListTestView: View {

let spy1: () -> Void
let spy2: () -> Void

let spyCell1: () -> Void
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()
}
}
}

Expand Down Expand Up @@ -151,17 +160,21 @@ private struct SegmentedControlTestView: View {
class AppKitTests: XCTestCase {

func testList() {

let expectation1 = XCTestExpectation()
let expectation2 = XCTestExpectation()
let cellExpectation1 = XCTestExpectation()
let cellExpectation2 = XCTestExpectation()

let view = ListTestView(
spy1: { expectation1.fulfill() },
spy2: { expectation2.fulfill() }
spy2: { expectation2.fulfill() },
spyCell1: { cellExpectation1.fulfill() },
spyCell2: { cellExpectation2.fulfill() }
)
TestUtils.present(view: view)
wait(for: [expectation1, expectation2], timeout: TestUtils.Constants.timeout)
wait(for: [expectation1, expectation2, cellExpectation1, cellExpectation2], timeout: TestUtils.Constants.timeout)
}

func testScrollView() {

let expectation1 = XCTestExpectation()
Expand Down
20 changes: 17 additions & 3 deletions IntrospectTests/UIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -112,18 +112,27 @@ private struct ListTestView: View {

let spy1: () -> Void
let spy2: () -> Void

let spyCell1: () -> Void
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()
}
}
}

Expand Down Expand Up @@ -294,12 +303,17 @@ class UIKitTests: XCTestCase {

let expectation1 = XCTestExpectation()
let expectation2 = XCTestExpectation()
let cellExpectation1 = XCTestExpectation()
let cellExpectation2 = XCTestExpectation()

let view = ListTestView(
spy1: { expectation1.fulfill() },
spy2: { expectation2.fulfill() }
spy2: { expectation2.fulfill() },
spyCell1: { cellExpectation1.fulfill() },
spyCell2: { cellExpectation2.fulfill() }
)
TestUtils.present(view: view)
wait(for: [expectation1, expectation2], timeout: TestUtils.Constants.timeout)
wait(for: [expectation1, expectation2, cellExpectation1, cellExpectation2], timeout: TestUtils.Constants.timeout)
}

func testScrollView() {
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,11 @@ List {
.introspectTableView { tableView in
tableView.separatorStyle = .none
}
.introspectTableViewCell { cell in
let backgroundView = UIView()
backgroundView.backgroundColor = .clear
cell.selectedBackgroundView = backgroundView
}
```

### ScrollView
Expand Down

0 comments on commit f246b07

Please sign in to comment.