Skip to content

Commit a02c3fd

Browse files
committed
add more tests. now at 98% coverage.
1 parent de04f16 commit a02c3fd

File tree

3 files changed

+41
-4
lines changed

3 files changed

+41
-4
lines changed

Tests/Fakes/FakeLayout.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ import Foundation
1616
import UIKit
1717

1818
extension UICollectionViewCompositionalLayout {
19-
static func fakeLayout(useNibViews: Bool = false) -> UICollectionViewCompositionalLayout {
19+
static func fakeLayout(addSupplementaryViews: Bool = true,
20+
useNibViews: Bool = false) -> UICollectionViewCompositionalLayout {
2021
let fractionalWidth = CGFloat(0.5)
2122

2223
// Supplementary Item
@@ -37,7 +38,10 @@ extension UICollectionViewCompositionalLayout {
3738
// Item
3839
let itemSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(fractionalWidth),
3940
heightDimension: .fractionalHeight(1))
40-
let item = NSCollectionLayoutItem(layoutSize: itemSize, supplementaryItems: [view])
41+
let item = NSCollectionLayoutItem(
42+
layoutSize: itemSize,
43+
supplementaryItems: addSupplementaryViews ? [view] : []
44+
)
4145

4246
// Group
4347
let groupSize = NSCollectionLayoutSize(widthDimension: .fractionalWidth(1),

Tests/Fakes/FakeSupplementaryViewModel.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ final class FakeSupplementaryView: UICollectionViewCell { }
4949
struct FakeHeaderViewModel: SupplementaryHeaderViewModel {
5050
let title = String.random
5151

52-
nonisolated var id: UniqueIdentifier { self.title }
52+
nonisolated var id: UniqueIdentifier { "Header" }
5353

5454
var expectationConfigureView: XCTestExpectation?
5555
func configure(view: FakeCollectionHeaderView) {
@@ -70,7 +70,7 @@ final class FakeCollectionHeaderView: UICollectionReusableView { }
7070
struct FakeFooterViewModel: SupplementaryFooterViewModel {
7171
let title = String.random
7272

73-
nonisolated var id: UniqueIdentifier { self.title }
73+
nonisolated var id: UniqueIdentifier { "Footer" }
7474

7575
var expectationConfigureView: XCTestExpectation?
7676
func configure(view: FakeCollectionFooterView) {

Tests/TestCollectionViewDriverReconfigure.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,39 @@ final class TestCollectionViewDriverReconfigure: UnitTestCase {
4848

4949
self.keepDriverAlive(driver)
5050
}
51+
52+
@MainActor
53+
func test_reconfigure_header_footer() {
54+
let viewController = FakeCollectionViewController()
55+
viewController.collectionView.setCollectionViewLayout(
56+
UICollectionViewCompositionalLayout.fakeLayout(addSupplementaryViews: false),
57+
animated: false
58+
)
59+
60+
let driver = CollectionViewDriver(view: viewController.collectionView, options: .test())
61+
62+
// Initial header and footer
63+
let header = FakeHeaderViewModel(expectationConfigureView: self.expectation(name: "initial_header"))
64+
let footer = FakeFooterViewModel(expectationConfigureView: self.expectation(name: "initial_footer"))
65+
let cells = [FakeNumberCellViewModel()]
66+
let section = SectionViewModel(id: "id", cells: cells, header: header, footer: footer)
67+
let model = CollectionViewModel(id: "id", sections: [section])
68+
69+
driver.update(viewModel: model)
70+
self.simulateAppearance(viewController: viewController)
71+
self.waitForExpectations()
72+
73+
// Update header and footer to be reconfigured
74+
let updatedHeader = FakeHeaderViewModel(expectationConfigureView: self.expectation(name: "updated_header"))
75+
let updatedFooter = FakeFooterViewModel(expectationConfigureView: self.expectation(name: "updated_footer"))
76+
let updatedSection = SectionViewModel(id: "id", cells: cells, header: updatedHeader, footer: updatedFooter)
77+
let updatedModel = CollectionViewModel(id: "id", sections: [updatedSection])
78+
79+
driver.update(viewModel: updatedModel)
80+
self.waitForExpectations()
81+
82+
self.keepDriverAlive(driver)
83+
}
5184
}
5285

5386
private struct MyStaticCellViewModel: CellViewModel {

0 commit comments

Comments
 (0)