-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathUITableViewTests.swift
More file actions
158 lines (116 loc) · 5.14 KB
/
Copy pathUITableViewTests.swift
File metadata and controls
158 lines (116 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
//
// UITableViewTests.swift
// ExampleTests
//
// Created by Joan Disho on 08.07.20.
// Copyright © 2020 Combine Community. All rights reserved.
//
import XCTest
import Combine
@testable import CombineCocoa
class UITableViewTests: XCTestCase {
var subscriptions = Set<AnyCancellable>()
override func tearDown() {
subscriptions = .init()
}
func test_didSelectRowAt() {
let tableView = UITableView()
var resultIndexPath: IndexPath? = nil
tableView.didSelectRowPublisher
.sink(receiveValue: { resultIndexPath = $0 })
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
tableView.delegate!.tableView!(tableView, didSelectRowAt: givenIndexPath)
XCTAssertEqual(resultIndexPath, givenIndexPath)
}
func test_didDeselectRowAt() {
let tableView = UITableView()
var resultIndexPath: IndexPath? = nil
tableView.didDeselectRowPublisher
.sink(receiveValue: { resultIndexPath = $0 })
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
tableView.delegate!.tableView!(tableView, didDeselectRowAt: givenIndexPath)
XCTAssertEqual(resultIndexPath, givenIndexPath)
}
func test_willDisplayCell() {
let tableView = UITableView()
var resultIndexPath: IndexPath? = nil
var resultTableViewCell: UITableViewCell? = nil
tableView.willDisplayCellPublisher
.sink(receiveValue: { cell, indexPath in
resultTableViewCell = cell
resultIndexPath = indexPath
})
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
let givenTableViewCell = UITableViewCell()
tableView.delegate!.tableView!(tableView, willDisplay: givenTableViewCell, forRowAt: givenIndexPath)
XCTAssertEqual(resultIndexPath, givenIndexPath)
XCTAssertEqual(resultTableViewCell, givenTableViewCell)
}
func test_didEndDisplayingCell() {
let tableView = UITableView()
var resultIndexPath: IndexPath? = nil
var resultTableViewCell: UITableViewCell? = nil
tableView.didEndDisplayingCellPublisher
.sink(receiveValue: { cell, indexPath in
resultTableViewCell = cell
resultIndexPath = indexPath
})
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
let givenTableViewCell = UITableViewCell()
tableView.delegate!.tableView!(tableView, didEndDisplaying: givenTableViewCell, forRowAt: givenIndexPath)
XCTAssertEqual(resultIndexPath, givenIndexPath)
XCTAssertEqual(resultTableViewCell, givenTableViewCell)
}
func test_itemAccessoryButtonTapped() {
let tableView = UITableView()
var resultIndexPath: IndexPath? = nil
tableView.itemAccessoryButtonTappedPublisher
.sink(receiveValue: { resultIndexPath = $0 })
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
tableView.delegate!.tableView!(tableView, accessoryButtonTappedForRowWith: givenIndexPath)
XCTAssertEqual(resultIndexPath, givenIndexPath)
}
func test_didSelectRowAt_for_multiple_subscribers() {
let tableView = UITableView()
var firstResultIndexPaths = [IndexPath]()
var secondResultIndexPaths = [IndexPath]()
tableView.didSelectRowPublisher
.sink(receiveValue: { firstResultIndexPaths.append($0) })
.store(in: &subscriptions)
tableView.didSelectRowPublisher
.sink(receiveValue: { secondResultIndexPaths.append($0) })
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
tableView.delegate!.tableView!(tableView, didSelectRowAt: givenIndexPath)
XCTAssertEqual(firstResultIndexPaths, [givenIndexPath])
XCTAssertEqual(firstResultIndexPaths, secondResultIndexPaths)
}
func test_setDelegate() {
let tableView = UITableView()
let delegate = StubTableViewDelegate()
var resultIndexPath: IndexPath? = nil
tableView.didSelectRowPublisher
.sink(receiveValue: { resultIndexPath = $0 })
.store(in: &subscriptions)
tableView.setDelegate(delegate)
.store(in: &subscriptions)
let givenIndexPath = IndexPath(row: 1, section: 0)
tableView.delegate!.tableView!(tableView, didSelectRowAt: givenIndexPath)
let selector = #selector(UITableViewDelegate.tableView(_:heightForRowAt:))
let height = tableView.delegate!.tableView!(tableView, heightForRowAt: givenIndexPath)
XCTAssertTrue(tableView.delegate!.responds(to: selector))
XCTAssertEqual(resultIndexPath, givenIndexPath)
XCTAssertEqual(height, StubTableViewDelegate.height)
}
}
private class StubTableViewDelegate: NSObject, UITableViewDelegate {
static let height = 10.0
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
Self.height
}
}