Skip to content

Commit 0893c4e

Browse files
authored
Add tests for debug descriptions of CollectionViewModel and SectionViewModel (#120)
1 parent fd0ce43 commit 0893c4e

11 files changed

+900
-42
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ NEXT
1010
0.1.3
1111
-----
1212

13-
- Improve debug descriptions for `CollectionViewModel` and `SectionViewModel` ([@nuomi1](https://github.com/nuomi1), [#119](https://github.com/jessesquires/ReactiveCollectionsKit/pull/119))
13+
- Improve debug descriptions for `CollectionViewModel` and `SectionViewModel` ([@nuomi1](https://github.com/nuomi1), [#119](https://github.com/jessesquires/ReactiveCollectionsKit/pull/119), [#120](https://github.com/jessesquires/ReactiveCollectionsKit/pull/120))
1414

1515
0.1.2
1616
-----

ReactiveCollectionsKit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
07C025E42C535FE7009AF8AD /* TestDebugDescriptionCollection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07C025E32C535FE6009AF8AD /* TestDebugDescriptionCollection.swift */; };
1011
07E72F9C2C3ECE86009B2387 /* DebugDescriptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 07E72F9B2C3ECE86009B2387 /* DebugDescriptions.swift */; };
1112
0B01476F2BBCF0330041D432 /* Collection+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B01476E2BBCF0330041D432 /* Collection+Extensions.swift */; };
1213
0B0433262C2FE25C0083C8DB /* TestDiffableSnapshot.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B0433252C2FE25C0083C8DB /* TestDiffableSnapshot.swift */; };
1314
0B0B5AA32BFF0BDB0037BC02 /* EmptyViewProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B0B5AA22BFF0BDB0037BC02 /* EmptyViewProvider.swift */; };
1415
0B25D73A2BFAD31C00EB3664 /* UICollectionView+Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B25D7392BFAD31C00EB3664 /* UICollectionView+Extensions.swift */; };
1516
0B31B9312BCC5B66006F2078 /* CellEventCoordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B31B9302BCC5B66006F2078 /* CellEventCoordinator.swift */; };
1617
0B4245DA2C014F4B00DBEC2F /* CollectionViewDriverOptions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B4245D92C014F4B00DBEC2F /* CollectionViewDriverOptions.swift */; };
18+
0B4263E12C54409F006FE56D /* TestDebugDescriptionSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B4263E02C54409F006FE56D /* TestDebugDescriptionSection.swift */; };
1719
0B7A18EC2C2A17A700E59C35 /* TestCollectionViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B7A18EB2C2A17A700E59C35 /* TestCollectionViewModel.swift */; };
1820
0B7A18EF2C2A4DE300E59C35 /* FakeSupplementaryNib.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0B7A18EE2C2A4DE300E59C35 /* FakeSupplementaryNib.xib */; };
1921
0B7A18F12C2A4E1A00E59C35 /* FakeCellNibView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B7A18F02C2A4E1A00E59C35 /* FakeCellNibView.swift */; };
@@ -75,13 +77,15 @@
7577
/* End PBXContainerItemProxy section */
7678

