Skip to content

Commit

Permalink
test: Fix test to adopt new handleApiResponse signature
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippeWeidmann committed Feb 15, 2024
1 parent ab305a8 commit e1e7588
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions Tests/InfomaniakCoreTests/UTDecodeApiResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,23 @@
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

@testable import Alamofire
@testable import InfomaniakCore
import XCTest

@available(iOS 13.0, *)
final class UTDecodeApiResponse: XCTestCase {
func fakeDataResponse<T: Decodable>(decodedResponse: ApiResponse<T>) -> DataResponse<ApiResponse<T>, AFError> {
DataResponse(
request: nil,
response: HTTPURLResponse(),
data: nil,
metrics: nil,
serializationDuration: 0,
result: .success(decodedResponse)
)
}

func testDecodeNullDataResponse() throws {
// GIVEN
let apiFetcher = ApiFetcher()
Expand All @@ -33,12 +45,12 @@ final class UTDecodeApiResponse: XCTestCase {

// WHEN
let decodedResponse = try? JSONDecoder().decode(ApiResponse<NullableResponse>.self, from: jsonData)
let dataResponse = fakeDataResponse(decodedResponse: decodedResponse!)

// THEN
XCTAssertNotNil(decodedResponse, "Response shouldn't be nil")
XCTAssertNoThrow(
try apiFetcher.handleApiResponse(decodedResponse!, responseStatusCode: 0),
"handleApiResponse shouldn't throw"
try apiFetcher.handleApiResponse(dataResponse), "handleApiResponse shouldn't throw"
)
}

Expand All @@ -54,13 +66,11 @@ final class UTDecodeApiResponse: XCTestCase {

// WHEN
let decodedResponse = try? JSONDecoder().decode(ApiResponse<Int>.self, from: jsonData)
let dataResponse = fakeDataResponse(decodedResponse: decodedResponse!)

// THEN
XCTAssertNotNil(decodedResponse, "Response shouldn't be nil")
XCTAssertNoThrow(
try apiFetcher.handleApiResponse(decodedResponse!, responseStatusCode: 0),
"handleApiResponse shouldn't throw"
)
XCTAssertNoThrow(try apiFetcher.handleApiResponse(dataResponse), "handleApiResponse shouldn't throw")
}

func testDecodeErrorNullApiResponse() throws {
Expand All @@ -74,11 +84,12 @@ final class UTDecodeApiResponse: XCTestCase {

// WHEN
let decodedResponse = try? JSONDecoder().decode(ApiResponse<Int>.self, from: jsonData)
let dataResponse = fakeDataResponse(decodedResponse: decodedResponse!)

// THEN
XCTAssertNotNil(decodedResponse, "Response shouldn't be nil")
do {
let _ = try apiFetcher.handleApiResponse(decodedResponse!, responseStatusCode: 0)
let _ = try apiFetcher.handleApiResponse(dataResponse)
} catch {
let ikError = error as? InfomaniakError
XCTAssertNotNil(ikError, "Error should be InfomaniakError")
Expand All @@ -97,11 +108,12 @@ final class UTDecodeApiResponse: XCTestCase {

// WHEN
let decodedResponse = try? JSONDecoder().decode(ApiResponse<Int>.self, from: jsonData)
let dataResponse = fakeDataResponse(decodedResponse: decodedResponse!)

// THEN
XCTAssertNotNil(decodedResponse, "Response shouldn't be nil")
do {
let _ = try apiFetcher.handleApiResponse(decodedResponse!, responseStatusCode: 0)
let _ = try apiFetcher.handleApiResponse(dataResponse)
} catch {
let ikError = error as? InfomaniakError
XCTAssertNotNil(ikError, "Error should be InfomaniakError")
Expand All @@ -123,11 +135,12 @@ final class UTDecodeApiResponse: XCTestCase {

// WHEN
let decodedResponse = try? JSONDecoder().decode(ApiResponse<Int>.self, from: jsonData)
let dataResponse = fakeDataResponse(decodedResponse: decodedResponse!)

// THEN
XCTAssertNotNil(decodedResponse, "Response shouldn't be nil")
do {
let _ = try apiFetcher.handleApiResponse(decodedResponse!, responseStatusCode: 0)
let _ = try apiFetcher.handleApiResponse(dataResponse)
} catch {
let ikError = error as? InfomaniakError
XCTAssertNotNil(ikError, "Error should be InfomaniakError")
Expand Down

0 comments on commit e1e7588

Please sign in to comment.