Skip to content

Added introspect for SwiftUI's TextEditor #52

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 10 commits into from
Mar 10, 2021
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 `.introspectTextView()`.
- Update CircleCI config to use Xcode 12.4.0

## [0.1.2]
Expand Down
10 changes: 10 additions & 0 deletions Introspect/ViewExtensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ extension View {
public func introspectTextField(customize: @escaping (UITextField) -> ()) -> some View {
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
}

/// Finds a `UITextView` from a `SwiftUI.TextEditor`
public func introspectTextView(customize: @escaping (UITextView) -> ()) -> some View {
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
}

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

/// Finds a `NSTextView` from a `SwiftUI.TextView`
public func introspectTextView(customize: @escaping (NSTextView) -> ()) -> some View {
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
}

/// Finds a `NSSlider` from a `SwiftUI.Slider`
public func introspectSlider(customize: @escaping (NSSlider) -> ()) -> some View {
return introspect(selector: TargetViewSelector.siblingContaining, customize: customize)
Expand Down
24 changes: 24 additions & 0 deletions IntrospectTests/AppKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,18 @@ private struct TextFieldTestView: View {
}
}

@available(macOS 11.0, *)
private struct TextEditorTestView: View {
let spy: () -> Void
@State private var textEditorValue = ""
var body: some View {
TextEditor(text: $textEditorValue)
.introspectTextView { textView in
self.spy()
}
}
}

@available(macOS 10.15.0, *)
private struct SliderTestView: View {
let spy: () -> Void
Expand Down Expand Up @@ -168,6 +180,18 @@ class AppKitTests: XCTestCase {
wait(for: [expectation], timeout: 1)
}

func testTextEditor() {

if #available(macOS 11.0, *) {
let expectation = XCTestExpectation()
let view = TextEditorTestView(spy: {
expectation.fulfill()
})
TestUtils.present(view: view)
wait(for: [expectation], timeout: 1)
}
}

func testSlider() {

let expectation = XCTestExpectation()
Expand Down
25 changes: 25 additions & 0 deletions IntrospectTests/UIKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,19 @@ private struct TextFieldTestView: View {
}
}

@available(iOS 14.0, macCatalyst 14.0, macOS 11.0, *)
@available(tvOS, unavailable, message: "TextEditor is not available in tvOS.")
private struct TextEditorTestView: View {
let spy: () -> Void
@State private var textEditorValue = ""
var body: some View {
TextEditor(text: $textEditorValue)
.introspectTextView { textView in
self.spy()
}
}
}

@available(iOS 13.0, tvOS 13.0, macOS 10.15.0, *)
@available(tvOS, unavailable)
private struct ToggleTestView: View {
Expand Down Expand Up @@ -367,6 +380,18 @@ class UIKitTests: XCTestCase {
TestUtils.present(view: view)
wait(for: [expectation], timeout: 1)
}

@available(iOS 14.0, macCatalyst 14.0, macOS 15.0, *)
@available(tvOS, unavailable, message: "TextEditor is not available in tvOS.")
func testTextEditor() {

let expectation = XCTestExpectation()
let view = TextEditorTestView(spy: {
expectation.fulfill()
})
TestUtils.present(view: view)
wait(for: [expectation], timeout: 1)
}
#endif
}
#endif