Skip to content

Commit

Permalink
Add SearchTableView unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Mar 9, 2024
1 parent 221d5f6 commit 1a31325
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 5 deletions.
10 changes: 5 additions & 5 deletions SWDestinyTrades/Classes/Search/View/SearchTableView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import UIKit

final class SearchTableView: UITableView, SearchDelegate {

var didSelectCard: ((CardDTO) -> Void)?
var doingSearch: ((String) -> Void)?

private var searchDatasource: SearchDatasource?
private let search = Search()
Expand All @@ -35,18 +35,18 @@ final class SearchTableView: UITableView, SearchDelegate {
searchDatasource?.updateSearchList(cards)
}

// MARK: <SearchDelegate>
// MARK: - <SearchDelegate>

func didSelectRow(at index: IndexPath) {
if let card = searchDatasource?.getCard(at: index) {
didSelectCard?(card)
}
}

// MARK: Keyboard handling
// MARK: - Keyboard handling

@objc
func keyboardWillShow(notification: NSNotification) {
private func keyboardWillShow(notification: NSNotification) {
if let userInfo: NSDictionary = notification.userInfo as NSDictionary? {
if let keyboardInfo = userInfo[UIResponder.keyboardFrameEndUserInfoKey] as? NSValue {
let keyboardSize = keyboardInfo.cgRectValue.size
Expand All @@ -58,7 +58,7 @@ final class SearchTableView: UITableView, SearchDelegate {
}

@objc
func keyboardWillHide(notification: NSNotification) {
private func keyboardWillHide(notification: NSNotification) {
contentInset = .zero
scrollIndicatorInsets = .zero
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// SearchTableView+Mirror.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 09/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

@testable import SWDestinyTrades

extension SearchTableView {

var searchDatasource: SearchDatasource? {
Mirror.extract(variable: "searchDatasource", from: self)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//
// SearchTableViewTests.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 09/03/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import UIKit
import XCTest

@testable import SWDestinyTrades

final class SearchTableViewTests: XCTestCase {

private var sut: SearchTableView!
private var delegate: PeopleListPresenterSpy!

override func setUp() {
super.setUp()
delegate = PeopleListPresenterSpy()
sut = SearchTableView(frame: .testDevice)
sut.updateSearchList([.stub()])
}

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

func test_updateSearchList() {
sut.updateSearchList([.stub(), .stub()])

XCTAssertEqual(sut.searchDatasource?.cardsData.count, 2)
}

func test_didSelectRowAt() {
var didCallDidSelectCard = 0
var didSelectCardValues: [CardDTO] = []
sut.didSelectCard = { card in
didCallDidSelectCard += 1
didSelectCardValues.append(card)
}

sut.didSelectRow(at: IndexPath(row: 0, section: 0))

XCTAssertEqual(didCallDidSelectCard, 1)
XCTAssertNotNil(didSelectCardValues[0])
}

func test_keyboardWillShow() {
let keyboardSize = CGSize(width: 320, height: 216)
let userInfo: [AnyHashable: Any] = [
UIResponder.keyboardFrameEndUserInfoKey: NSValue(cgRect: CGRect(x: 0, y: 0, width: keyboardSize.width, height: keyboardSize.height))
]

NotificationCenter.default.post(name: UIResponder.keyboardWillShowNotification, object: nil, userInfo: userInfo)

XCTAssertEqual(sut.contentInset.bottom, keyboardSize.height)
XCTAssertEqual(sut.verticalScrollIndicatorInsets.bottom, keyboardSize.height)
}

func test_keyboardWillHide() {
NotificationCenter.default.post(name: UIResponder.keyboardWillHideNotification, object: nil, userInfo: nil)

XCTAssertEqual(sut.contentInset, UIEdgeInsets.zero)
XCTAssertEqual(sut.verticalScrollIndicatorInsets, UIEdgeInsets.zero)
}
}

0 comments on commit 1a31325

Please sign in to comment.