Skip to content
This repository was archived by the owner on Mar 10, 2022. It is now read-only.

Commit 5fed024

Browse files
committed
Added tests for the KeyboardHelper
1 parent 37f4766 commit 5fed024

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

KeyboardHelper/Classes/KeyboardHelper.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ public class KeyboardHelper {
5353
delegate = nil
5454
}
5555

56-
private func keyboardWillAppear(note: NSNotification) {
56+
dynamic private func keyboardWillAppear(note: NSNotification) {
5757
let info = KeyboardAppearanceInfo(notification: note)
5858
self.delegate?.keyboardWillAppear(info)
5959
}
6060

61-
private func keyboardWillDisappear(note: NSNotification) {
61+
dynamic private func keyboardWillDisappear(note: NSNotification) {
6262
let info = KeyboardAppearanceInfo(notification: note)
6363
self.delegate?.keyboardWillDisappear(info)
6464
}

KeyboardHelperTests/Tests/KeyboardHelperTests.swift

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,93 @@
99
import XCTest
1010
@testable import KeyboardHelper
1111

12+
class ShowSpyDelegate : KeyboardNotificationDelegate {
13+
var kai : KeyboardAppearanceInfo?
14+
15+
var expectation : XCTestExpectation?
16+
17+
func keyboardWillDisappear(info: KeyboardAppearanceInfo) {
18+
19+
}
20+
21+
func keyboardWillAppear(info: KeyboardAppearanceInfo) {
22+
guard let expectation = expectation else {
23+
XCTFail("ShowSpyDelegate was not setup correctly. Missing XCTExpectation reference")
24+
return
25+
}
26+
27+
kai = info
28+
expectation.fulfill()
29+
}
30+
}
31+
32+
class HideSpyDelegate : KeyboardNotificationDelegate {
33+
var kai : KeyboardAppearanceInfo?
34+
35+
var expectation : XCTestExpectation?
36+
37+
func keyboardWillDisappear(info: KeyboardAppearanceInfo) {
38+
guard let expectation = expectation else {
39+
XCTFail("HideSpyDelegate was not setup correctly. Missing XCTExpectation reference")
40+
return
41+
}
42+
43+
kai = info
44+
expectation.fulfill()
45+
}
46+
47+
func keyboardWillAppear(info: KeyboardAppearanceInfo) {
48+
49+
}
50+
}
51+
1252
class KeyboardHelperTests: XCTestCase {
1353

54+
func testKeyboardShowDelegateMethodIsCalledAsync() {
55+
let spyDelegate = ShowSpyDelegate()
56+
let kh = KeyboardHelper(delegate: spyDelegate)
57+
58+
let expectation = expectationWithDescription("KeyboardHelper calls the delegate as the result of receiving the show notification")
59+
spyDelegate.expectation = expectation
60+
61+
NSNotificationCenter.defaultCenter().postNotificationName(UIKeyboardWillShowNotification, object: kh)
62+
63+
64+
waitForExpectationsWithTimeout(1) { error in
65+
if let error = error {
66+
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
67+
}
68+
69+
guard let result = spyDelegate.kai else {
70+
XCTFail("Expected delegate to be called")
71+
return
72+
}
73+
74+
XCTAssertNotNil(result)
75+
}
76+
}
77+
78+
func testKeyboardHideDelegateMethodIsCalledAsync() {
79+
let spyDelegate = HideSpyDelegate()
80+
let kh = KeyboardHelper(delegate: spyDelegate)
81+
82+
let expectation = expectationWithDescription("KeyboardHelper calls the delegate as the result of receiving the hide notification")
83+
spyDelegate.expectation = expectation
84+
85+
NSNotificationCenter.defaultCenter().postNotificationName(UIKeyboardWillHideNotification, object: kh)
86+
87+
88+
waitForExpectationsWithTimeout(1) { error in
89+
if let error = error {
90+
XCTFail("waitForExpectationsWithTimeout errored: \(error)")
91+
}
92+
93+
guard let result = spyDelegate.kai else {
94+
XCTFail("Expected delegate to be called")
95+
return
96+
}
97+
98+
XCTAssertNotNil(result)
99+
}
100+
}
14101
}

0 commit comments

Comments
 (0)