Skip to content

Commit ac4207e

Browse files
author
Christian Elies
committed
test(): implemented more remote image service tests
1 parent 05a70dd commit ac4207e

File tree

1 file changed

+62
-1
lines changed

1 file changed

+62
-1
lines changed

Tests/RemoteImageTests/Services/RemoteImageServiceTests.swift

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,40 @@ final class RemoteImageServiceTests: XCTestCase {
9595
}
9696
}
9797

98+
func testFetchImageURLFailureCompletion() {
99+
guard let url = URL(string: "https://www.google.de") else {
100+
XCTFail("Could not create mock URL")
101+
return
102+
}
103+
104+
let expectation = self.expectation(description: "FetchImageURLState")
105+
106+
let remoteImageType: RemoteImageType = .url(url)
107+
service.fetchImage(ofType: remoteImageType)
108+
109+
// publish completion
110+
let expectedError = URLError(.cancelled)
111+
remoteImageURLDataPublisher?.publisher.send(completion: .failure(expectedError))
112+
113+
var state: RemoteImageState?
114+
cancellable = service.$state.sink { st in
115+
guard case RemoteImageState.error = st else {
116+
return
117+
}
118+
state = st
119+
expectation.fulfill()
120+
}
121+
122+
waitForExpectations(timeout: 1)
123+
124+
switch state {
125+
case .error(let error):
126+
XCTAssertEqual(error as? URLError, expectedError)
127+
default:
128+
XCTFail("Invalid fetch image URL completion")
129+
}
130+
}
131+
98132
func testFetchImageURLCached() {
99133
guard let url = URL(string: "https://www.google.de") else {
100134
XCTFail("Could not create mock URL")
@@ -163,7 +197,7 @@ final class RemoteImageServiceTests: XCTestCase {
163197
}
164198
}
165199

166-
func testFetchPHAccessFailure() {
200+
func testFetchPHAccessInvalidData() {
167201
let expectation = self.expectation(description: "FetchPHAsset")
168202
photoKitService?.resultToReturn = .success(Data())
169203
let localIdentifier = "TestIdentifier"
@@ -189,6 +223,33 @@ final class RemoteImageServiceTests: XCTestCase {
189223
}
190224
}
191225

226+
func testFetchPHAccessFailure() {
227+
let expectation = self.expectation(description: "FetchPHAsset")
228+
let expectedError: RemoteImageServiceError = .couldNotCreateImage
229+
photoKitService?.resultToReturn = .failure(expectedError)
230+
let localIdentifier = "TestIdentifier"
231+
let remoteImageType: RemoteImageType = .phAsset(localIdentifier: localIdentifier)
232+
service.fetchImage(ofType: remoteImageType)
233+
234+
var state: RemoteImageState?
235+
cancellable = service.$state.sink { st in
236+
guard case RemoteImageState.error = st else {
237+
return
238+
}
239+
state = st
240+
expectation.fulfill()
241+
}
242+
243+
waitForExpectations(timeout: 1)
244+
245+
switch state {
246+
case .error(let error):
247+
XCTAssertEqual(error as? RemoteImageServiceError, expectedError)
248+
default:
249+
XCTFail("Invalid fetch ph asset result")
250+
}
251+
}
252+
192253
func testFetchPHAssetCached() {
193254
guard let image = PlatformSpecificImageType(systemName: "paperplane.fill") else {
194255
XCTFail("Could not create mock image")

0 commit comments

Comments
 (0)