Skip to content

Commit 56e1631

Browse files
vukradoSplittyDev
andauthored
Added introspect for SwiftUI's TextEditor (#52)
* Added introspectTextEditor * Fix compilation on tvOS and macCatalyst * Add AppKit support for `introspectTextView` * Split version-specific test cases up * Only run TextEditor test if macOS 11 is available This hack will resolve itself once CircleCI upgrades to macOS 11. * Revert platform split * Improve tvOS conditionals * Fix variable naming Co-authored-by: SplittyDev <[email protected]>
1 parent b0d3dd7 commit 56e1631

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-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 `.introspectTextView()`.
67
- Update CircleCI config to use Xcode 12.4.0
78

89
## [0.1.2]

Introspect/ViewExtensions.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ extension View {
8888
public func introspectTextField(customize: @escaping (UITextField) -> ()) -> some View {
8989
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
9090
}
91+
92+
/// Finds a `UITextView` from a `SwiftUI.TextEditor`
93+
public func introspectTextView(customize: @escaping (UITextView) -> ()) -> some View {
94+
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
95+
}
9196

9297
/// Finds a `UISwitch` from a `SwiftUI.Toggle`
9398
@available(tvOS, unavailable)
@@ -150,6 +155,11 @@ extension View {
150155
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
151156
}
152157

158+
/// Finds a `NSTextView` from a `SwiftUI.TextView`
159+
public func introspectTextView(customize: @escaping (NSTextView) -> ()) -> some View {
160+
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
161+
}
162+
153163
/// Finds a `NSSlider` from a `SwiftUI.Slider`
154164
public func introspectSlider(customize: @escaping (NSSlider) -> ()) -> some View {
155165
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)

IntrospectTests/AppKitTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,18 @@ private struct TextFieldTestView: View {
7575
}
7676
}
7777

78+
@available(macOS 11.0, *)
79+
private struct TextEditorTestView: View {
80+
let spy: () -> Void
81+
@State private var textEditorValue = ""
82+
var body: some View {
83+
TextEditor(text: $textEditorValue)
84+
.introspectTextView { textView in
85+
self.spy()
86+
}
87+
}
88+
}
89+
7890
@available(macOS 10.15.0, *)
7991
private struct SliderTestView: View {
8092
let spy: () -> Void
@@ -168,6 +180,18 @@ class AppKitTests: XCTestCase {
168180
wait(for: [expectation], timeout: 1)
169181
}
170182

183+
func testTextEditor() {
184+
185+
if #available(macOS 11.0, *) {
186+
let expectation = XCTestExpectation()
187+
let view = TextEditorTestView(spy: {
188+
expectation.fulfill()
189+
})
190+
TestUtils.present(view: view)
191+
wait(for: [expectation], timeout: 1)
192+
}
193+
}
194+
171195
func testSlider() {
172196

173197
let expectation = XCTestExpectation()

IntrospectTests/UIKitTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,19 @@ private struct TextFieldTestView: View {
159159
}
160160
}
161161

162+
@available(iOS 14.0, macCatalyst 14.0, macOS 11.0, *)
163+
@available(tvOS, unavailable, message: "TextEditor is not available in tvOS.")
164+
private struct TextEditorTestView: View {
165+
let spy: () -> Void
166+
@State private var textEditorValue = ""
167+
var body: some View {
168+
TextEditor(text: $textEditorValue)
169+
.introspectTextView { textView in
170+
self.spy()
171+
}
172+
}
173+
}
174+
162175
@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
163176
@available(tvOS, unavailable)
164177
private struct ToggleTestView: View {
@@ -367,6 +380,18 @@ class UIKitTests: XCTestCase {
367380
TestUtils.present(view: view)
368381
wait(for: [expectation], timeout: 1)
369382
}
383+
384+
@available(iOS 14.0, macCatalyst 14.0, macOS 15.0, *)
385+
@available(tvOS, unavailable, message: "TextEditor is not available in tvOS.")
386+
func testTextEditor() {
387+
388+
let expectation = XCTestExpectation()
389+
let view = TextEditorTestView(spy: {
390+
expectation.fulfill()
391+
})
392+
TestUtils.present(view: view)
393+
wait(for: [expectation], timeout: 1)
394+
}
370395
#endif
371396
}
372397
#endif

0 commit comments

Comments
 (0)