Skip to content

Commit 4646077

Browse files
committed
Add tests for newly added methods and update the implementation of the CollectionViewDiffableDataSource with canMove/moveItemAt methods
1 parent 7152ec8 commit 4646077

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

Sources/UIKit/CollectionViewDiffableDataSource.swift

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,29 @@ open class CollectionViewDiffableDataSource<SectionIdentifierType: Hashable, Ite
132132

133133
return view
134134
}
135+
136+
/// Returns whether it is possible to edit a row at given index path.
137+
///
138+
/// - Parameters:
139+
/// - collectionView: A collection view instance managed by `self`.
140+
/// - section: An index of section.
141+
///
142+
/// - Returns: A boolean for row at specified index path.
143+
open func collectionView(_ collectionView: UICollectionView, canMoveItemAt indexPath: IndexPath) -> Bool {
144+
return false
145+
}
146+
147+
/// Moves a row at given index path.
148+
///
149+
/// - Parameters:
150+
/// - collectionView: A collection view instance managed by `self`.
151+
/// - sourceIndexPath: An index path for given cell position.
152+
/// - destinationIndexPath: An index path for target cell position.
153+
///
154+
/// - Returns: Void.
155+
public func collectionView(_ collectionView: UICollectionView, moveItemAt sourceIndexPath: IndexPath, to destinationIndexPath: IndexPath) {
156+
// Empty implementation.
157+
}
135158
}
136159

137160
#endif

Tests/CollectionViewDiffableDataSourceTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,24 @@ final class CollectionViewDiffableDataSourceTests: XCTestCase {
167167
cell
168168
)
169169
}
170+
171+
func testCanMoveRowAt() {
172+
let collectionView = MockCollectionView()
173+
let cell = UICollectionViewCell()
174+
let dataSource = CollectionViewDiffableDataSource<Int, Int>(collectionView: collectionView) { _, _, _ in
175+
cell
176+
}
177+
178+
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
179+
snapshot.appendSections([0, 1, 2])
180+
snapshot.appendItems([0, 1, 2], toSection: 0)
181+
dataSource.apply(snapshot)
182+
183+
XCTAssertEqual(
184+
dataSource.collectionView(collectionView, canMoveItemAt: IndexPath(item: 1, section: 0)),
185+
false
186+
)
187+
}
170188
}
171189

172190
final class MockCollectionView: UICollectionView {

Tests/TableViewDiffableDataSourceTests.swift

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,42 @@ final class TableViewDiffableDataSourceTests: XCTestCase {
167167
cell
168168
)
169169
}
170+
171+
func testCanEditRowAt() {
172+
let tableView = MockTableView()
173+
let cell = UITableViewCell()
174+
let dataSource = TableViewDiffableDataSource<Int, Int>(tableView: tableView) { _, _, _ in
175+
cell
176+
}
177+
178+
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
179+
snapshot.appendSections([0, 1, 2])
180+
snapshot.appendItems([0, 1, 2], toSection: 0)
181+
dataSource.apply(snapshot)
182+
183+
XCTAssertEqual(
184+
dataSource.tableView(tableView, canEditRowAt: IndexPath(item: 1, section: 0)),
185+
false
186+
)
187+
}
188+
189+
func testCanMoveRowAt() {
190+
let tableView = MockTableView()
191+
let cell = UITableViewCell()
192+
let dataSource = TableViewDiffableDataSource<Int, Int>(tableView: tableView) { _, _, _ in
193+
cell
194+
}
195+
196+
var snapshot = DiffableDataSourceSnapshot<Int, Int>()
197+
snapshot.appendSections([0, 1, 2])
198+
snapshot.appendItems([0, 1, 2], toSection: 0)
199+
dataSource.apply(snapshot)
200+
201+
XCTAssertEqual(
202+
dataSource.tableView(tableView, canMoveRowAt: IndexPath(item: 1, section: 0)),
203+
false
204+
)
205+
}
170206
}
171207

172208
final class MockTableView: UITableView {

0 commit comments

Comments
 (0)