-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement some FloatingTextfield unit tests
- Loading branch information
Showing
4 changed files
with
255 additions
and
23 deletions.
There are no files selected for viewing
23 changes: 23 additions & 0 deletions
23
SWDestinyTrades/Classes/Base/NotificationCenter/NotificationCenterProtocol.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// | ||
// NotificationCenterProtocol.swift | ||
// SWDestinyTrades | ||
// | ||
// Created by Diogo Autilio on 25/08/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
protocol NotificationCenterProtocol { | ||
|
||
func post(_ notification: Notification) | ||
|
||
func addObserver(_ observer: Any, | ||
selector aSelector: Selector, | ||
name aName: NSNotification.Name?, | ||
object anObject: Any?) | ||
|
||
func removeObserver(_ observer: Any) | ||
} | ||
|
||
extension NotificationCenter: NotificationCenterProtocol {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
SWDestinyTradesTests/Base/View/FloatingTextfieldTests.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
// | ||
// FloatingTextfieldTests.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 25/08/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class FloatingTextfieldTests: XCTestCase { | ||
|
||
private var sut: FloatingTextfield! | ||
private var notificationCenter: NotificationCenterMock! | ||
|
||
override func setUp() { | ||
super.setUp() | ||
notificationCenter = NotificationCenterMock() | ||
sut = FloatingTextfield(frame: CGRect(x: 0, y: 0, width: 200, height: 50), | ||
notificationCenter: notificationCenter) | ||
sut.animationDuration = 0.0 | ||
} | ||
|
||
override func tearDown() { | ||
sut = nil | ||
super.tearDown() | ||
} | ||
|
||
func testInitialState() { | ||
sut.draw(sut.frame) | ||
sut.drawPlaceholder(in: sut.frame) | ||
|
||
XCTAssertEqual(sut.isLifted, false) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaBefore) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaBefore) | ||
} | ||
|
||
func testDrawUnderline() { | ||
sut.draw(CGRect(x: 0, y: 0, width: 200, height: 50)) | ||
|
||
XCTAssertEqual(sut.underlineView.frame.size.height, sut.underlineWidth) | ||
XCTAssertEqual(sut.underlineView.frame.size.width, sut.frame.size.width) | ||
XCTAssertEqual(sut.underlineView.backgroundColor, sut.underlineColor) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaBefore) | ||
} | ||
|
||
func testPlaceholderLabelSetup() { | ||
let placeholderText = "Test Placeholder" | ||
sut.placeholder = placeholderText | ||
sut.drawPlaceholder(in: sut.bounds) | ||
|
||
XCTAssertEqual(sut.placeholderLabel.text, placeholderText) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaBefore) | ||
XCTAssertEqual(sut.placeholderLabel.textColor, sut.placeholderTextColor) | ||
} | ||
|
||
func testLiftUpPlaceholder() { | ||
sut.didBeginChangeText() | ||
|
||
XCTAssertEqual(sut.isLifted, true) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaAfter, accuracy: 0.0001) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaAfter) | ||
} | ||
|
||
func testLiftDownPlaceholderIfTextIsEmpty() { | ||
sut.text = "" | ||
sut.didEndChangingText() | ||
|
||
XCTAssertEqual(sut.isLifted, false) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaBefore) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaBefore) | ||
} | ||
|
||
func testLiftDownPlaceholderIfTextIsNotEmpty() { | ||
sut.text = "Some text" | ||
sut.didEndChangingText() | ||
|
||
XCTAssertEqual(sut.isLifted, true) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaAfter, accuracy: 0.0001) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaAfter) | ||
} | ||
|
||
func testDidChangeText() { | ||
sut.text = "Some text" | ||
sut.didChangeText() | ||
|
||
XCTAssertEqual(sut.isLifted, true) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaAfter, accuracy: 0.0001) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaAfter) | ||
} | ||
|
||
func testPlaceholderTextChangesOnTextChange() { | ||
sut.text = "" | ||
sut.didChangeText() | ||
|
||
XCTAssertEqual(sut.isLifted, false) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaBefore) | ||
} | ||
|
||
func testPlaceholderLiftWhenEditingBegins() { | ||
sut.text = "" | ||
sut.didBeginChangeText() | ||
|
||
XCTAssertEqual(sut.isLifted, true) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaAfter, accuracy: 0.0001) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaAfter) | ||
} | ||
|
||
func testPlaceholderFallsWhenEditingEndsAndTextIsEmpty() { | ||
sut.text = "" | ||
sut.didEndChangingText() | ||
|
||
XCTAssertEqual(sut.isLifted, false) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaBefore) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaBefore) | ||
} | ||
|
||
func testPlaceholderRemainsLiftedWhenTextIsNotEmptyAfterEditing() { | ||
sut.text = "Some text" | ||
sut.didEndChangingText() | ||
|
||
XCTAssertEqual(sut.isLifted, true) | ||
XCTAssertEqual(sut.placeholderLabel.alpha, sut.placeholderAlphaAfter, accuracy: 0.0001) | ||
XCTAssertEqual(sut.underlineView.alpha, sut.underlineAlphaAfter) | ||
} | ||
|
||
func testNotificationObservers() { | ||
let textDidBeginEditingNotification = notificationCenter.notificationNames.contains(UITextField.textDidBeginEditingNotification) | ||
let textDidChangeNotification = notificationCenter.notificationNames.contains(UITextField.textDidChangeNotification) | ||
let textDidEndEditingNotification = notificationCenter.notificationNames.contains(UITextField.textDidEndEditingNotification) | ||
|
||
XCTAssertTrue(textDidBeginEditingNotification) | ||
XCTAssertTrue(textDidChangeNotification) | ||
XCTAssertTrue(textDidEndEditingNotification) | ||
} | ||
|
||
func testDeinit() { | ||
sut = nil | ||
XCTAssertEqual(notificationCenter.didCallRemoveObserverCount, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
// | ||
// NotificationCenterMock.swift | ||
// SWDestinyTradesTests | ||
// | ||
// Created by Diogo Autilio on 25/08/24. | ||
// Copyright © 2024 Diogo Autilio. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
@testable import SWDestinyTrades | ||
|
||
final class NotificationCenterMock: NotificationCenterProtocol { | ||
|
||
private(set) var notificationNames: [Notification.Name] = [] | ||
|
||
private(set) var didCallPostNotificationCount = 0 | ||
func post(_ notification: Notification) { | ||
didCallPostNotificationCount += 1 | ||
notificationNames.append(notification.name) | ||
NotificationCenter.default.post(notification) | ||
} | ||
|
||
private(set) var didCallAddObserverCount = 0 | ||
func addObserver(_ observer: Any, | ||
selector aSelector: Selector, | ||
name aName: NSNotification.Name?, | ||
object anObject: Any?) { | ||
didCallAddObserverCount += 1 | ||
|
||
if let aName { | ||
notificationNames.append(aName) | ||
} | ||
|
||
NotificationCenter.default.addObserver(observer, | ||
selector: aSelector, | ||
name: aName, | ||
object: anObject) | ||
} | ||
|
||
private(set) var didCallRemoveObserverCount = 0 | ||
func removeObserver(_ observer: Any) { | ||
didCallRemoveObserverCount += 1 | ||
NotificationCenter.default.removeObserver(observer) | ||
} | ||
} |