Skip to content

Commit

Permalink
Add CardDetailViewController unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Jan 25, 2024
1 parent a8700d5 commit 120d0d1
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import UIKit

final class CardDetailViewController: UIViewController {

private let cardView: CardView
private let cardView: CardViewType

var presenter: CardDetailPresenterProtocol?

// MARK: - Life Cycle

init(with view: CardView = CardView()) {
init(with view: CardViewType = CardView()) {
cardView = view
super.init(nibName: nil, bundle: nil)
}
Expand Down
21 changes: 21 additions & 0 deletions SWDestinyTrades/Classes/CardDetail/Protocol/CardViewType.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// CardViewType.swift
// SWDestinyTrades
//
// Created by Diogo Autilio on 25/01/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import ImageSlideshow
import UIKit

protocol CardViewType where Self: UIView {

var currentPageChanged: ((_ page: Int) -> Void)? { get set }

func setSlideshowImageInputs(_ imageInputs: [InputSource])
func setCurrentPage(_ page: Int, animated: Bool)
func getCurrentPage() -> Int
func getCurrentSlideshowItem() -> ImageSlideshowItem?
}
2 changes: 1 addition & 1 deletion SWDestinyTrades/Classes/CardDetail/View/CardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ protocol CardDetailViewProtocol: AnyObject {
func showSuccessMessage(card: CardDTO)
}

final class CardView: UIView {
final class CardView: UIView, CardViewType {

var currentPageChanged: ((_ page: Int) -> Void)? {
didSet {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,93 @@
import UIKit
import XCTest

@testable import PKHUD
@testable import SWDestinyTrades

final class CardDetailViewControllerTests: XCTestCase {

private var sut: CardDetailViewController!
private var view: CardViewSpy!
private var presenter: CardDetailPresenterSpy!
private var keyWindow: UIWindow!
private var navigationController: UINavigationControllerMock!

override func setUp() {
super.setUp()
sut = CardDetailViewController()
keyWindow = UIWindow(frame: .testDevice)
view = CardViewSpy()
sut = CardDetailViewController(with: view)
presenter = CardDetailPresenterSpy()
sut.presenter = presenter
navigationController = UINavigationControllerMock(rootViewController: sut)
keyWindow.showTestWindow(controller: navigationController)
}

func testCreateController() {
XCTAssertNotNil(sut)
override func tearDown() {
view = nil
presenter = nil
navigationController = nil
sut = nil
keyWindow.cleanTestWindow()
super.tearDown()
}

func testViewIsKindOfCardView() {
XCTAssertTrue(sut.view is CardView)
func test_loadView() {
sut.loadView()

XCTAssertTrue(sut.view is CardViewType)
}

func test_viewDidLoad() {
sut.viewDidLoad()

XCTAssertEqual(presenter.didCallViewDidLoadCount, 1)
XCTAssertEqual(presenter.didCallSetupNavigationItemsCount, 1)
}

func testNavigationTitle() {
_ = UINavigationController(rootViewController: sut)
func test_viewWillAppear() {
sut.viewWillAppear(false)

XCTAssertEqual(sut.navigationItem.title, "")
XCTAssertEqual(presenter.didCallSetNavigationTitleCount, 1)
}

func test_currentPageChanged() {
sut.viewDidLoad()
view.currentPageChanged?(1)

XCTAssertEqual(presenter.didCallSetNavigationTitleCount, 1)
}

func test_setSlideshowImageInputs() {
sut.setSlideshowImageInputs([])

XCTAssertEqual(view.didCallSetSlideshowImageInputs.count, 0)
}

func test_setCurrentPage() {
sut.setCurrentPage(1, animated: false)

XCTAssertEqual(view.didCallSetCurrentPage[0].page, 1)
XCTAssertEqual(view.didCallSetCurrentPage[0].animated, false)
}

func test_getCurrentPage() {
XCTAssertEqual(sut.getCurrentPage(), 0)
}

func test_getCurrentSlideshowItem() {
XCTAssertEqual(sut.getCurrentSlideshowItem(), nil)
}

func test_setNavigationTitle() {
sut.setNavigationTitle("Card Title")

XCTAssertEqual(sut.navigationItem.title, "Card Title")
}

func test_showSuccessMessage() {
sut.showSuccessMessage(card: .stub())

XCTAssertTrue(keyWindow.subviews.contains { $0 is ContainerView })
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// CardDetailPresenterSpy.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 25/01/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

@testable import SWDestinyTrades

final class CardDetailPresenterSpy: CardDetailPresenterProtocol {

private(set) var didCallViewDidLoadCount = 0
func viewDidLoad() {
didCallViewDidLoadCount += 1
}

private(set) var didCallSetNavigationTitleCount = 0
func setNavigationTitle() {
didCallSetNavigationTitleCount += 1
}

private(set) var didCallSetupNavigationItemsCount = 0
func setupNavigationItems(completion: ([UIBarButtonItem]?) -> Void) {
didCallSetupNavigationItemsCount += 1
completion([])
}
}
40 changes: 40 additions & 0 deletions SWDestinyTradesTests/Screens/CardDetail/Doubles/CardViewSpy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// CardViewSpy.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 25/01/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import ImageSlideshow
import UIKit

@testable import SWDestinyTrades

final class CardViewSpy: UIView, CardViewType {

var currentPageChanged: ((Int) -> Void)?

private(set) var didCallSetSlideshowImageInputs = [InputSource]()
func setSlideshowImageInputs(_ imageInputs: [InputSource]) {
didCallSetSlideshowImageInputs.append(contentsOf: imageInputs)
}

private(set) var didCallSetCurrentPage = [(page: Int, animated: Bool)]()
func setCurrentPage(_ page: Int, animated: Bool) {
didCallSetCurrentPage.append((page, animated))
}

private(set) var didCallGetCurrentPageCount = 0
func getCurrentPage() -> Int {
didCallGetCurrentPageCount += 1
return 0
}

private(set) var didCallGetCurrentSlideshowItemCount = 0
func getCurrentSlideshowItem() -> ImageSlideshowItem? {
didCallGetCurrentSlideshowItemCount += 1
return nil
}
}

0 comments on commit 120d0d1

Please sign in to comment.