From a02c3fdf15a52d2c7652d93c8944466d7a45c4d3 Mon Sep 17 00:00:00 2001 From: Jesse Squires Date: Sun, 30 Jun 2024 18:52:54 -0700 Subject: [PATCH] add more tests. now at 98% coverage. --- Tests/Fakes/FakeLayout.swift | 8 +++-- Tests/Fakes/FakeSupplementaryViewModel.swift | 4 +-- .../TestCollectionViewDriverReconfigure.swift | 33 +++++++++++++++++++ 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/Tests/Fakes/FakeLayout.swift b/Tests/Fakes/FakeLayout.swift index 70cc0b5..46ab1e7 100644 --- a/Tests/Fakes/FakeLayout.swift +++ b/Tests/Fakes/FakeLayout.swift @@ -16,7 +16,8 @@ import Foundation import UIKit extension UICollectionViewCompositionalLayout { - static func fakeLayout(useNibViews: Bool = false) -> UICollectionViewCompositionalLayout { + static func fakeLayout(addSupplementaryViews: Bool = true, + useNibViews: Bool = false) -> UICollectionViewCompositionalLayout { let fractionalWidth = CGFloat(0.5) // Supplementary Item @@ -37,7 +38,10 @@ extension UICollectionViewCompositionalLayout { // Item let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(fractionalWidth), heightDimension: .fractionalHeight(1)) - let item = NSCollectionLayoutItem(layoutSize: itemSize, supplementaryItems: [view]) + let item = NSCollectionLayoutItem( + layoutSize: itemSize, + supplementaryItems: addSupplementaryViews ? [view] : [] + ) // Group let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1), diff --git a/Tests/Fakes/FakeSupplementaryViewModel.swift b/Tests/Fakes/FakeSupplementaryViewModel.swift index bd2d20e..19cc9a9 100644 --- a/Tests/Fakes/FakeSupplementaryViewModel.swift +++ b/Tests/Fakes/FakeSupplementaryViewModel.swift @@ -49,7 +49,7 @@ final class FakeSupplementaryView: UICollectionViewCell { } struct FakeHeaderViewModel: SupplementaryHeaderViewModel { let title = String.random - nonisolated var id: UniqueIdentifier { self.title } + nonisolated var id: UniqueIdentifier { "Header" } var expectationConfigureView: XCTestExpectation? func configure(view: FakeCollectionHeaderView) { @@ -70,7 +70,7 @@ final class FakeCollectionHeaderView: UICollectionReusableView { } struct FakeFooterViewModel: SupplementaryFooterViewModel { let title = String.random - nonisolated var id: UniqueIdentifier { self.title } + nonisolated var id: UniqueIdentifier { "Footer" } var expectationConfigureView: XCTestExpectation? func configure(view: FakeCollectionFooterView) { diff --git a/Tests/TestCollectionViewDriverReconfigure.swift b/Tests/TestCollectionViewDriverReconfigure.swift index ca74cb6..85618e3 100644 --- a/Tests/TestCollectionViewDriverReconfigure.swift +++ b/Tests/TestCollectionViewDriverReconfigure.swift @@ -48,6 +48,39 @@ final class TestCollectionViewDriverReconfigure: UnitTestCase { self.keepDriverAlive(driver) } + + @MainActor + func test_reconfigure_header_footer() { + let viewController = FakeCollectionViewController() + viewController.collectionView.setCollectionViewLayout( + UICollectionViewCompositionalLayout.fakeLayout(addSupplementaryViews: false), + animated: false + ) + + let driver = CollectionViewDriver(view: viewController.collectionView, options: .test()) + + // Initial header and footer + let header = FakeHeaderViewModel(expectationConfigureView: self.expectation(name: "initial_header")) + let footer = FakeFooterViewModel(expectationConfigureView: self.expectation(name: "initial_footer")) + let cells = [FakeNumberCellViewModel()] + let section = SectionViewModel(id: "id", cells: cells, header: header, footer: footer) + let model = CollectionViewModel(id: "id", sections: [section]) + + driver.update(viewModel: model) + self.simulateAppearance(viewController: viewController) + self.waitForExpectations() + + // Update header and footer to be reconfigured + let updatedHeader = FakeHeaderViewModel(expectationConfigureView: self.expectation(name: "updated_header")) + let updatedFooter = FakeFooterViewModel(expectationConfigureView: self.expectation(name: "updated_footer")) + let updatedSection = SectionViewModel(id: "id", cells: cells, header: updatedHeader, footer: updatedFooter) + let updatedModel = CollectionViewModel(id: "id", sections: [updatedSection]) + + driver.update(viewModel: updatedModel) + self.waitForExpectations() + + self.keepDriverAlive(driver) + } } private struct MyStaticCellViewModel: CellViewModel {