7779
/* Begin PBXFileReference section */
80+
07C025E32C535FE6009AF8AD /* TestDebugDescriptionCollection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDebugDescriptionCollection.swift; sourceTree = "<group>"; };
7881
07E72F9B2C3ECE86009B2387 /* DebugDescriptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DebugDescriptions.swift; sourceTree = "<group>"; };
7982
0B01476E2BBCF0330041D432 /* Collection+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Collection+Extensions.swift"; sourceTree = "<group>"; };
8083
0B0433252C2FE25C0083C8DB /* TestDiffableSnapshot.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDiffableSnapshot.swift; sourceTree = "<group>"; };
8184
0B0B5AA22BFF0BDB0037BC02 /* EmptyViewProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyViewProvider.swift; sourceTree = "<group>"; };
8285
0B25D7392BFAD31C00EB3664 /* UICollectionView+Extensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UICollectionView+Extensions.swift"; sourceTree = "<group>"; };
8386
0B31B9302BCC5B66006F2078 /* CellEventCoordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CellEventCoordinator.swift; sourceTree = "<group>"; };
8487
0B4245D92C014F4B00DBEC2F /* CollectionViewDriverOptions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewDriverOptions.swift; sourceTree = "<group>"; };
88+
0B4263E02C54409F006FE56D /* TestDebugDescriptionSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestDebugDescriptionSection.swift; sourceTree = "<group>"; };
8589
0B7A18EB2C2A17A700E59C35 /* TestCollectionViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TestCollectionViewModel.swift; sourceTree = "<group>"; };
8690
0B7A18EE2C2A4DE300E59C35 /* FakeSupplementaryNib.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = FakeSupplementaryNib.xib; sourceTree = "<group>"; };
8791
0B7A18F02C2A4E1A00E59C35 /* FakeCellNibView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FakeCellNibView.swift; sourceTree = "<group>"; };
@@ -251,6 +255,8 @@
251255
0BA0E2CB2C030445001FB5AA /* TestCollectionViewDriverOptions.swift */,
252256
0BBB8A482C32309800200477 /* TestCollectionViewDriverReconfigure.swift */,
253257
0B7A18EB2C2A17A700E59C35 /* TestCollectionViewModel.swift */,
258+
07C025E32C535FE6009AF8AD /* TestDebugDescriptionCollection.swift */,
259+
0B4263E02C54409F006FE56D /* TestDebugDescriptionSection.swift */,
254260
0B0433252C2FE25C0083C8DB /* TestDiffableSnapshot.swift */,
255261
0BA0E2CD2C0304B4001FB5AA /* TestEmptyView.swift */,
256262
0BFBB2F62C0D4A91000842B6 /* TestPlans */,
@@ -437,13 +443,15 @@
437443
0BFBB2EE2C0C3C36000842B6 /* TestCellEventCoordinator.swift in Sources */,
438444
0BFBB2EA2C0BF8B8000842B6 /* FakeTextModel.swift in Sources */,
439445
88BD6D16238D03F100FB1846 /* TestCellViewModel.swift in Sources */,
446+
0B4263E12C54409F006FE56D /* TestDebugDescriptionSection.swift in Sources */,
440447
0BA0E2D02C03056D001FB5AA /* TestViewRegistration.swift in Sources */,
441448
0BFBB2FB2C0D4C34000842B6 /* FakeSupplementaryViewModel.swift in Sources */,
442449
0BA0E2D22C030849001FB5AA /* TestViewRegistrationMethod.swift in Sources */,
443450
0B8F50452C2B46A20081C0D2 /* TestBundle.swift in Sources */,
444451
0BA0E2CC2C030445001FB5AA /* TestCollectionViewDriverOptions.swift in Sources */,
445452
0BBB8A492C32309800200477 /* TestCollectionViewDriverReconfigure.swift in Sources */,
446453
0BFBB2EC2C0BFD1B000842B6 /* FakeCellEventCoordinator.swift in Sources */,
454+
07C025E42C535FE7009AF8AD /* TestDebugDescriptionCollection.swift in Sources */,
447455
0BFA774C2BB8B717009B9C5B /* FakeCellViewModel.swift in Sources */,
448456
0B7A18F32C2A4E4500E59C35 /* FakeSupplementaryNibView.swift in Sources */,
449457
0BFBB2F02C0D3C92000842B6 /* XCTestExpectation+Expectations.swift in Sources */,

Tests/Fakes/FakeCellNibView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ final class FakeCellNibView: UICollectionViewCell {
2121
}
2222

