Skip to content

Commit 09f2770

Browse files
committed
Add tests
1 parent dc18cf8 commit 09f2770

File tree

5 files changed

+75
-1
lines changed

5 files changed

+75
-1
lines changed

Tests/AnyComponentTests.swift

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,31 @@ final class AnyComponentTests: XCTestCase {
6666
XCTAssertEqual((anyContent as? MockComponent.Content)?.frame, frame)
6767
}
6868

69+
func testIntrinsicContentSizeForView() {
70+
struct TestComponent: Component {
71+
func renderContent() -> UILabel {
72+
UILabel()
73+
}
74+
75+
func render(in content: UILabel) {
76+
content.text = "Test"
77+
}
78+
}
79+
80+
let component = TestComponent()
81+
let content = component.renderContent()
82+
let anyComponent = AnyComponent(component)
83+
let anyContent = anyComponent.renderContent()
84+
85+
component.render(in: content)
86+
anyComponent.render(in: anyContent)
87+
88+
XCTAssertEqual(
89+
component.intrinsicContentSize(for: content),
90+
anyComponent.intrinsicContentSize(for: anyContent)
91+
)
92+
}
93+
6994
func testShouldContentUpdate() {
7095
let component1 = MockComponent(shouldContentUpdate: true)
7196
let component2 = MockComponent(shouldContentUpdate: false)

Tests/ComponentTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,24 @@ final class ComponentTests: XCTestCase {
2222
XCTAssertEqual(content.frame, frame)
2323
}
2424

25+
func testIntrinsicContentSizeForView() {
26+
struct TestComponent: Component {
27+
func renderContent() -> UILabel {
28+
UILabel()
29+
}
30+
31+
func render(in content: UILabel) {
32+
content.text = "Test"
33+
}
34+
}
35+
36+
let component = TestComponent()
37+
let content = component.renderContent()
38+
component.render(in: content)
39+
40+
XCTAssertEqual(component.intrinsicContentSize(for: content), content.intrinsicContentSize)
41+
}
42+
2543
func testShouldContentUpdateWhenEquatable() {
2644
let component1 = A.Component(value: 100)
2745
let component2 = A.Component(value: 200)

Tests/ComponentWrapper/ComponentWrappingTests.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ import XCTest
44
final class ComponentWrappingTests: XCTestCase {
55
func testForwardingActions() {
66
let reuseIdentifier = "testForwardingActions"
7-
let referenceSize = CGSize(width: 200, height: 200)
7+
let referenceSize = CGSize(width: 200, height: 200)
8+
let intrinsicContentSize = CGSize(width: 300, height: 300)
89
let shouldContentUpdate = true
910
let shouldRender = true
1011
let content = UIView()
1112

1213
let mock = MockComponent(
1314
reuseIdentifier: reuseIdentifier,
1415
referenceSize: referenceSize,
16+
intrinsicContentSize: intrinsicContentSize,
1517
shouldContentUpdate: shouldContentUpdate,
1618
shouldRender: shouldRender,
1719
content: content
@@ -26,6 +28,7 @@ final class ComponentWrappingTests: XCTestCase {
2628
XCTAssertEqual(wrapper.renderContent(), content)
2729
XCTAssertEqual(mock.contentCapturedOnRender, content)
2830
XCTAssertEqual(wrapper.referenceSize(in: .zero), referenceSize)
31+
XCTAssertEqual(wrapper.intrinsicContentSize(for: content), intrinsicContentSize)
2932
XCTAssertEqual(wrapper.shouldContentUpdate(with: wrapper), shouldContentUpdate)
3033
XCTAssertEqual(wrapper.shouldRender(next: wrapper, in: content), shouldRender)
3134
XCTAssertEqual(mock.contentCapturedOnLayout, content)

Tests/SwiftUISupport/ComponentSwiftUISupportTests.swift

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,27 @@ final class ComponentSwiftUISupportTests: XCTestCase {
7474

7575
XCTAssertEqual(hostingController.view.sizeThatFits(.zero), TestComponent.testSize)
7676
}
77+
78+
func testIntrinsicContentSize() {
79+
struct TestComponent: Component, View {
80+
static let testSize = CGSize(width: 123, height: 456)
81+
82+
func renderContent() -> UIView {
83+
UIView()
84+
}
85+
86+
func render(in content: UIView) {}
87+
88+
func intrinsicContentSize(for content: UIView) -> CGSize {
89+
Self.testSize
90+
}
91+
}
92+
93+
let component = TestComponent()
94+
let hostingController = UIHostingController(rootView: component.body)
95+
96+
XCTAssertEqual(hostingController.view.sizeThatFits(.zero), TestComponent.testSize)
97+
}
7798
}
7899

79100
#endif

Tests/TestTools.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ enum B {
5656
class MockComponent: Component, Equatable {
5757
let reuseIdentifier: String
5858
let referenceSize: CGSize?
59+
let intrinsicContentSize: CGSize
5960
let shouldContentUpdate: Bool
6061
let shouldRender: Bool
6162
let content: UIView
@@ -68,12 +69,14 @@ class MockComponent: Component, Equatable {
6869
init(
6970
reuseIdentifier: String = "MockComponent",
7071
referenceSize: CGSize? = nil,
72+
intrinsicContentSize: CGSize = .zero,
7173
shouldContentUpdate: Bool = false,
7274
shouldRender: Bool = false,
7375
content: UIView = UIView()
7476
) {
7577
self.reuseIdentifier = reuseIdentifier
7678
self.referenceSize = referenceSize
79+
self.intrinsicContentSize = intrinsicContentSize
7780
self.shouldContentUpdate = shouldContentUpdate
7881
self.shouldRender = shouldRender
7982
self.content = content
@@ -103,6 +106,10 @@ class MockComponent: Component, Equatable {
103106
contentCapturedOnLayout = content
104107
}
105108

109+
func intrinsicContentSize(for content: UIView) -> CGSize {
110+
intrinsicContentSize
111+
}
112+
106113
func contentWillDisplay(_ content: UIView) {
107114
contentCapturedOnWillDisplay = content
108115
}

0 commit comments

Comments
 (0)