Skip to content

Commit

Permalink
Add UserCollectionViewController structure tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Mar 23, 2024
1 parent d301201 commit d27ae4f
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ final class UserCollectionViewController: UIViewController {
try? database?.save(object: object)
}

func loadDataFromRealm() {
private func loadDataFromRealm() {
let user = getUserCollection()
userCollectionView.updateTableViewData(collection: user)
userCollectionView.sort(currentSortIndex)
}

func popOverMenuConfiguration() -> FTConfiguration {
private func popOverMenuConfiguration() -> FTConfiguration {
let config = FTConfiguration()
config.backgoundTintColor = ColorPalette.appTheme
config.borderColor = ColorPalette.appTheme
Expand All @@ -80,7 +80,7 @@ final class UserCollectionViewController: UIViewController {
return config
}

func addToCollection(carDTO: CardDTO) {
private func addToCollection(carDTO: CardDTO) {
let user = getUserCollection()
try? database?.update {
let predicate = NSPredicate(format: "code == %@", carDTO.code)
Expand All @@ -93,7 +93,7 @@ final class UserCollectionViewController: UIViewController {
}
}

func getUserCollection() -> UserCollectionDTO {
private func getUserCollection() -> UserCollectionDTO {
var user = UserCollectionDTO()
try? database?.fetch(UserCollectionDTO.self, predicate: nil, sorted: nil) { [weak self] results in
if let userCollection = results.first {
Expand All @@ -107,17 +107,17 @@ final class UserCollectionViewController: UIViewController {

// MARK: - Navigation

func navigateToCardDetailViewController(cardList: [CardDTO], card: CardDTO) {
private func navigateToCardDetailViewController(cardList: [CardDTO], card: CardDTO) {
navigator.navigate(to: .cardDetail(database: database, with: cardList, card: card))
}

@objc
func navigateToAddCardViewController() {
private func navigateToAddCardViewController() {
navigator.navigate(to: .addCard(database: database, with: getUserCollection()))
}

@objc
func share(_ sender: UIBarButtonItem) {
private func share(_ sender: UIBarButtonItem) {
var collectionList = ""

if let cardList = userCollectionView.getCardList() {
Expand All @@ -143,7 +143,7 @@ final class UserCollectionViewController: UIViewController {
}

@objc
func sort(_ sender: UIBarButtonItem, event: UIEvent) {
private func sort(_ sender: UIBarButtonItem, event: UIEvent) {
FTPopOverMenu.showForEvent(event: event,
with: [L10n.aToZ, L10n.cardNumber, L10n.color],
config: popOverMenuConfiguration(),
Expand All @@ -155,6 +155,7 @@ final class UserCollectionViewController: UIViewController {
}

extension UserCollectionViewController: UserCollectionProtocol {

func stepperValueChanged(newValue: Int, card: CardDTO) {
try? database?.update {
card.quantity = newValue
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
//
// UserCollectionViewControllerTests.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 22/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import UIKit
import XCTest

@testable import SWDestinyTrades

final class UserCollectionViewControllerTests: XCTestCase {

private var sut: UserCollectionViewController!
private var database: RealmDatabase?
private var navigationController: UINavigationController!
private var keyWindow: UIWindow!

override func setUp() {
super.setUp()
keyWindow = UIWindow(frame: .testDevice)
database = RealmDatabaseHelper.createMemoryDatabase(identifier: #function)
sut = UserCollectionViewController(database: database)
navigationController = UINavigationControllerMock(rootViewController: sut)
keyWindow.showTestWindow(controller: navigationController)
}

override func tearDown() {
navigationController = nil
sut = nil
keyWindow.cleanTestWindow()
super.tearDown()
}

func test_loadView() {
sut.loadView()

XCTAssertTrue(sut.view is UserCollectionTableView)
}

func test_viewDidLoad() {
sut.viewDidLoad()

XCTAssertEqual(sut.navigationItem.rightBarButtonItems?.count, 2)
XCTAssertNotNil(sut.navigationItem.leftBarButtonItem)
}

func test_didSelectCard() {
sut.viewDidLoad()
// view.didSelectCard(cardList: [.stub()], card: .stub())

// XCTAssertEqual(presenter.didCallDidSelectSet.count, 1)
// XCTAssertEqual(presenter.didCallDidSelectSet[0].name, "Awakenings")
}

func test_viewWillAppear() {
sut.viewWillAppear(false)

XCTAssertEqual(sut.navigationItem.title, "My Collection")
}

func test_stepperValueChanged() {
let card: CardDTO = .stub()
sut.stepperValueChanged(newValue: 4, card: card)

XCTAssertEqual(card.quantity, 4)
}

func test_remove() {
// XCTAssertEqual(person.borrowed.count, 2)

sut.remove(at: 0)

// XCTAssertEqual(person.borrowed.count, 1)
}
}

0 comments on commit d27ae4f

Please sign in to comment.