Skip to content

Commit a70391c

Browse files
authored
test: run WorkflowSwiftUI tests in app host (#347)
Adds an app host to to the WorkflowSwiftUI tests to restore the preferred content size test disabled in #343. Fixes #346
1 parent 4a6b2d0 commit a70391c

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

Samples/Project.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,10 @@ let project = Project(
227227
.unitTest(
228228
for: "WorkflowSwiftUI",
229229
sources: "../WorkflowSwiftUI/Tests/**",
230-
dependencies: [.external(name: "WorkflowSwiftUI")]
230+
dependencies: [
231+
.external(name: "WorkflowSwiftUI"),
232+
.target(name: "TestAppHost"),
233+
]
231234
),
232235

233236
// It's not currently possible to create a Tuist target that depends on a macro target. See

WorkflowSwiftUI/Tests/PreferredContentSizeTests.swift

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,15 @@ import XCTest
77

88
final class PreferredContentSizeTests: XCTestCase {
99
func test_preferredContentSize() throws {
10-
// FIXME: actually solve whatever the problem is here
11-
try XCTSkipUnless(UIApplication.shared.delegate != nil)
12-
1310
let maxWidth: CGFloat = 50
1411
let maxHeight: CGFloat = 50
1512

13+
// fudged offset to avoid safe area interference
14+
let origin = CGPoint(x: 50, y: 50)
15+
1616
func assertPreferredContentSize(in axes: Axis.Set) {
1717
let screen = TestScreen(model: .constant(state: State(axes: axes)))
1818
let viewController = screen.buildViewController(in: .empty)
19-
viewController.view.frame = CGRect(x: 0, y: 0, width: maxWidth, height: maxHeight)
20-
viewController.view.layoutIfNeeded()
2119

2220
func assertContentSize(
2321
_ contentSize: CGSize,
@@ -41,16 +39,24 @@ final class PreferredContentSizeTests: XCTestCase {
4139
)
4240
}
4341

44-
assertContentSize(CGSize(width: 20, height: 20))
45-
assertContentSize(CGSize(width: 40, height: 20))
46-
assertContentSize(CGSize(width: 20, height: 40))
47-
assertContentSize(
48-
CGSize(width: 100, height: 100),
49-
expected: CGSize(
50-
width: axes.contains(.horizontal) ? maxWidth : 100,
51-
height: axes.contains(.vertical) ? maxHeight : 100
42+
show(viewController: viewController) { _ in
43+
viewController.view.frame = CGRect(
44+
origin: origin,
45+
size: CGSize(width: maxWidth, height: maxHeight)
46+
)
47+
viewController.view.layoutIfNeeded()
48+
49+
assertContentSize(CGSize(width: 20, height: 20))
50+
assertContentSize(CGSize(width: 40, height: 20))
51+
assertContentSize(CGSize(width: 20, height: 40))
52+
assertContentSize(
53+
CGSize(width: 100, height: 100),
54+
expected: CGSize(
55+
width: axes.contains(.horizontal) ? maxWidth : 100,
56+
height: axes.contains(.vertical) ? maxHeight : 100
57+
)
5258
)
53-
)
59+
}
5460
}
5561

5662
assertPreferredContentSize(in: [])
@@ -116,10 +122,12 @@ private struct TestView: View {
116122
}
117123
}
118124
}
125+
.ignoresSafeArea()
119126
}
120127

121128
var box: some View {
122129
Color.red.frame(width: store.width, height: store.height)
130+
.ignoresSafeArea()
123131
}
124132
}
125133

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#if canImport(UIKit)
2+
3+
import UIKit
4+
import XCTest
5+
6+
extension XCTestCase {
7+
/// Call this method to show a view controller in the test host application during a unit test.
8+
///
9+
/// After the test runs, the view controller will be removed from the view hierarchy.
10+
///
11+
/// A test failure will occur if the host application does not exist, or does not have a root
12+
/// view controller.
13+
///
14+
func show<ViewController: UIViewController>(
15+
viewController: ViewController,
16+
test: (ViewController) throws -> Void
17+
) rethrows {
18+
guard let rootVC = UIApplication.shared.delegate?.window??.rootViewController else {
19+
#if SWIFT_PACKAGE
20+
print("WARNING: Test cannot run directly from swift, it requires an app host. Please run from the Tuist project.")
21+
#else
22+
XCTFail("Cannot present a view controller in a test host that does not have a root window.")
23+
#endif
24+
return
25+
}
26+
27+
rootVC.addChild(viewController)
28+
rootVC.view.addSubview(viewController.view)
29+
viewController.didMove(toParent: rootVC)
30+
31+
try autoreleasepool {
32+
try test(viewController)
33+
}
34+
35+
viewController.willMove(toParent: nil)
36+
viewController.view.removeFromSuperview()
37+
viewController.removeFromParent()
38+
}
39+
}
40+
41+
#endif

0 commit comments

Comments
 (0)