Skip to content

Commit

Permalink
Add collection test
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Juarez committed Jul 5, 2023
1 parent c42358c commit 88694b8
Show file tree
Hide file tree
Showing 2 changed files with 109 additions and 2 deletions.
10 changes: 8 additions & 2 deletions ReactiveLists.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
357B96E9201956760000443F /* CollectionViewHeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 357B96E8201956760000443F /* CollectionViewHeaderView.xib */; };
357B96EA2019599C0000443F /* CollectionViewHeaderView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 357B96E8201956760000443F /* CollectionViewHeaderView.xib */; };
39A211882A5342EE00288547 /* CollectionCellViewModelDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39A211872A5342EE00288547 /* CollectionCellViewModelDataSource.swift */; };
39D64C592A54E4DB0071A7F3 /* CollectionViewLazyDiffingTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 39D64C582A54E4DB0071A7F3 /* CollectionViewLazyDiffingTests.swift */; };
7C24B1408A8B3A147C254BCA /* Pods_ReactiveLists.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 978763204EC113AFD1F7EB54 /* Pods_ReactiveLists.framework */; };
A8069332250046AD0036CA11 /* TableViewLazyDiffingTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8069330250042700036CA11 /* TableViewLazyDiffingTest.swift */; };
A8D93CAA24FD9BDF00459EBB /* TableCellViewModelDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = A8D93CA924FD9BDF00459EBB /* TableCellViewModelDataSource.swift */; };
Expand Down Expand Up @@ -134,6 +135,7 @@
357B96D9201934C50000443F /* CollectionToolCell.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CollectionToolCell.xib; sourceTree = "<group>"; };
357B96E8201956760000443F /* CollectionViewHeaderView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = CollectionViewHeaderView.xib; sourceTree = "<group>"; };
39A211872A5342EE00288547 /* CollectionCellViewModelDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionCellViewModelDataSource.swift; sourceTree = "<group>"; };
39D64C582A54E4DB0071A7F3 /* CollectionViewLazyDiffingTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewLazyDiffingTests.swift; sourceTree = "<group>"; };
42A9724B45F9E14DD1B210D1 /* Pods-ReactiveLists.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReactiveLists.release.xcconfig"; path = "Pods/Target Support Files/Pods-ReactiveLists/Pods-ReactiveLists.release.xcconfig"; sourceTree = "<group>"; };
45132CB591F9F96746AF8E96 /* Pods-ReactiveListsTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-ReactiveListsTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-ReactiveListsTests/Pods-ReactiveListsTests.debug.xcconfig"; sourceTree = "<group>"; };
932473A2DAECE0F923C4B570 /* Pods_ReactiveListsTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_ReactiveListsTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -228,6 +230,7 @@
257A97DB2017AA3500164403 /* CollectionViewMocks.swift */,
257A97CA2017A80B00164403 /* CollectionViewModelTests.swift */,
257A97BC2017A5AA00164403 /* TestCollectionViewModels.swift */,
39D64C582A54E4DB0071A7F3 /* CollectionViewLazyDiffingTests.swift */,
);
path = CollectionView;
sourceTree = "<group>";
Expand Down Expand Up @@ -636,6 +639,7 @@
257A97D82017A82F00164403 /* CollectionViewDriverTests.swift in Sources */,
257A97C02017A5D300164403 /* TestCollectionViewModels.swift in Sources */,
257A97D92017A82F00164403 /* CollectionViewModelTests.swift in Sources */,
39D64C592A54E4DB0071A7F3 /* CollectionViewLazyDiffingTests.swift in Sources */,
25B1B0B920195F1C0036545F /* CollectionViewDriverDiffingTests.swift in Sources */,
257A97DA2017A83400164403 /* XCTest+Parameterized.swift in Sources */,
257A97D42017A82900164403 /* TableViewMocks.swift in Sources */,
Expand Down Expand Up @@ -886,7 +890,8 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Tests/Info.plist;
INFOPLIST_FILE = "$(SRCROOT)/Tests/Info.plist";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.plangrid.ReactiveListsTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -903,7 +908,8 @@
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "";
CODE_SIGN_STYLE = Manual;
DEVELOPMENT_TEAM = "";
INFOPLIST_FILE = Tests/Info.plist;
INFOPLIST_FILE = "$(SRCROOT)/Tests/Info.plist";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks";
PRODUCT_BUNDLE_IDENTIFIER = com.plangrid.ReactiveListsTests;
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
101 changes: 101 additions & 0 deletions Tests/CollectionView/CollectionViewLazyDiffingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//
// PlanGrid
// https://www.plangrid.com
// https://medium.com/plangrid-technology
//
// Documentation
// https://plangrid.github.io/ReactiveLists
//
// GitHub
// https://github.com/plangrid/ReactiveLists
//
// License
// Copyright © 2018-present PlanGrid, Inc.
// Released under an MIT license: https://opensource.org/licenses/MIT
//

@testable import ReactiveLists
import XCTest

final class CollectionViewDiffingTests: XCTestCase {

var collectionViewDataSource: CollectionViewDriver!
var mockCollectionView: TestCollectionView!

override func setUp() {
super.setUp()
self.mockCollectionView = TestCollectionView(
frame: .zero,
collectionViewLayout: UICollectionViewFlowLayout()
)
// self.mockCollectionView.indexPathsForVisibleRowsOverride = [
// IndexPath(row: 0, section: 0),
// ]
self.collectionViewDataSource = CollectionViewDriver(
collectionView: self.mockCollectionView,
shouldDeselectUponSelection: false,
useDataSource: true,
automaticDiffingEnabled: true
)
}

/// Tests that changes to individual rows result in the correct calls to update the
/// table view.
///
/// - Note: We're only testing one type of row update since this is sufficient to test the
/// communication between the diffing lib and the table view. The diffing lib itself has
/// extensive tests for the various diffing scenarios.
func testChangingRows() {
let userCells = [LazyTestUserCell(user: "Name")]
let dataSource = CollectionCellViewModelDataSource(userCells)
let section = CollectionSectionViewModel(
diffingKey: "default_section",
cellViewModels: [],
cellViewModelDataSource: dataSource
)
let initialModel = CollectionViewModel(
sectionModels: [section]
)

self.collectionViewDataSource.collectionViewModel = initialModel

let testUser1 = [LazyTestUserCell(user: "TestUser1")]
// [LazyUserCell(user: "TestUser1"), LazyUserCell(user: "TestUser2")]
// let testUser2 = LazyUserCell(user: "TestUser2")
let dataSource1 = CollectionCellViewModelDataSource(testUser1)
let section2 = CollectionSectionViewModel(
diffingKey: "default_section",
cellViewModels: [],
cellViewModelDataSource: dataSource1
)
let updatedModel = CollectionViewModel(
sectionModels: [section2]
)

self.collectionViewDataSource.collectionViewModel = updatedModel

XCTAssertEqual(self.mockCollectionView.callsToInsertItems.count, 1)
XCTAssertEqual(self.mockCollectionView.callsToReloadData, 2)
}
}

final class LazyTestUserCell: CollectionCellViewModel, DiffableViewModel {
var accessibilityFormat: CellAccessibilityFormat = ""
let registrationInfo = ViewRegistrationInfo(classType: UICollectionViewCell.self)

let user: String
private(set) var diffingKeyAccessed: Bool = false

init(user: String) {
self.user = user
}

func applyViewModelToCell(_ cell: UICollectionViewCell) {}

func willDisplay(cell: UICollectionViewCell) {}

var diffingKey: String {
self.diffingKeyAccessed = true
return self.user
}
}

0 comments on commit 88694b8

Please sign in to comment.