From 3c290d3d8d304794b47cf49d29a460182cf49e98 Mon Sep 17 00:00:00 2001 From: Kazumasa Shimomura Date: Wed, 1 Nov 2023 12:09:33 +0900 Subject: [PATCH] fix: adapter tests --- .../ComponentSwiftUISupport.swift | 14 +++++++-- Tests/Adapters/UITableViewAdapterTests.swift | 30 +++++++++---------- .../UITableViewReloadDataUpdaterTests.swift | 4 +-- Tests/Updater/UITableViewUpdaterTests.swift | 12 ++++---- 4 files changed, 35 insertions(+), 25 deletions(-) diff --git a/Sources/SwiftUISupport/ComponentSwiftUISupport.swift b/Sources/SwiftUISupport/ComponentSwiftUISupport.swift index 383e7930..d9d3561a 100644 --- a/Sources/SwiftUISupport/ComponentSwiftUISupport.swift +++ b/Sources/SwiftUISupport/ComponentSwiftUISupport.swift @@ -40,8 +40,18 @@ private struct ComponentRepresenting: UIViewRepresentable { } @available(iOS 16.0, *) - func sizeThatFits(_ proposal: ProposedViewSize, uiView: UIComponentView, context: Context) -> CGSize? { - uiView.intrinsicContentSize + func sizeThatFits( + _ proposal: ProposedViewSize, + uiView: UIComponentView, + context: Context + ) -> CGSize? { + if let width = proposal.width { + uiView.bounds.width = width + } + if let height = proposal.height { + uiView.bounds.height = height + } + return uiView.intrinsicContentSize } } diff --git a/Tests/Adapters/UITableViewAdapterTests.swift b/Tests/Adapters/UITableViewAdapterTests.swift index 4395adb5..15942bb3 100644 --- a/Tests/Adapters/UITableViewAdapterTests.swift +++ b/Tests/Adapters/UITableViewAdapterTests.swift @@ -3,7 +3,7 @@ import XCTest final class UITableViewAdapterTests: XCTestCase { func testNumberOfSections() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) adapter.data = [ Section(id: TestID.a), Section(id: TestID.b), @@ -14,7 +14,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testNumberOfRowsInSection() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) adapter.data = [ Section( id: TestID.a, @@ -36,7 +36,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testCellForRow() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let component = A.Component() adapter.data = [ Section( @@ -61,7 +61,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testViewForHeader() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let component = A.Component() adapter.data = [ Section( @@ -86,7 +86,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testViewForFooter() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let component = A.Component() adapter.data = [ Section( @@ -111,7 +111,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testHeightForRow() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let referenceSize = CGSize(width: 100, height: 200) let defaultHeight: CGFloat = 300 @@ -135,7 +135,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testEstimatedHeightForRow() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let referenceSize = CGSize(width: 100, height: 200) let defaultHeight: CGFloat = 300 @@ -159,7 +159,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testHeightForHeader() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let referenceSize = CGSize(width: 100, height: 200) let defaultHeight: CGFloat = 300 @@ -181,7 +181,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testEstimatedHeightForHeader() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let referenceSize = CGSize(width: 100, height: 200) let defaultHeight: CGFloat = 300 @@ -203,7 +203,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testHeightForFooter() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let referenceSize = CGSize(width: 100, height: 200) let defaultHeight: CGFloat = 300 @@ -225,7 +225,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testEstimatedHeightForFooter() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let referenceSize = CGSize(width: 100, height: 200) let defaultHeight: CGFloat = 300 @@ -247,7 +247,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testDidSelect() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let indexPath = IndexPath(row: 0, section: 0) let node = CellNode(MockIdentifiableComponent(id: TestID.a)) @@ -274,7 +274,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testDisplaying() { - let adapter = UITableViewAdapter() + let adapter = UITableViewAdapter(data: []) let tableView = UITableView() let headerComponent = MockComponent() let cellComponent = MockIdentifiableComponent(id: TestID.a) @@ -309,7 +309,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testCustomCell() { - let adapter = MockCustomTableViewAdapter() + let adapter = MockCustomTableViewAdapter(data: []) let component = A.Component() adapter.data = [ Section( @@ -346,7 +346,7 @@ final class UITableViewAdapterTests: XCTestCase { } func testViewForHeaderFooter() { - let adapter = MockCustomTableViewAdapter() + let adapter = MockCustomTableViewAdapter(data: []) let component = A.Component() adapter.data = [ Section( diff --git a/Tests/Updater/UITableViewReloadDataUpdaterTests.swift b/Tests/Updater/UITableViewReloadDataUpdaterTests.swift index eada2c49..315796c3 100644 --- a/Tests/Updater/UITableViewReloadDataUpdaterTests.swift +++ b/Tests/Updater/UITableViewReloadDataUpdaterTests.swift @@ -5,7 +5,7 @@ import DifferenceKit final class UITableViewReloadDataUpdaterTests: XCTestCase { func testPrepare() { let updater = MockTableViewReloadDataUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView() updater.prepare(target: tableView, adapter: adapter) @@ -16,7 +16,7 @@ final class UITableViewReloadDataUpdaterTests: XCTestCase { func testPerformUpdates() { let updater = MockTableViewReloadDataUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView() updater.performUpdates(target: tableView, adapter: adapter, data: [Section(id: TestID.a)]) diff --git a/Tests/Updater/UITableViewUpdaterTests.swift b/Tests/Updater/UITableViewUpdaterTests.swift index b0163c8d..12393352 100644 --- a/Tests/Updater/UITableViewUpdaterTests.swift +++ b/Tests/Updater/UITableViewUpdaterTests.swift @@ -17,7 +17,7 @@ final class UITableViewUpdaterTests: XCTestCase { func testPrepare() { let updater = MockTableViewUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView() updater.prepare(target: tableView, adapter: adapter) @@ -28,7 +28,7 @@ final class UITableViewUpdaterTests: XCTestCase { func testReloadDataFallbackIfNotInViewHierarchy() { let updater = MockTableViewUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView() performAsyncTests( @@ -44,7 +44,7 @@ final class UITableViewUpdaterTests: XCTestCase { func testReloadDataFallbackIfOverAnimatableChangeCount() { let updater = MockTableViewUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView().addingToWindow() let sourceData = [ Section(id: 0) @@ -71,7 +71,7 @@ final class UITableViewUpdaterTests: XCTestCase { func testNopAndCompletion() { let updater = MockTableViewUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView().addingToWindow() let data = [Section(id: TestID.a)] @@ -91,7 +91,7 @@ final class UITableViewUpdaterTests: XCTestCase { func testDifferentialUpdates() { let updater = MockTableViewUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView().addingToWindow() // Rendering visible components is no needed in this test. @@ -123,7 +123,7 @@ final class UITableViewUpdaterTests: XCTestCase { func testAlwaysRenderVisibleComponents() { let updater = MockTableViewUpdater() - let adapter = MockTableViewAdapter() + let adapter = MockTableViewAdapter(data: []) let tableView = MockTableView().addingToWindow() let visible = (