Skip to content
Snippets Groups Projects
Commit ddc0ffab authored by Dariusz Rybicki's avatar Dariusz Rybicki
Browse files

Fix issue with cell building

parent c474540c
No related branches found
No related tags found
2 merge requests!54Releasing 1.1.4,!51CollectionView library
......@@ -52,9 +52,12 @@ extension CellFactory {
factories.forEach { $0.register(in: collectionView) }
},
build: .init { model, collectionView, indexPath in
factories.lazy
.compactMap { $0.build(for: model, in: collectionView, at: indexPath) }
.first
for factory in factories {
if let cell = factory.build(for: model, in: collectionView, at: indexPath) {
return cell
}
}
return nil
}
)
}
......
import CustomDump
import XCTest
@testable import CollectionView
final class CellFactoryTests: XCTestCase {
func testCombined() {
struct Cell: Equatable {
var model: Int
var collectionView: UICollectionView
var indexPath: IndexPath
}
var didRegisterFirst = [UICollectionView]()
var didRegisterSecond = [UICollectionView]()
var didRegisterThird = [UICollectionView]()
class Cell: UICollectionViewCell {
var collectionView: UICollectionView?
var indexPath: IndexPath?
}
var didBuildFirst = [Cell]()
var didBuildSecond = [Cell]()
var didBuildThird = [Cell]()
let factory = CellFactory<Int>.combined(
.init(
register: .init { didRegisterFirst.append($0) },
build: .init { model, collectionView, indexPath in
guard model == 1 else { return nil }
let cell = Cell()
cell.collectionView = collectionView
cell.indexPath = indexPath
return cell
didBuildFirst.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath))
return UICollectionViewCell()
}
),
.init(
register: .init { didRegisterSecond.append($0) },
build: .init { model, collectionView, indexPath in
guard model == 2 else { return nil }
let cell = Cell()
cell.collectionView = collectionView
cell.indexPath = indexPath
return cell
didBuildSecond.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath))
return UICollectionViewCell()
}
),
.init(
register: .init { didRegisterThird.append($0) },
build: .init { model, collectionView, indexPath in
guard model == 3 else { return nil }
let cell = Cell()
cell.collectionView = collectionView
cell.indexPath = indexPath
return cell
didBuildThird.append(Cell(model: model, collectionView: collectionView, indexPath: indexPath))
return UICollectionViewCell()
}
)
)
......@@ -53,23 +53,53 @@ final class CellFactoryTests: XCTestCase {
XCTAssertEqual(didRegisterSecond, [collectionView])
XCTAssertEqual(didRegisterThird, [collectionView])
let firstCell = factory.build(for: 1, in: collectionView, at: IndexPath(item: 0, section: 1)) as? Cell
let firstCell = factory.build(for: 1, in: collectionView, at: IndexPath(item: 0, section: 1))
XCTAssertEqual(firstCell?.collectionView, collectionView)
XCTAssertEqual(firstCell?.indexPath, IndexPath(item: 0, section: 1))
XCTAssertNotNil(firstCell)
XCTAssertNoDifference(didBuildFirst, [Cell(
model: 1,
collectionView: collectionView,
indexPath: IndexPath(row: 0, section: 1)
)])
XCTAssertNoDifference(didBuildSecond, [])
XCTAssertNoDifference(didBuildThird, [])
let secondCell = factory.build(for: 2, in: collectionView, at: IndexPath(item: 2, section: 3)) as? Cell
didBuildFirst = []
didBuildSecond = []
didBuildThird = []
let secondCell = factory.build(for: 2, in: collectionView, at: IndexPath(item: 2, section: 3))
XCTAssertEqual(secondCell?.collectionView, collectionView)
XCTAssertEqual(secondCell?.indexPath, IndexPath(item: 2, section: 3))
XCTAssertNotNil(secondCell)
XCTAssertNoDifference(didBuildFirst, [])
XCTAssertNoDifference(didBuildSecond, [Cell(
model: 2,
collectionView: collectionView,
indexPath: IndexPath(row: 2, section: 3)
)])
XCTAssertNoDifference(didBuildThird, [])
let thirdCell = factory.build(for: 3, in: collectionView, at: IndexPath(item: 4, section: 5)) as? Cell
didBuildFirst = []
didBuildSecond = []
didBuildThird = []
let thirdCell = factory.build(for: 3, in: collectionView, at: IndexPath(item: 4, section: 5))
XCTAssertEqual(thirdCell?.collectionView, collectionView)
XCTAssertEqual(thirdCell?.indexPath, IndexPath(item: 4, section: 5))
XCTAssertNotNil(thirdCell)
XCTAssertNoDifference(didBuildFirst, [])
XCTAssertNoDifference(didBuildSecond, [])
XCTAssertNoDifference(didBuildThird, [Cell(
model: 3,
collectionView: collectionView,
indexPath: IndexPath(row: 4, section: 5)
)])
didBuildFirst = []
didBuildSecond = []
didBuildThird = []
let otherCell = factory.build(for: 4, in: collectionView, at: IndexPath(item: 0, section: 0))
XCTAssertNil(otherCell)
XCTAssertNoDifference(didBuildFirst, [])
XCTAssertNoDifference(didBuildSecond, [])
XCTAssertNoDifference(didBuildThird, [])
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment