diff --git a/SWDestinyTrades/Classes/UserCollection/Controller/UserCollectionViewController.swift b/SWDestinyTrades/Classes/UserCollection/Controller/UserCollectionViewController.swift index eafd08bf..10f8eacb 100644 --- a/SWDestinyTrades/Classes/UserCollection/Controller/UserCollectionViewController.swift +++ b/SWDestinyTrades/Classes/UserCollection/Controller/UserCollectionViewController.swift @@ -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 @@ -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) @@ -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 { @@ -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() { @@ -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(), @@ -155,6 +155,7 @@ final class UserCollectionViewController: UIViewController { } extension UserCollectionViewController: UserCollectionProtocol { + func stepperValueChanged(newValue: Int, card: CardDTO) { try? database?.update { card.quantity = newValue diff --git a/SWDestinyTradesTests/Screens/UserCollection/Controller/UserCollectionViewControllerTests.swift b/SWDestinyTradesTests/Screens/UserCollection/Controller/UserCollectionViewControllerTests.swift new file mode 100644 index 00000000..1a3c9439 --- /dev/null +++ b/SWDestinyTradesTests/Screens/UserCollection/Controller/UserCollectionViewControllerTests.swift @@ -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) + } +}