Skip to content

Commit

Permalink
Add CollapsibleTableViewHeader unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dogo committed Aug 27, 2024
1 parent ce1f894 commit 2399d28
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ protocol Storable {}

struct Sorted {
var key: String
var ascending: Bool = true
var ascending: Bool
}

protocol DatabaseProtocol: AnyObject {
Expand Down
3 changes: 3 additions & 0 deletions SWDestinyTrades/Classes/Extensions/UIView+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,8 @@ extension UIView {
animation.fillMode = CAMediaTimingFillMode.forwards

layer.add(animation, forKey: nil)

// Update the transform to reflect the final state after animation
transform = CGAffineTransform(rotationAngle: toValue)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//
// CollapsibleTableViewHeaderTests.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 27/08/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import XCTest

@testable import SWDestinyTrades

final class CollapsibleTableViewHeaderTests: XCTestCase {

func test_initialization() {
let header = CollapsibleTableViewHeader(reuseIdentifier: "header")

XCTAssertNotNil(header)
XCTAssertNotNil(header.titleLabel)
XCTAssertNotNil(header.arrowLabel)
XCTAssertEqual(header.section, 0)
XCTAssertNil(header.delegate)
}

func test_height() {
XCTAssertEqual(CollapsibleTableViewHeader.height(), 44)
}

func test_tap_header_calls_delegate() {
let header = CollapsibleTableViewHeader(reuseIdentifier: "header")
let mockDelegate = CollapsibleTableViewHeaderDelegateMock()
header.delegate = mockDelegate
header.section = 1

header.tapHeader(header.gestureRecognizers?[0] as! UITapGestureRecognizer)

XCTAssertTrue(mockDelegate.toggleSectionCalled)
XCTAssertEqual(mockDelegate.toggledSection, 1)
}

func test_tap_header_dont_calls_delegate() {
let header = CollapsibleTableViewHeader(reuseIdentifier: "header")
let mockDelegate = CollapsibleTableViewHeaderDelegateMock()
header.delegate = mockDelegate
header.section = 1

header.tapHeader(UITapGestureRecognizer())

XCTAssertFalse(mockDelegate.toggleSectionCalled)
XCTAssertNil(mockDelegate.toggledSection)
}

func test_set_collapsed_rotates_arrow_label() {
let header = CollapsibleTableViewHeader(reuseIdentifier: "header")

header.setCollapsed(true)
XCTAssertEqual(header.arrowLabel.transform.rotationAngle, 0.0, accuracy: 1e-10)

header.setCollapsed(false)
XCTAssertEqual(header.arrowLabel.transform.rotationAngle, CGFloat.pi / 2, accuracy: 1e-10)
}
}

// MARK: - Mocks

private extension CGAffineTransform {
var rotationAngle: CGFloat {
return atan2(b, a)
}
}

final class CollapsibleTableViewHeaderDelegateMock: CollapsibleTableViewHeaderDelegate {
private(set) var toggleSectionCalled = false
private(set) var toggledSection: Int?

func toggleSection(header: CollapsibleTableViewHeader, section: Int) {
toggleSectionCalled = true
toggledSection = section
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// CollapsibleTableViewHeader+Mirror.swift
// SWDestinyTradesTests
//
// Created by Diogo Autilio on 27/08/24.
// Copyright © 2024 Diogo Autilio. All rights reserved.
//

import Foundation
import UIKit

@testable import SWDestinyTrades

extension CollapsibleTableViewHeader {

var arrowLabel: UILabel {
Mirror.extract(variable: "arrowLabel", from: self)!
}
}

0 comments on commit 2399d28

Please sign in to comment.