Skip to content

Commit 2e0c475

Browse files
authored
Merge pull request #13 from susan335/support_barbuttonitem
Support for UIBarButtonItem
2 parents e0c634d + 876840e commit 2e0c475

File tree

5 files changed

+58
-1
lines changed

5 files changed

+58
-1
lines changed

Example/Pods/Pods.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Example/SpecTools/ViewController.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ class ViewController: UIViewController {
2121
let button1 = UIButton()
2222
let longPressButton = UIButton()
2323

24+
let barButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: nil, action: nil)
25+
2426

2527
// MARK: Test properties
2628
var didLoadView: Bool = false
@@ -102,6 +104,10 @@ class ViewController: UIViewController {
102104
make.right.equalTo(-20)
103105
make.height.equalTo(36)
104106
}
107+
108+
self.navigationItem.rightBarButtonItem = self.barButtonItem
109+
self.barButtonItem.target = self
110+
self.barButtonItem.action = #selector(didTapBarButtonItem(_:))
105111
}
106112

107113
override func viewWillAppear(_ animated: Bool) {
@@ -135,4 +141,10 @@ class ViewController: UIViewController {
135141
@objc func longTap(_ sender: UIGestureRecognizer) {
136142
didLongTap = true
137143
}
144+
145+
var didTapBarButtonItem: Bool = false
146+
147+
@objc func didTapBarButtonItem(_ sender: UIBarButtonItem) {
148+
didTapBarButtonItem = true
149+
}
138150
}

Example/SpecToolsExampleTests/ViewControllerSpec.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ViewControllerSpec: QuickSpec {
2323

2424
var button1: UIButton!
2525
var longPressButton: UIButton!
26+
var barButtonItem: UIBarButtonItem!
2627

2728
describe("ViewController") {
2829
beforeEach {
@@ -42,6 +43,8 @@ class ViewControllerSpec: QuickSpec {
4243

4344
// Get a button that equals "Long press button"
4445
longPressButton = subject.view.spec.find.first(buttonWithText: "Long press button")!
46+
47+
barButtonItem = subject.barButtonItem
4548
}
4649

4750
it("should have gone through the whole initial lifecycle procedure") {
@@ -120,6 +123,18 @@ class ViewControllerSpec: QuickSpec {
120123
expect(subject.didLongTap).toEventually(beTrue())
121124
}
122125
}
126+
127+
describe("when we tap on barButtonItem") {
128+
beforeEach {
129+
// Simulate long press on button
130+
barButtonItem.spec.action.tap()
131+
}
132+
133+
it("should be able to trigget tap bar button item") {
134+
// Check didTabBarButtonItem function was called
135+
expect(subject.didTapBarButtonItem).to(beTrue())
136+
}
137+
}
123138
}
124139
}
125140
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Action+UIBarButtonItem.swift
3+
// Nimble-iOS
4+
//
5+
// Created by Yohta Watanave on 2019/04/03.
6+
//
7+
8+
import Foundation
9+
import UIKit
10+
11+
extension Action where T: UIBarButtonItem {
12+
13+
// MARK: UIBarButtonItem
14+
15+
/// Simulate tap on a bar button item
16+
/// - Parameter event: Event type to trigger
17+
@discardableResult public func tap() -> Action {
18+
if let target = element.target, let action = element.action {
19+
_ = target.perform(action, with: element)
20+
}
21+
22+
return self
23+
}
24+
}

SpecTools/Classes/SpecTools.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,4 @@ extension PropertyProtocol {
105105
extension UIGestureRecognizer: PropertyProtocol { }
106106
extension UIView: PropertyProtocol { }
107107
extension UIViewController: PropertyProtocol { }
108-
108+
extension UIBarButtonItem: PropertyProtocol { }

0 commit comments

Comments
 (0)