2323
struct FakeCellNibViewModel: CellViewModel {
24-
let id: UniqueIdentifier = String.random
24+
let id: UniqueIdentifier
2525

2626
var registration: ViewRegistration {
2727
ViewRegistration(

Tests/Fakes/FakeCollectionViewModel.swift

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,29 @@ extension XCTestCase {
2020
@MainActor
2121
func fakeCollectionViewModel(
2222
id: String = .random,
23+
sectionId: (Int) -> String = { _ in .random },
24+
cellId: (Int, Int) -> String = { _, _ in .random },
25+
supplementaryViewId: (Int, Int) -> String = { _, _ in .random },
2326
numSections: Int = Int.random(in: 2...15),
2427
numCells: Int? = nil,
2528
useCellNibs: Bool = false,
29+
includeHeader: Bool = false,
30+
includeFooter: Bool = false,
31+
includeSupplementaryViews: Bool = false,
2632
expectDidSelectCell: Bool = false,
2733
expectConfigureCell: Bool = false
2834
) -> CollectionViewModel {
29-
let sections = (0..<numSections).map { _ in
35+
let sections = (0..<numSections).map { sectionIndex in
3036
self.fakeSectionViewModel(
37+
id: sectionId(sectionIndex),
38+
cellId: cellId,
39+
supplementaryViewId: supplementaryViewId,
40+
sectionIndex: sectionIndex,
3141
numCells: numCells ?? Int.random(in: 3...20),
3242
useCellNibs: useCellNibs,
43+
includeHeader: includeHeader,
44+
includeFooter: includeFooter,
45+
includeSupplementaryViews: includeSupplementaryViews,
3346
expectDidSelectCell: expectDidSelectCell,
3447
expectConfigureCell: expectConfigureCell
3548
)
@@ -40,40 +53,53 @@ extension XCTestCase {
4053
@MainActor
4154
func fakeSectionViewModel(
4255
id: String = .random,
56+
cellId: (Int, Int) -> String = { _, _ in .random },
57+
supplementaryViewId: (Int, Int) -> String = { _, _ in .random },
58+
sectionIndex: Int = 0,
4359
numCells: Int = Int.random(in: 1...20),
4460
useCellNibs: Bool = false,
4561
includeHeader: Bool = false,
4662
includeFooter: Bool = false,
63+
includeSupplementaryViews: Bool = false,
4764
expectDidSelectCell: Bool = false,
4865
expectConfigureCell: Bool = false
4966
) -> SectionViewModel {
5067
let cells = self.fakeCellViewModels(
68+
id: cellId,
69+
sectionIndex: sectionIndex,
5170
count: numCells,
5271
useNibs: useCellNibs,
5372
expectDidSelectCell: expectDidSelectCell,
5473
expectConfigureCell: expectConfigureCell
5574
)
5675
let header = includeHeader ? FakeHeaderViewModel() : nil
5776
let footer = includeFooter ? FakeFooterViewModel() : nil
77+
let supplementaryViews = includeSupplementaryViews
78+
? (0..<numCells).map { cellIndex in FakeSupplementaryViewModel(title: supplementaryViewId(sectionIndex, cellIndex)) }
79+
: []
5880
return SectionViewModel(
5981
id: "section_\(id)",
6082
cells: cells,
6183
header: header,
62-
footer: footer
84+
footer: footer,
85+
supplementaryViews: supplementaryViews
6386
)
6487
}
6588

6689
@MainActor
6790
func fakeCellViewModels(
91+
id: (Int, Int) -> String = { _, _ in .random },
92+
sectionIndex: Int = 0,
6893
count: Int = Int.random(in: 3...20),
6994
useNibs: Bool = false,
7095
expectDidSelectCell: Bool = false,
7196
expectConfigureCell: Bool = false
7297
) -> [AnyCellViewModel] {
7398
var cells = [AnyCellViewModel]()
74-
for index in 0..<count {
99+
for cellIndex in 0..<count {
75100
let model = self._fakeCellViewModel(
76-
index: index,
101+
id: id(sectionIndex, cellIndex),
102+
cellIndex: cellIndex,
77103
useNibs: useNibs,
78104
expectDidSelectCell: expectDidSelectCell,
79105
expectConfigureCell: expectConfigureCell
@@ -85,26 +111,27 @@ extension XCTestCase {
85111

86112
@MainActor
87113
private func _fakeCellViewModel(
88-
index: Int,
114+
id: String,
115+
cellIndex: Int,
89116
useNibs: Bool,
90117
expectDidSelectCell: Bool,
91118
expectConfigureCell: Bool
92119
) -> AnyCellViewModel {
93120
if useNibs {
94-
var viewModel = FakeCellNibViewModel()
121+
var viewModel = FakeCellNibViewModel(id: id)
95122
viewModel.expectationDidSelect = self._cellDidSelectExpectation(expect: expectDidSelectCell, id: viewModel.id)
96123
viewModel.expectationConfigureCell = self._cellConfigureExpectation(expect: expectConfigureCell, id: viewModel.id)
97124
return viewModel.eraseToAnyViewModel()
98125
}
99126

100-
if index.isMultiple(of: 2) {
101-
var viewModel = FakeNumberCellViewModel()
127+
if cellIndex.isMultiple(of: 2) {
128+
var viewModel = FakeNumberCellViewModel(model: .init(id: id))
102129
viewModel.expectationDidSelect = self._cellDidSelectExpectation(expect: expectDidSelectCell, id: viewModel.id)
103130
viewModel.expectationConfigureCell = self._cellConfigureExpectation(expect: expectConfigureCell, id: viewModel.id)
104131
return viewModel.eraseToAnyViewModel()
105132
}
106133

107-
var viewModel = FakeTextCellViewModel()
134+
var viewModel = FakeTextCellViewModel(model: .init(text: id))
108135
viewModel.expectationDidSelect = self._cellDidSelectExpectation(expect: expectDidSelectCell, id: viewModel.id)
109136
viewModel.expectationConfigureCell = self._cellConfigureExpectation(expect: expectConfigureCell, id: viewModel.id)
110137
return viewModel.eraseToAnyViewModel()

Tests/Fakes/FakeNumberModel.swift

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ import XCTest
1818

1919
struct FakeNumberModel: Hashable {
2020
let number = Int.random(in: 0...1_000_000)
21-
let id = String.random
21+
let id: String
22+
23+
init(id: String = .random) {
24+
self.id = id
25+
}
2226
}
2327

2428
struct FakeNumberCellViewModel: CellViewModel {
25-
let model = FakeNumberModel()
29+
let model: FakeNumberModel
2630

2731
nonisolated var id: UniqueIdentifier {
2832
self.model.id
@@ -42,6 +46,10 @@ struct FakeNumberCellViewModel: CellViewModel {
4246
self.expectationDidSelect?.fulfillAndLog()
4347
}
4448

49+
init(model: FakeNumberModel = FakeNumberModel()) {
50+
self.model = model
51+
}
52+
4553
nonisolated static func == (left: Self, right: Self) -> Bool {
4654
left.model == right.model
4755
}

Tests/Fakes/FakeSupplementaryViewModel.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import XCTest
1818
struct FakeSupplementaryViewModel: SupplementaryViewModel {
1919
static let kind = "FakeKind"
2020

21-
let title = String.random
21+
let title: String
2222

2323
nonisolated var id: UniqueIdentifier { self.title }
2424

@@ -35,6 +35,10 @@ struct FakeSupplementaryViewModel: SupplementaryViewModel {
3535
self.expectationConfigureView?.fulfillAndLog()
3636
}
3737

38+
init(title: String = .random) {
39+
self.title = title
40+
}
41+
3842
nonisolated static func == (left: Self, right: Self) -> Bool {
3943
left.title == right.title
4044
}
@@ -47,7 +51,7 @@ struct FakeSupplementaryViewModel: SupplementaryViewModel {
4751
final class FakeSupplementaryView: UICollectionViewCell { }
4852

4953
struct FakeHeaderViewModel: SupplementaryHeaderViewModel {
50-
let title = String.random
54+
let title: String
5155

5256
nonisolated var id: UniqueIdentifier { "Header" }
5357

@@ -56,6 +60,14 @@ struct FakeHeaderViewModel: SupplementaryHeaderViewModel {
5660
self.expectationConfigureView?.fulfillAndLog()
5761
}
5862

63+
init(
64+
title: String = .random,
65+
expectationConfigureView: XCTestExpectation? = nil
66+
) {
67+
self.title = title
68+
self.expectationConfigureView = expectationConfigureView
69+
}
70+
5971
nonisolated static func == (left: Self, right: Self) -> Bool {
6072
left.title == right.title
6173
}
@@ -68,7 +80,7 @@ struct FakeHeaderViewModel: SupplementaryHeaderViewModel {
6880
final class FakeCollectionHeaderView: UICollectionReusableView { }
6981

7082
struct FakeFooterViewModel: SupplementaryFooterViewModel {
71-
let title = String.random
83+
let title: String
7284

7385
nonisolated var id: UniqueIdentifier { "Footer" }
7486

@@ -77,6 +89,14 @@ struct FakeFooterViewModel: SupplementaryFooterViewModel {
7789
self.expectationConfigureView?.fulfillAndLog()
7890
}
7991

92+
init(
93+
title: String = .random,
94+
expectationConfigureView: XCTestExpectation? = nil
95+
) {
96+
self.title = title
97+
self.expectationConfigureView = expectationConfigureView
98+
}
99+
80100
nonisolated static func == (left: Self, right: Self) -> Bool {
81101
left.title == right.title
82102
}

Tests/Fakes/FakeTextModel.swift

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,15 @@ import UIKit
1717
import XCTest
1818

1919
struct FakeTextModel: Hashable {
20-
let text = String.random
20+
let text: String
21+
22+
init(text: String = .random) {
23+
self.text = text
24+
}
2125
}
2226

2327
struct FakeTextCellViewModel: CellViewModel {
24-
let model = FakeTextModel()
28+
let model: FakeTextModel
2529

2630
nonisolated var id: UniqueIdentifier {
2731
self.model.text
@@ -41,6 +45,20 @@ struct FakeTextCellViewModel: CellViewModel {
4145
self.expectationDidSelect?.fulfillAndLog()
4246
}
4347

48+
init(
49+
model: FakeTextModel = FakeTextModel(),
50+
shouldHighlight: Bool = true,
51+
contextMenuConfiguration: UIContextMenuConfiguration? = nil,
52+
expectationConfigureCell: XCTestExpectation? = nil,
53+
expectationDidSelect: XCTestExpectation? = nil
54+
) {
55+
self.model = model
56+
self.shouldHighlight = shouldHighlight
57+
self.contextMenuConfiguration = contextMenuConfiguration
58+
self.expectationConfigureCell = expectationConfigureCell
59+
self.expectationDidSelect = expectationDidSelect
60+
}
61+
4462
nonisolated static func == (left: Self, right: Self) -> Bool {
4563
left.model == right.model
4664
}

Tests/TestCollectionViewModel.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -255,10 +255,4 @@ final class TestCollectionViewModel: XCTestCase {
255255
XCTAssertEqual(model[.init(item: 1, section: 0)], cell2)
256256
XCTAssertEqual(model[item: 2, section: 0], cell3)
257257
}
258-
259-
@MainActor
260-
func test_debugDescription() {
261-
let viewModel = self.fakeCollectionViewModel()
262-
print(viewModel.debugDescription)
263-
}
264258
}

0 commit comments

Comments
 (0)