Skip to content

Commit

Permalink
add more tests. now at 98% coverage.
Browse files Browse the repository at this point in the history
  • Loading branch information
jessesquires committed Jul 1, 2024
1 parent de04f16 commit a02c3fd
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Tests/Fakes/FakeLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions Tests/Fakes/FakeSupplementaryViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions Tests/TestCollectionViewDriverReconfigure.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit a02c3fd

Please sign in to comment.