Skip to content

Commit

Permalink
Some adjusts
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Nov 23, 2024
1 parent bffd941 commit 273dbfb
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,15 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol {
let rightBarButtonItems = [addCardBarItem, shareBarItem]

let sortAZAction = UIAction(title: L10n.aToZ) { [weak self] _ in
self?.controller?.sort(.alphabetical)
self?.currentSortIndex = .alphabetical
self?.performSort(.alphabetical)
}

let sortCardNumberAction = UIAction(title: L10n.cardNumber) { [weak self] _ in
self?.controller?.sort(.number)
self?.currentSortIndex = .number
self?.performSort(.number)
}

let sortColorAction = UIAction(title: L10n.color) { [weak self] _ in
self?.controller?.sort(.color)
self?.currentSortIndex = .color
self?.performSort(.color)
}

let sortMenu = UIMenu(children: [sortAZAction, sortCardNumberAction, sortColorAction])
Expand All @@ -97,6 +94,11 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol {
navigator.navigate(to: .addCard(database: database, with: getUserCollection()))
}

private func performSort(_ sortType: SortType) {
controller?.sort(sortType)
currentSortIndex = sortType
}

private func createDatabase(object: UserCollectionDTO) {
try? database?.save(object: object, completion: nil)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ protocol UserCollectionViewType where Self: UITableView {
var didSelectCard: (([CardDTO], CardDTO) -> Void)? { get set }

func updateTableViewData(collection: UserCollectionDTO)
func sort(_ selectedIndex: UserCollectionPresenter.SortType)
func sort(_ type: UserCollectionPresenter.SortType)
func getCardList() -> [CardDTO]?
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,19 +48,14 @@ final class UserCollectionTableView: UITableView, UserCollectionViewType {

// MARK: Sort

func sort(_ selectedIndex: UserCollectionPresenter.SortType) {
switch selectedIndex {
func sort(_ type: UserCollectionPresenter.SortType) {
switch type {
case .alphabetical:
tableViewDatasource?.sortAlphabetically()

case .number:
tableViewDatasource?.sortNumerically()

case .color:
tableViewDatasource?.sortByColor()

default:
break
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ final class UserCollectionViewControllerTests: XCTestCase {
}

func test_sort() {
sut.sort(0)
sut.sort(.alphabetical)

XCTAssertEqual(view.didCallSort.count, 1)
XCTAssertEqual(view.didCallSort[0], 0)
XCTAssertEqual(view.didCallSort[0], .alphabetical)
}

func test_getCardList() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ final class UserCollectionViewControllerSpy: UIViewController, UserCollectionVie
didCallUpdateTableViewData.append(collection)
}

private(set) var didCallSort = [Int]()
func sort(_ selectedIndex: Int) {
didCallSort.append(selectedIndex)
private(set) var didCallSort = [UserCollectionPresenter.SortType]()
func sort(_ type: UserCollectionPresenter.SortType) {
didCallSort.append(type)
}

private(set) var didCallGetCardListCount = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ final class UserCollectionViewSpy: UITableView, UserCollectionViewType {
didCallUpdateTableViewData.append(collection)
}

private(set) var didCallSort = [Int]()
func sort(_ selectedIndex: Int) {
didCallSort.append(selectedIndex)
private(set) var didCallSort = [UserCollectionPresenter.SortType]()
func sort(_ type: UserCollectionPresenter.SortType) {
didCallSort.append(type)
}

private(set) var didCallGetCardListCount = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UserCollectionPresenter+Mirror.swift
// swdestiny-trades
//
// Created by Diogo Autilio on 16/11/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

@testable import SWDestinyTrades

extension UserCollectionPresenter {

var currentSortIndex: UserCollectionPresenter.SortType? {
Mirror.extract(variable: "currentSortIndex", from: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,32 +61,6 @@ final class UserCollectionPresenterTests: XCTestCase {
XCTAssertEqual(expectedItems?.1?.count, 2)
}

// MARK: - Test sort

func test_sort() {
var barButtonItems: ([UIBarButtonItem]?, [UIBarButtonItem]?)?
sut.setupNavigationItems { leftItems, rightItems in
barButtonItems = (leftItems, rightItems)
}
let sortButton = barButtonItems?.0?[0]
_ = sortButton?.target?.perform(sortButton!.action, with: nil)

// XCTAssertEqual(manager.didCallShowPopoverMenuCount, 1)
}

func test_sort_selecting_item() {
var barButtonItems: ([UIBarButtonItem]?, [UIBarButtonItem]?)?
sut.setupNavigationItems { leftItems, rightItems in
barButtonItems = (leftItems, rightItems)
}
let sortButton = barButtonItems?.0?[0]
_ = sortButton?.target?.perform(sortButton!.action, with: nil)

// XCTAssertEqual(manager.didCallShowPopoverMenuCount, 1)
XCTAssertEqual(controller.didCallSort.count, 1)
XCTAssertEqual(controller.didCallSort[0], 0)
}

// MARK: - Test addCard

func test_addCard() {
Expand All @@ -95,7 +69,8 @@ final class UserCollectionPresenterTests: XCTestCase {
barButtonItems = (leftItems, rightItems)
}
let addCardButton = barButtonItems?.1?[0]
_ = addCardButton?.target?.perform(addCardButton!.action, with: nil)
_ = addCardButton?.primaryAction?.performWithSender(<#T##sender: Any?##Any?#>, target: <#T##Any?#>)
//?.target?.perform(addCardButton!.action, with: nil)

XCTAssertTrue(navigationController.currentPushedViewController is AddCardViewController)
}
Expand Down Expand Up @@ -124,7 +99,7 @@ final class UserCollectionPresenterTests: XCTestCase {
XCTAssertNotNil(controller.didCallUpdateTableViewData[0])

XCTAssertEqual(controller.didCallSort.count, 1)
XCTAssertEqual(controller.didCallSort[0], 0)
XCTAssertEqual(controller.didCallSort[0], .alphabetical)
}

func test_loadDataFromRealm_using_existing_database() {
Expand All @@ -134,7 +109,7 @@ final class UserCollectionPresenterTests: XCTestCase {
XCTAssertNotNil(controller.didCallUpdateTableViewData[0])

XCTAssertEqual(controller.didCallSort.count, 1)
XCTAssertEqual(controller.didCallSort[0], 0)
XCTAssertEqual(controller.didCallSort[0], .alphabetical)
}

// MARK: - Test navigateToCardDetail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ final class UserCollectionTableViewTests: XCTestCase {

sut.updateTableViewData(collection: .stub(collection: unsortedCards))

sut.sort(0)
sut.sort(.alphabetical)

XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].name, "Card A")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].name, "Card B")
Expand All @@ -77,7 +77,7 @@ final class UserCollectionTableViewTests: XCTestCase {

sut.updateTableViewData(collection: .stub(collection: unsortedCards))

sut.sort(1)
sut.sort(.number)

XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].code, "001")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].code, "002")
Expand All @@ -91,41 +91,12 @@ final class UserCollectionTableViewTests: XCTestCase {

sut.updateTableViewData(collection: .stub(collection: unsortedCards))

sut.sort(2)
sut.sort(.color)

XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].factionCode, "blue")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].factionCode, "red")
}

func test_sort_wrong_index() {
let unsortedCards: [CardDTO] = [
.stub(factionCode: "red",
code: "002",
name: "Card B"),
.stub(factionCode: "blue",
code: "001",
name: "Card A")
]

sut.updateTableViewData(collection: .stub(collection: unsortedCards))

XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].name, "Card B")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].name, "Card A")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].factionCode, "red")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].factionCode, "blue")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].code, "002")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].code, "001")

sut.sort(3)

XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].name, "Card B")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].name, "Card A")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].factionCode, "red")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].factionCode, "blue")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].code, "002")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].code, "001")
}

func test_heightForRowAt() {
let height = sut.tableView(UITableView(), heightForRowAt: IndexPath(row: 0, section: 0))

Expand Down

0 comments on commit 273dbfb

Please sign in to comment.