Skip to content

Commit 4232b86

Browse files
authored
Add NSButton Introspection (#85)
1 parent 07a80dd commit 4232b86

File tree

3 files changed

+27
-0
lines changed

3 files changed

+27
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Changelog
33

44
## master
55

6+
- Added `.introspectButton()` on macOS
67
- Fix UITextField with cornerRadius
78
- Added `.introspectTabView()` on macOS
89

Introspect/ViewExtensions.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,5 +198,10 @@ extension View {
198198
public func introspectTabView(customize: @escaping (NSTabView) -> ()) -> some View {
199199
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
200200
}
201+
202+
/// Finds a `NSButton` from a `SwiftUI.Button`
203+
public func introspectButton(customize: @escaping (NSButton) -> ()) -> some View {
204+
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
205+
}
201206
}
202207
#endif

IntrospectTests/AppKitTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ private struct TabViewTestView: View {
203203
}
204204
}
205205

206+
@available(macOS 10.15.0, *)
207+
private struct ButtonTestView: View {
208+
let spy: () -> Void
209+
var body: some View {
210+
Button("Test", action: {})
211+
.introspectButton { button in
212+
self.spy()
213+
}
214+
}
215+
}
216+
206217
@available(macOS 10.15.0, *)
207218
class AppKitTests: XCTestCase {
208219

@@ -346,5 +357,15 @@ class AppKitTests: XCTestCase {
346357
TestUtils.present(view: view)
347358
wait(for: [expectation], timeout: TestUtils.Constants.timeout)
348359
}
360+
361+
func testButton() {
362+
363+
let expectation = XCTestExpectation()
364+
let view = ButtonTestView(spy: {
365+
expectation.fulfill()
366+
})
367+
TestUtils.present(view: view)
368+
wait(for: [expectation], timeout: TestUtils.Constants.timeout)
369+
}
349370
}
350371
#endif

0 commit comments

Comments
 (0)