Skip to content

Commit

Permalink
Add UserCollectionDatasource unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Mar 22, 2024
1 parent c821ef0 commit d301201
Show file tree
Hide file tree
Showing 5 changed files with 188 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ final class UserCollectionDatasource: NSObject, UITableViewDataSource {
private var tableView: UITableView?
private var userCollection: UserCollectionDTO?
private weak var delegate: UserCollectionProtocol?
var collectionList: [CardDTO] = []
private var collectionList: [CardDTO] = []

required init(tableView: UITableView, delegate: UserCollectionProtocol?) {
super.init()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
//
// UserCollectionDatasourceTests.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 22/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit
import XCTest

@testable import SWDestinyTrades

final class UserCollectionDatasourceTests: XCTestCase {

private var sut: UserCollectionDatasource!
private var delegate: UserCollectionPresenterSpy!
private var tableView: UITableView!

override func setUp() {
super.setUp()
tableView = UITableView()
delegate = UserCollectionPresenterSpy()
sut = UserCollectionDatasource(tableView: tableView, delegate: delegate)
sut.updateTableViewData(collection: .stub(collection: [.stub(), .stub()]))
}

override func tearDown() {
tableView = nil
delegate = nil
sut = nil
super.tearDown()
}

func test_cellForRowAt() {
let cell = sut.tableView(tableView, cellForRowAt: IndexPath(row: 0, section: 0))

XCTAssertTrue(cell is LoanDetailCell)
}

func test_deleteRow() {
XCTAssertEqual(sut.collectionList?.count, 2)

let indexPathToDelete = IndexPath(row: 0, section: 0)
sut.tableView(tableView, commit: .delete, forRowAt: indexPathToDelete)

XCTAssertEqual(sut.collectionList?.count, 1)
XCTAssertEqual(delegate.didCallRemoveAtIndex.count, 1)
XCTAssertNotNil(delegate.didCallRemoveAtIndex[0])
}

func test_numberOfRowsInSection() {
let rows = sut.tableView(tableView, numberOfRowsInSection: 0)

XCTAssertEqual(rows, 2)
}

func test_getCard() {
let card = sut.getCard(at: IndexPath(row: 0, section: 0))

XCTAssertNotNil(card)
}

func test_getCardList() {
let cardList = sut.getCardList()

XCTAssertEqual(cardList.count, 2)
}

func test_updateTableViewData() {
let cardList: [CardDTO] = [
.stub(),
.stub(),
.stub(),
.stub()
]

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

XCTAssertEqual(sut.collectionList?.count, 4)
}

func test_sortAlphabetically() {
let unsortedCards: [CardDTO] = [
.stub(name: "Card B"),
.stub(name: "Card A")
]

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

sut.sortAlphabetically()

XCTAssertEqual(sut.collectionList?[0].name, "Card A")
XCTAssertEqual(sut.collectionList?[1].name, "Card B")
}

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

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

sut.sortNumerically()

XCTAssertEqual(sut.collectionList?[0].code, "001")
XCTAssertEqual(sut.collectionList?[1].code, "002")
}

func test_sortByColor() {
let unsortedCards: [CardDTO] = [
.stub(factionCode: "red"),
.stub(factionCode: "blue")
]

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

sut.sortByColor()

XCTAssertEqual(sut.collectionList?[0].factionCode, "blue")
XCTAssertEqual(sut.collectionList?[1].factionCode, "red")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// UserCollectionPresenterSpy.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 22/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation

@testable import SWDestinyTrades

final class UserCollectionPresenterSpy: UserCollectionProtocol {

private(set) var didCallStepperValueChanged = [(newValue: Int, card: CardDTO)]()
func stepperValueChanged(newValue: Int, card: CardDTO) {
didCallStepperValueChanged.append((newValue: newValue, card: card))
}

private(set) var didCallRemoveAtIndex = [Int]()
func remove(at index: Int) {
didCallRemoveAtIndex.append(index)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// UserCollectionDatasource+Mirror.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 22/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

@testable import SWDestinyTrades

extension UserCollectionDatasource {

var collectionList: [CardDTO]? {
Mirror.extract(variable: "collectionList", from: self)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ final class UserCollectionTableViewTests: XCTestCase {
func test_updateTableViewData() {
sut.updateTableViewData(collection: .stub(collection: [.stub(), .stub()]))

XCTAssertEqual(sut.tableViewDatasource?.collectionList.count, 2)
XCTAssertEqual(sut.tableViewDatasource?.collectionList?.count, 2)
}

func test_getCardList() {
Expand Down Expand Up @@ -65,8 +65,8 @@ final class UserCollectionTableViewTests: XCTestCase {

sut.sort(0)

XCTAssertEqual(sut.tableViewDatasource?.collectionList[0].name, "Card A")
XCTAssertEqual(sut.tableViewDatasource?.collectionList[1].name, "Card B")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].name, "Card A")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].name, "Card B")
}

func test_sort_numerically() {
Expand All @@ -79,8 +79,8 @@ final class UserCollectionTableViewTests: XCTestCase {

sut.sort(1)

XCTAssertEqual(sut.tableViewDatasource?.collectionList[0].code, "001")
XCTAssertEqual(sut.tableViewDatasource?.collectionList[1].code, "002")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[0].code, "001")
XCTAssertEqual(sut.tableViewDatasource?.collectionList?[1].code, "002")
}

func test_sort_byColor() {
Expand All @@ -93,8 +93,8 @@ final class UserCollectionTableViewTests: XCTestCase {

sut.sort(2)

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

func test_sort_wrong_index() {
Expand All @@ -109,21 +109,21 @@ final class UserCollectionTableViewTests: XCTestCase {

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")
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")
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() {
Expand Down

0 comments on commit d301201

Please sign in to comment.