-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
218 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
SWDestinyTrades/Classes/NewPerson/Protocols/NewPersonPresenter.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// NewPersonPresenter.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 27/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
protocol NewPersonPresenterProtocol: AnyObject { | ||
func setNavigationTitle() | ||
func setupNavigationItems(completion: ([UIBarButtonItem]?) -> Void) | ||
} | ||
|
||
final class NewPersonPresenter: NewPersonPresenterProtocol { | ||
|
||
private weak var controller: NewPersonViewControllerProtocol? | ||
|
||
private weak var delegate: UpdateTableDataDelegate? | ||
|
||
init(controller: NewPersonViewControllerProtocol?, | ||
delegate: UpdateTableDataDelegate?) { | ||
self.controller = controller | ||
self.delegate = delegate | ||
} | ||
|
||
func setNavigationTitle() { | ||
controller?.setNavigationTitle(L10n.newPerson) | ||
} | ||
|
||
func setupNavigationItems(completion: ([UIBarButtonItem]?) -> Void) { | ||
let doneButton = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(doneButtonTouched(_:))) | ||
completion([doneButton]) | ||
} | ||
|
||
// MARK: - UIBarButton Actions | ||
|
||
@objc | ||
private func doneButtonTouched(_ sender: Any) { | ||
let userInput = controller?.retriveUserInput() | ||
let name = userInput?.name ?? "" | ||
let lastName = userInput?.lastName ?? "" | ||
|
||
if !name.isEmpty { | ||
let person = PersonDTO() | ||
person.name = name | ||
person.lastName = lastName | ||
delegate?.insertNew(person: person) | ||
} | ||
controller?.closeViewController() | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
SWDestinyTrades/Classes/NewPerson/Protocols/NewPersonViewControllerProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// NewPersonViewControllerProtocol.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 27/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol NewPersonViewControllerProtocol: AnyObject { | ||
|
||
func setNavigationTitle(_ title: String) | ||
func retriveUserInput() -> (name: String, lastName: String) | ||
func closeViewController() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
SWDestinyTradesTests/Screens/NewPerson/Doubles/NewPersonViewControllerSpy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// | ||
// NewPersonViewControllerSpy.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 28/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import UIKit | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class NewPersonViewControllerSpy: UIViewController, NewPersonViewControllerProtocol { | ||
|
||
private(set) var didCallSetNavigationTitle = [String]() | ||
func setNavigationTitle(_ title: String) { | ||
didCallSetNavigationTitle.append(title) | ||
} | ||
|
||
private(set) var didCallRetriveUserInputCount = 0 | ||
func retriveUserInput() -> (name: String, lastName: String) { | ||
didCallRetriveUserInputCount += 1 | ||
return ("name", "lastName") | ||
} | ||
|
||
private(set) var didCallCloseViewControllerCount = 0 | ||
func closeViewController() { | ||
didCallCloseViewControllerCount += 1 | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
SWDestinyTradesTests/Screens/NewPerson/Doubles/UpdateTableDataDelegateSpy.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// | ||
// UpdateTableDataDelegateSpy.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 28/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class UpdateTableDataDelegateSpy: UpdateTableDataDelegate { | ||
|
||
private(set) var didCallInsertNew = [PersonDTO]() | ||
func insertNew(person: PersonDTO) { | ||
didCallInsertNew.append(person) | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
SWDestinyTradesTests/Screens/NewPerson/Presenter/NewPersonPresenterTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// NewPersonPresenterTests.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 28/02/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import UIKit | ||
import XCTest | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class NewPersonPresenterTests: XCTestCase { | ||
|
||
private var sut: NewPersonPresenter! | ||
private var navigationController: UINavigationControllerMock! | ||
private var controller: NewPersonViewControllerSpy! | ||
private var delegate: UpdateTableDataDelegateSpy! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
controller = NewPersonViewControllerSpy() | ||
delegate = UpdateTableDataDelegateSpy() | ||
navigationController = UINavigationControllerMock(rootViewController: controller) | ||
sut = NewPersonPresenter(controller: controller, | ||
delegate: delegate) | ||
} | ||
|
||
override func tearDown() { | ||
controller = nil | ||
navigationController = nil | ||
sut = nil | ||
super.tearDown() | ||
} | ||
|
||
// MARK: - Test setupNavigationItems | ||
|
||
func test_setupNavigationItems() { | ||
var expectedItem: [UIBarButtonItem]? = [] | ||
sut.setupNavigationItems { items in | ||
expectedItem = items | ||
} | ||
|
||
XCTAssertEqual(expectedItem?.count, 1) | ||
} | ||
|
||
// MARK: - Test setNavigationTitle | ||
|
||
func test_setNavigationTitle() { | ||
sut.setNavigationTitle() | ||
|
||
XCTAssertEqual(controller.didCallSetNavigationTitle.count, 1) | ||
XCTAssertEqual(controller.didCallSetNavigationTitle[0], "New Person") | ||
} | ||
|
||
// MARK: - Test addButtonTouched | ||
|
||
func test_doneButtonTouched() { | ||
var barButtonItems: [UIBarButtonItem]? = [] | ||
sut.setupNavigationItems { items in | ||
barButtonItems = items | ||
} | ||
let addButton = barButtonItems?[0] | ||
_ = addButton?.target?.perform(addButton!.action, with: nil) | ||
|
||
XCTAssertEqual(controller.didCallCloseViewControllerCount, 1) | ||
XCTAssertEqual(delegate.didCallInsertNew.count, 1) | ||
XCTAssertNotNil(delegate.didCallInsertNew[0]) | ||
} | ||
} |