Skip to content

Feature/home view model test #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func test_didFetchHomeData_isCalled () {
func test_fetchHomeData_shouldCallDelegate() {

//Given
sut.delegate = delegateSpy
//When
sut.fetchData()
//Then
XCTAssertTrue(delegateSpy.delegateIsCalled)
}

func test_fetchHomeDataAndDispatchQueue_isCalled() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func test_fetchHomeDataAndDispatchQueue_isCalled() {
func test_fetchHomeData_whenDataIsNotNil_shouldCallDispatch() {

//when
sut.fetchData()
//then
XCTAssertTrue(financeServiceSpy.fetchHomeDataIsCalled)
XCTAssertTrue(dispatchQueueSpy.isDispatchCalled)
}

func test_fetchData_shouldNotCallDelegate_whenHomedataIsNill () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func test_fetchData_shouldNotCallDelegate_whenHomedataIsNill () {
func test_fetchData_whenHomedataIsNill _shouldNotCallDelegate() {

// Given
financeServiceSpy.homeData = nil
//When
sut.fetchData()
//Then
XCTAssertFalse(dispatchQueueSpy.isDispatchCalled)
}
}

class HomeViewModelDelegateSpy: HomeViewModelDelegate {
var delegateIsCalled: Bool = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var delegateIsCalled: Bool = false
private(set) 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: "")])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

O que você acha de utilizarmos uma fixture

var fetchHomeDataIsCalled: Bool = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var fetchHomeDataIsCalled: Bool = false
private(set) var fetchHomeDataIsCalled: Bool = false


func fetchHomeData(_ completion: @escaping (FinanceApp.HomeData?) -> Void) {
fetchHomeDataIsCalled = true
completion(homeData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import Foundation
@testable import FinanceApp

public class DispatchQueueSpy: DispatchQueueProtocol {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public class DispatchQueueSpy: DispatchQueueProtocol {
final class DispatchQueueSpy: DispatchQueueProtocol {


var isDispatchCalled: Bool = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
var isDispatchCalled: Bool = false
private(set) var isDispatchCalled: Bool = false


public func async(group: DispatchGroup?, qos: DispatchQoS, flags: DispatchWorkItemFlags, execute work: @escaping @convention(block) () -> Void) {
isDispatchCalled = true
work()
}
}