Skip to content

Commit

Permalink
Merge pull request #780 from DeluxeAlonso/feature/unit-tests
Browse files Browse the repository at this point in the history
Feature/unit tests
  • Loading branch information
DeluxeAlonso committed Sep 3, 2023
2 parents 054ffd5 + 8701e61 commit 740d0ab
Showing 1 changed file with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//
// AccountClientTests.swift
// NetworkInfrastructure-Unit-NetworkInfrastructureTests
//
// Created by Alonso on 3/09/23.
//

import XCTest
@testable import NetworkInfrastructure

final class AccountClientTests: XCTestCase {

private var urlSession: MockURLSession!
private var accountClient: AccountClient!

override func setUpWithError() throws {
try super.setUpWithError()
urlSession = MockURLSession()
accountClient = AccountClient(session: urlSession)

}

override func tearDownWithError() throws {
urlSession = nil
accountClient = nil
try super.tearDownWithError()
}

func testGetFavoriteListSuccess() throws {
// Arrange
let data = try JSONEncoder().encode(MovieResult(results: [], currentPage: 1, totalPages: 1))
guard let url = URL(string: "www.google.com") else {
XCTFail("Invalid URL")
return
}
urlSession.dataTaskWithRequestCompletionHandler = (data, HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil), nil)
let expectation = XCTestExpectation(description: "Get favorite list success")
// Act
accountClient.getFavoriteList(page: 1, sortBy: .createdAtAsc, sessionId: "", accountId: 1) { result in
switch result {
case .success:
break
case .failure:
XCTFail("Get favorite list error")
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

func testGetFavoriteListError() throws {
// Arrange
urlSession.dataTaskWithRequestCompletionHandler = (nil, nil, nil)
let expectation = XCTestExpectation(description: "Get favorite list error")
// Act
accountClient.getFavoriteList(page: 1, sortBy: .createdAtAsc, sessionId: "", accountId: 1) { result in
switch result {
case .success:
XCTFail("Get favorite list success")
case .failure:
break
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

func testGetWatchlistSuccess() throws {
// Arrange
let data = try JSONEncoder().encode(MovieResult(results: [], currentPage: 1, totalPages: 1))
guard let url = URL(string: "www.google.com") else {
XCTFail("Invalid URL")
return
}
urlSession.dataTaskWithRequestCompletionHandler = (data, HTTPURLResponse(url: url, statusCode: 200, httpVersion: nil, headerFields: nil), nil)
let expectation = XCTestExpectation(description: "Get watchlist success")
// Act
accountClient.getWatchlist(page: 1, sortBy: .createdAtAsc, sessionId: "", accountId: 1) { result in
switch result {
case .success:
break
case .failure:
XCTFail("Get watchlist error")
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

func testGetWatchlistError() throws {
// Arrange
urlSession.dataTaskWithRequestCompletionHandler = (nil, nil, nil)
let expectation = XCTestExpectation(description: "Get watchlist error")
// Act
accountClient.getWatchlist(page: 1, sortBy: .createdAtAsc, sessionId: "", accountId: 1) { result in
switch result {
case .success:
XCTFail("Get watchlist success")
case .failure:
break
}
expectation.fulfill()
}
// Assert
wait(for: [expectation], timeout: 1.0)
}

}

0 comments on commit 740d0ab

Please sign in to comment.