Skip to content

Commit

Permalink
Improve UserCollectionPresenter sort tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Mar 23, 2024
1 parent 4011ea8 commit ce3f90e
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,17 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol {
private let dispatchQueue: DispatchQueueType
private let database: DatabaseProtocol?
private let navigator: UserCollectionNavigator
private let manager: PopoverMenuManagerType
private var currentSortIndex = 0

init(controller: UserCollectionViewControllerProtocol,
dispatchQueue: DispatchQueueType = DispatchQueue.main,
manager: PopoverMenuManagerType = PopoverMenuManager(),
database: DatabaseProtocol?,
navigator: UserCollectionNavigator) {
self.controller = controller
self.dispatchQueue = dispatchQueue
self.manager = manager
self.database = database
self.navigator = navigator
}
Expand Down Expand Up @@ -114,12 +117,11 @@ final class UserCollectionPresenter: UserCollectionPresenterProtocol {

@objc
private func sort(_ sender: UIBarButtonItem, event: UIEvent) {
let manager = PopoverMenuManager()
manager.showPopoverMenu(forEvent: event,
with: [L10n.aToZ, L10n.cardNumber, L10n.color]) { [weak self] selectedIndex in
with: [L10n.aToZ, L10n.cardNumber, L10n.color], done: { [weak self] selectedIndex in
self?.controller?.sort(selectedIndex)
self?.currentSortIndex = selectedIndex
}
}, cancel: {})
}
}

Expand Down
31 changes: 31 additions & 0 deletions SWDestinyTradesTests/Doubles/PopoverMenuManagerSpy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// PopoverMenuManagerSpy.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 23/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

@testable import SWDestinyTrades

final class PopoverMenuManagerSpy: PopoverMenuManagerType {

private(set) var didCallShowPopoverMenuCount = 0
var executeDoneBlock = false
var executeCancelBlock = false
func showPopoverMenu(forEvent event: UIEvent,
with menuArray: [String],
done: ((NSInteger) -> Void)?,
cancel: (() -> Void)?) {
didCallShowPopoverMenuCount += 1
if executeDoneBlock {
done?(0)
}
if executeCancelBlock {
cancel?()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,18 @@ final class UserCollectionPresenterTests: XCTestCase {
private var controller: UserCollectionViewControllerSpy!
private var navigator: UserCollectionNavigator!
private var database: RealmDatabase?
private var manager: PopoverMenuManagerSpy!

override func setUp() {
super.setUp()
controller = UserCollectionViewControllerSpy()
navigationController = UINavigationControllerMock(rootViewController: controller)
database = RealmDatabaseHelper.createMemoryDatabase(identifier: #function)
manager = PopoverMenuManagerSpy()
navigator = UserCollectionNavigator(controller)
sut = UserCollectionPresenter(controller: controller,
dispatchQueue: DispatchQueueSpy(),
manager: manager,
database: database,
navigator: navigator)
}
Expand Down Expand Up @@ -71,7 +74,22 @@ final class UserCollectionPresenterTests: XCTestCase {
let sortButton = barButtonItems?.0?[0]
_ = sortButton?.target?.perform(sortButton!.action, with: nil)

// XCTAssertTrue(navigationController.currentPushedViewController is AddCardViewController)
XCTAssertEqual(manager.didCallShowPopoverMenuCount, 1)
}

func test_sort_selecting_item() {
manager.executeDoneBlock = true

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
Expand Down

0 comments on commit ce3f90e

Please sign in to comment.