-
Notifications
You must be signed in to change notification settings - Fork 96
/
ComponentSwiftUISupportTests.swift
111 lines (83 loc) 路 3.01 KB
/
ComponentSwiftUISupportTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#if canImport(SwiftUI) && !(os(iOS) && (arch(i386) || arch(arm)))
import XCTest
import SwiftUI
@testable import Carbon
final class ComponentSwiftUISupportTests: XCTestCase {
func testDisplayLifecycle() {
guard #available(iOS 13.0, *) else {
return
}
struct TestComponent: Component, View {
var willDisplay: () -> Void
var didEndDisplay: () -> Void
func renderContent() -> UIView {
UIView()
}
func render(in content: UIView) {}
func contentWillDisplay(_ content: UIView) {
willDisplay()
}
func contentDidEndDisplay(_ content: UIView) {
didEndDisplay()
}
}
var isWillDisplayCalled = false
var isDidEndDisplayCalled = false
let component = TestComponent(
willDisplay: { isWillDisplayCalled = true },
didEndDisplay: { isDidEndDisplayCalled = true }
)
let hostingController = UIHostingController(rootView: AnyView(component.body))
XCTAssertFalse(isWillDisplayCalled)
XCTAssertFalse(isDidEndDisplayCalled)
let window = UIWindow()
window.rootViewController = hostingController
window.isHidden = false
window.setNeedsLayout()
window.layoutIfNeeded()
XCTAssertTrue(isWillDisplayCalled)
XCTAssertFalse(isDidEndDisplayCalled)
hostingController.rootView = AnyView(EmptyView())
window.setNeedsLayout()
window.layoutIfNeeded()
XCTAssertTrue(isWillDisplayCalled)
XCTAssertTrue(isDidEndDisplayCalled)
}
func testReferenceSize() {
guard #available(iOS 13.0, *) else {
return
}
struct TestComponent: Component, View {
static let testSize = CGSize(width: 123, height: 456)
func renderContent() -> UIView {
UIView()
}
func render(in content: UIView) {}
func referenceSize(in bounds: CGRect) -> CGSize? {
Self.testSize
}
}
let component = TestComponent()
let hostingController = UIHostingController(rootView: component.body)
XCTAssertEqual(hostingController.view.sizeThatFits(.zero), TestComponent.testSize)
}
func testIntrinsicContentSize() {
guard #available(iOS 13.0, *) else {
return
}
struct TestComponent: Component, View {
static let testSize = CGSize(width: 123, height: 456)
func renderContent() -> UIView {
UIView()
}
func render(in content: UIView) {}
func intrinsicContentSize(for content: UIView) -> CGSize {
Self.testSize
}
}
let component = TestComponent()
let hostingController = UIHostingController(rootView: component.body)
XCTAssertEqual(hostingController.view.sizeThatFits(.zero), TestComponent.testSize)
}
}
#endif