Skip to content

Commit

Permalink
Fix several tests for watchOS
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Naumov committed Sep 13, 2021
1 parent ad1f7c1 commit 2cc3f88
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 19 deletions.
15 changes: 6 additions & 9 deletions .watchOS/watchOS-Ext/watchOSApp+Testable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import SwiftUI
import WatchKit
import Combine

typealias TestViewSubject = CurrentValueSubject<AnyView?, Never>
typealias TestViewSubject = CurrentValueSubject<[(String, AnyView)], Never>

#if os(watchOS) && DEBUG

Expand All @@ -14,18 +14,15 @@ extension View {

private struct TestViewHost: ViewModifier {

@State private var hostedView: AnyView?
@State private var hostedViews: [(String, AnyView)] = []
let injector: TestViewSubject

func body(content: Content) -> some View {
Group {
if let view = hostedView {
view
} else {
content
}
ZStack {
content
ForEach(hostedViews, id: \.0) { $0.1 }
}
.onReceive(injector) { hostedView = $0 }
.onReceive(injector) { hostedViews = $0 }
}
}

Expand Down
2 changes: 1 addition & 1 deletion .watchOS/watchOS-Ext/watchOSApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ struct watchOSApp: App {
}

final class ExtensionDelegate: NSObject, WKExtensionDelegate {
let testViewSubject = TestViewSubject(nil)
let testViewSubject = TestViewSubject([])
}
21 changes: 15 additions & 6 deletions Sources/ViewInspector/ViewHosting.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public extension ViewHosting {

struct ViewId: Hashable {
let function: String
var key: String { function }
}

static func host<V>(view: V, size: CGSize? = nil, function: String = #function) where V: View {
Expand All @@ -26,7 +27,8 @@ public extension ViewHosting {
}()
#if os(watchOS)
do {
try watchOS(host: AnyView(view))
store(Hosted(medium: medium), viewId: viewId)
try watchOS(host: AnyView(view), viewId: viewId)
} catch {
fatalError(error.localizedDescription)
}
Expand All @@ -53,10 +55,11 @@ public extension ViewHosting {

static func expel(function: String = #function) {
let viewId = ViewId(function: function)
guard let hosted = expel(viewId: viewId) else { return }
#if os(watchOS)
try? watchOS(host: nil)
_ = expel(viewId: viewId)
try? watchOS(host: nil, viewId: viewId)
#else
guard let hosted = expel(viewId: viewId) else { return }
let childVC = hosted.viewController
willMove(childVC, to: nil)
childVC.view.removeFromSuperview()
Expand All @@ -66,8 +69,8 @@ public extension ViewHosting {
}

#if os(watchOS)
private static func watchOS(host view: AnyView?) throws {
typealias Subject = CurrentValueSubject<AnyView?, Never>
private static func watchOS(host view: AnyView?, viewId: ViewId) throws {
typealias Subject = CurrentValueSubject<[(String, AnyView)], Never>
let ext = WKExtension.shared()
guard let subject: Subject = {
if let delegate = ext.delegate,
Expand All @@ -90,7 +93,13 @@ public extension ViewHosting {
throw InspectionError.notSupported(
"View hosting for watchOS is not set up. Please follow this guide: ")
}
subject.send(view)
var array = subject.value
if let view = view {
array.append((viewId.key, view))
} else if let index = array.firstIndex(where: { $0.0 == viewId.key }) {
array.remove(at: index)
}
subject.send(array)
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion Tests/ViewInspectorTests/SwiftUI/GroupBoxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ final class GroupBoxTests: XCTestCase {
XCTAssertEqual(sut, "abc")
}

#if !os(tvOS) && !os(macOS) && !targetEnvironment(macCatalyst)
#if os(iOS)
func testGroupBoxStyleInspection() throws {
guard #available(iOS 14, *) else { return }
let sut = EmptyView().groupBoxStyle(DefaultGroupBoxStyle())
Expand Down
4 changes: 2 additions & 2 deletions Tests/ViewInspectorTests/SwiftUI/TextAttributesTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ final class TextAttributesTests: XCTestCase {
let sut = try view.inspect().text().attributes()
XCTAssertTrue(try sut.isStrikethrough())
XCTAssertEqual(try sut.strikethroughColor(), .black)
if #available(iOS 15.0, tvOS 15.0, macOS 11.6, *) {
if #available(iOS 15.0, tvOS 15.0, macOS 11.6, watchOS 8.0, *) {
XCTAssertEqual(try sut.strikethroughStyle(), .single)
}
}
Expand All @@ -106,7 +106,7 @@ final class TextAttributesTests: XCTestCase {
let sut = try view.inspect().text().attributes()
XCTAssertTrue(try sut.isUnderline())
XCTAssertEqual(try sut.underlineColor(), .black)
if #available(iOS 15.0, tvOS 15.0, macOS 11.6, *) {
if #available(iOS 15.0, tvOS 15.0, macOS 11.6, watchOS 8.0, *) {
XCTAssertEqual(try sut.underlineStyle(), .single)
}
}
Expand Down

0 comments on commit 2cc3f88

Please sign in to comment.