From 46460779e6a7df574347703926d262a8af73cb9b Mon Sep 17 00:00:00 2001 From: Bobby Bobak Date: Tue, 7 Apr 2020 10:11:42 +0100 Subject: [PATCH] Add tests for newly added methods and update the implementation of the CollectionViewDiffableDataSource with canMove/moveItemAt methods --- .../CollectionViewDiffableDataSource.swift | 23 ++++++++++++ ...ollectionViewDiffableDataSourceTests.swift | 18 ++++++++++ Tests/TableViewDiffableDataSourceTests.swift | 36 +++++++++++++++++++ 3 files changed, 77 insertions(+) diff --git a/Sources/UIKit/CollectionViewDiffableDataSource.swift b/Sources/UIKit/CollectionViewDiffableDataSource.swift index 789aedf..8c13d99 100644 --- a/Sources/UIKit/CollectionViewDiffableDataSource.swift +++ b/Sources/UIKit/CollectionViewDiffableDataSource.swift @@ -132,6 +132,29 @@ open class CollectionViewDiffableDataSource Bool { + return false + } + + /// Moves a row at given index path. + /// + /// - Parameters: + /// - collectionView: A collection view instance managed by `self`. + /// - sourceIndexPath: An index path for given cell position. + /// - destinationIndexPath: An index path for target cell position. + /// + /// - Returns: Void. + public func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) { + // Empty implementation. + } } #endif diff --git a/Tests/CollectionViewDiffableDataSourceTests.swift b/Tests/CollectionViewDiffableDataSourceTests.swift index 872683a..212eff5 100644 --- a/Tests/CollectionViewDiffableDataSourceTests.swift +++ b/Tests/CollectionViewDiffableDataSourceTests.swift @@ -167,6 +167,24 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase { cell ) } + + func testCanMoveRowAt() { + let collectionView = MockCollectionView() + let cell = UICollectionViewCell() + let dataSource = CollectionViewDiffableDataSource(collectionView: collectionView) { _, _, _ in + cell + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([0, 1, 2]) + snapshot.appendItems([0, 1, 2], toSection: 0) + dataSource.apply(snapshot) + + XCTAssertEqual( + dataSource.collectionView(collectionView, canMoveItemAt: IndexPath(item: 1, section: 0)), + false + ) + } } final class MockCollectionView: UICollectionView { diff --git a/Tests/TableViewDiffableDataSourceTests.swift b/Tests/TableViewDiffableDataSourceTests.swift index ce3e1cb..8cec845 100644 --- a/Tests/TableViewDiffableDataSourceTests.swift +++ b/Tests/TableViewDiffableDataSourceTests.swift @@ -167,6 +167,42 @@ final class TableViewDiffableDataSourceTests: XCTestCase { cell ) } + + func testCanEditRowAt() { + let tableView = MockTableView() + let cell = UITableViewCell() + let dataSource = TableViewDiffableDataSource(tableView: tableView) { _, _, _ in + cell + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([0, 1, 2]) + snapshot.appendItems([0, 1, 2], toSection: 0) + dataSource.apply(snapshot) + + XCTAssertEqual( + dataSource.tableView(tableView, canEditRowAt: IndexPath(item: 1, section: 0)), + false + ) + } + + func testCanMoveRowAt() { + let tableView = MockTableView() + let cell = UITableViewCell() + let dataSource = TableViewDiffableDataSource(tableView: tableView) { _, _, _ in + cell + } + + var snapshot = DiffableDataSourceSnapshot() + snapshot.appendSections([0, 1, 2]) + snapshot.appendItems([0, 1, 2], toSection: 0) + dataSource.apply(snapshot) + + XCTAssertEqual( + dataSource.tableView(tableView, canMoveRowAt: IndexPath(item: 1, section: 0)), + false + ) + } } final class MockTableView: UITableView {