import Foundation import XCTest @testable import OldFella final class NetworkFailureTests: XCTestCase { func testCancellationIncludesWrappedErrors() { XCTAssertTrue(NetworkFailure.isCancellation(CancellationError())) XCTAssertTrue(NetworkFailure.isCancellation(URLError(.cancelled))) let wrapped = NSError(domain: "wrapper", code: 1, userInfo: [NSUnderlyingErrorKey: URLError(.cancelled)]) XCTAssertTrue(NetworkFailure.isCancellation(ActionNotSentError(underlying: wrapped))) XCTAssertFalse(NetworkFailure.isConnectionFailure(wrapped)) XCTAssertFalse(NetworkFailure.isCancellation(URLError(.timedOut))) } func testConnectionMessagesIncludeWrappedErrors() { let offline = ActionNotSentError(underlying: URLError(.notConnectedToInternet)) XCTAssertTrue(NetworkFailure.isConnectionFailure(offline)) XCTAssertEqual( NetworkFailure.message(for: offline), "You are not connected to the internet. Check your connection and try again.") XCTAssertEqual(NetworkFailure.message(for: URLError(.timedOut)), "The request timed out. Try again.") XCTAssertEqual( NetworkFailure.message(for: URLError(.networkConnectionLost)), "The connection to the server failed. Try again.") XCTAssertEqual( NetworkFailure.message(for: URLError(.cannotFindHost)), "The connection to the server failed. Try again.") } func testOtherErrorsKeepTheirDescription() { let error = NSError(domain: "example", code: 7, userInfo: [NSLocalizedDescriptionKey: "Example failure"]) XCTAssertFalse(NetworkFailure.isConnectionFailure(error)) XCTAssertEqual(NetworkFailure.message(for: error), "Example failure") } }