-
Notifications
You must be signed in to change notification settings - Fork 492
Expand file tree
/
Copy pathFeedUIIntegrationTests+LoaderSpy.swift
More file actions
94 lines (71 loc) · 2.76 KB
/
FeedUIIntegrationTests+LoaderSpy.swift
File metadata and controls
94 lines (71 loc) · 2.76 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
//
// Copyright © Essential Developer. All rights reserved.
//
import Foundation
import EssentialFeed
import EssentialFeediOS
import Combine
extension FeedUIIntegrationTests {
@MainActor
class LoaderSpy {
// MARK: - FeedLoader
private var feedRequests = [PassthroughSubject<Paginated<FeedImage>, Error>]()
var loadFeedCallCount: Int {
return feedRequests.count
}
func loadPublisher() -> AnyPublisher<Paginated<FeedImage>, Error> {
let publisher = PassthroughSubject<Paginated<FeedImage>, Error>()
feedRequests.append(publisher)
return publisher.eraseToAnyPublisher()
}
func completeFeedLoadingWithError(at index: Int = 0) {
feedRequests[index].send(completion: .failure(anyNSError()))
}
func completeFeedLoading(with feed: [FeedImage] = [], at index: Int = 0) {
feedRequests[index].send(Paginated(items: feed, loadMorePublisher: { [weak self] in
self?.loadMorePublisher() ?? Empty().eraseToAnyPublisher()
}))
feedRequests[index].send(completion: .finished)
}
// MARK: - LoadMoreFeedLoader
private var loadMoreRequests = [PassthroughSubject<Paginated<FeedImage>, Error>]()
var loadMoreCallCount: Int {
return loadMoreRequests.count
}
func loadMorePublisher() -> AnyPublisher<Paginated<FeedImage>, Error> {
let publisher = PassthroughSubject<Paginated<FeedImage>, Error>()
loadMoreRequests.append(publisher)
return publisher.eraseToAnyPublisher()
}
func completeLoadMore(with feed: [FeedImage] = [], lastPage: Bool = false, at index: Int = 0) {
loadMoreRequests[index].send(Paginated(
items: feed,
loadMorePublisher: lastPage ? nil : { [weak self] in
self?.loadMorePublisher() ?? Empty().eraseToAnyPublisher()
}))
}
func completeLoadMoreWithError(at index: Int = 0) {
loadMoreRequests[index].send(completion: .failure(anyNSError()))
}
// MARK: - FeedImageDataLoader
private var imageRequests = [(url: URL, publisher: PassthroughSubject<Data, Error>)]()
var loadedImageURLs: [URL] {
return imageRequests.map { $0.url }
}
private(set) var cancelledImageURLs = [URL]()
func loadImageDataPublisher(from url: URL) -> AnyPublisher<Data, Error> {
let publisher = PassthroughSubject<Data, Error>()
imageRequests.append((url, publisher))
return publisher.handleEvents(receiveCancel: { [weak self] in
self?.cancelledImageURLs.append(url)
}).eraseToAnyPublisher()
}
func completeImageLoading(with imageData: Data = Data(), at index: Int = 0) {
imageRequests[index].publisher.send(imageData)
imageRequests[index].publisher.send(completion: .finished)
}
func completeImageLoadingWithError(at index: Int = 0) {
imageRequests[index].publisher.send(completion: .failure(anyNSError()))
}
}
}