diff --git a/solutions/devsprint-brq-testes-ios-1/FinanceApp/Modules/Home/HomeViewModel.swift b/solutions/devsprint-brq-testes-ios-1/FinanceApp/Modules/Home/HomeViewModel.swift index c3f49fb9..54536c52 100644 --- a/solutions/devsprint-brq-testes-ios-1/FinanceApp/Modules/Home/HomeViewModel.swift +++ b/solutions/devsprint-brq-testes-ios-1/FinanceApp/Modules/Home/HomeViewModel.swift @@ -21,9 +21,9 @@ struct HomeViewModel { weak var delegate: HomeViewModelDelegate? private let dispatchQueue: DispatchQueueProtocol - private let financeService: FinanceServiceProtocol + private let financeService: FetchHomeDataProtocol - init(financeService: FinanceServiceProtocol, dispatchQueue: DispatchQueueProtocol) { + init(financeService: FetchHomeDataProtocol, dispatchQueue: DispatchQueueProtocol) { self.financeService = financeService self.dispatchQueue = dispatchQueue } diff --git a/solutions/devsprint-brq-testes-ios-1/FinanceApp/Service/FinanceService.swift b/solutions/devsprint-brq-testes-ios-1/FinanceApp/Service/FinanceService.swift index b0ebb8ff..086df9ed 100644 --- a/solutions/devsprint-brq-testes-ios-1/FinanceApp/Service/FinanceService.swift +++ b/solutions/devsprint-brq-testes-ios-1/FinanceApp/Service/FinanceService.swift @@ -7,15 +7,19 @@ import Foundation -protocol FinanceServiceProtocol { +protocol FinanceServiceProtocol: FetchHomeDataProtocol { - func fetchHomeData(_ completion: @escaping (HomeData?) -> Void) func fetchActivityDetails(_ completion: @escaping (ActivityDetails?) -> Void) func fetchContactList(_ completion: @escaping ([Contact]?) -> Void) func transferAmount(_ completion: @escaping (TransferResult?) -> Void) func fetchUserProfile(_ completion: @escaping (UserProfile?) -> Void) } +protocol FetchHomeDataProtocol { + + func fetchHomeData(_ completion: @escaping (HomeData?) -> Void) +} + class FinanceService: FinanceServiceProtocol { let networkClient: NetworkClientProtocol diff --git a/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/HomeViewModelTests.swift b/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/HomeViewModelTests.swift index 5531e969..541a2b7a 100644 --- a/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/HomeViewModelTests.swift +++ b/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/HomeViewModelTests.swift @@ -8,6 +8,55 @@ import XCTest @testable import FinanceApp -class HomeViewModelTests: XCTestCase { +final class HomeViewModelTests: XCTestCase { + + let dispatchQueueSpy = DispatchQueueSpy() + let financeServiceSpy = FinanceServiceSpy() + let delegateSpy = HomeViewModelDelegateSpy() + lazy var sut = HomeViewModel(financeService: financeServiceSpy, dispatchQueue: dispatchQueueSpy) + + func test_didFetchHomeData_isCalled () { + //Given + sut.delegate = delegateSpy + //When + sut.fetchData() + //Then + XCTAssertTrue(delegateSpy.delegateIsCalled) + } + + func test_fetchHomeDataAndDispatchQueue_isCalled() { + //when + sut.fetchData() + //then + XCTAssertTrue(financeServiceSpy.fetchHomeDataIsCalled) + XCTAssertTrue(dispatchQueueSpy.isDispatchCalled) + } + + func test_fetchData_shouldNotCallDelegate_whenHomedataIsNill () { + // Given + financeServiceSpy.homeData = nil + //When + sut.fetchData() + //Then + XCTAssertFalse(dispatchQueueSpy.isDispatchCalled) + } +} + +class HomeViewModelDelegateSpy: HomeViewModelDelegate { + var delegateIsCalled: Bool = false + + func didFetchHomeData(_ data: FinanceApp.HomeData) { + delegateIsCalled = true + } +} +class FinanceServiceSpy: FetchHomeDataProtocol { + + var homeData: HomeData? = HomeData(balance: 0, savings: 0, spending: 0, activity: [Activity(name: "", price: 0, time: "")]) + var fetchHomeDataIsCalled: Bool = false + + func fetchHomeData(_ completion: @escaping (FinanceApp.HomeData?) -> Void) { + fetchHomeDataIsCalled = true + completion(homeData) + } } diff --git a/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/Test Doubles/DispatchQueueSpy.swift b/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/Test Doubles/DispatchQueueSpy.swift index 3c56030d..a0915248 100644 --- a/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/Test Doubles/DispatchQueueSpy.swift +++ b/solutions/devsprint-brq-testes-ios-1/FinanceAppTests/Test Doubles/DispatchQueueSpy.swift @@ -5,7 +5,12 @@ import Foundation @testable import FinanceApp public class DispatchQueueSpy: DispatchQueueProtocol { + + var isDispatchCalled: Bool = false + public func async(group: DispatchGroup?, qos: DispatchQoS, flags: DispatchWorkItemFlags, execute work: @escaping @convention(block) () -> Void) { + isDispatchCalled = true work() } } +