Skip to content

Commit

Permalink
Merge pull request #13 from susan335/support_barbuttonitem
Browse files Browse the repository at this point in the history
Support for UIBarButtonItem
  • Loading branch information
rafiki270 committed Apr 3, 2019
2 parents e0c634d + 876840e commit 2e0c475
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 1 deletion.
6 changes: 6 additions & 0 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions Example/SpecTools/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ViewController: UIViewController {
let button1 = UIButton()
let longPressButton = UIButton()

let barButtonItem = UIBarButtonItem(barButtonSystemItem: .action, target: nil, action: nil)


// MARK: Test properties
var didLoadView: Bool = false
Expand Down Expand Up @@ -102,6 +104,10 @@ class ViewController: UIViewController {
make.right.equalTo(-20)
make.height.equalTo(36)
}

self.navigationItem.rightBarButtonItem = self.barButtonItem
self.barButtonItem.target = self
self.barButtonItem.action = #selector(didTapBarButtonItem(_:))
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -135,4 +141,10 @@ class ViewController: UIViewController {
@objc func longTap(_ sender: UIGestureRecognizer) {
didLongTap = true
}

var didTapBarButtonItem: Bool = false

@objc func didTapBarButtonItem(_ sender: UIBarButtonItem) {
didTapBarButtonItem = true
}
}
15 changes: 15 additions & 0 deletions Example/SpecToolsExampleTests/ViewControllerSpec.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class ViewControllerSpec: QuickSpec {

var button1: UIButton!
var longPressButton: UIButton!
var barButtonItem: UIBarButtonItem!

describe("ViewController") {
beforeEach {
Expand All @@ -42,6 +43,8 @@ class ViewControllerSpec: QuickSpec {

// Get a button that equals "Long press button"
longPressButton = subject.view.spec.find.first(buttonWithText: "Long press button")!

barButtonItem = subject.barButtonItem
}

it("should have gone through the whole initial lifecycle procedure") {
Expand Down Expand Up @@ -120,6 +123,18 @@ class ViewControllerSpec: QuickSpec {
expect(subject.didLongTap).toEventually(beTrue())
}
}

describe("when we tap on barButtonItem") {
beforeEach {
// Simulate long press on button
barButtonItem.spec.action.tap()
}

it("should be able to trigget tap bar button item") {
// Check didTabBarButtonItem function was called
expect(subject.didTapBarButtonItem).to(beTrue())
}
}
}
}
}
24 changes: 24 additions & 0 deletions SpecTools/Classes/Action/Action+UIBarButtonItem.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Action+UIBarButtonItem.swift
// Nimble-iOS
//
// Created by Yohta Watanave on 2019/04/03.
//

import Foundation
import UIKit

extension Action where T: UIBarButtonItem {

// MARK: UIBarButtonItem

/// Simulate tap on a bar button item
/// - Parameter event: Event type to trigger
@discardableResult public func tap() -> Action {
if let target = element.target, let action = element.action {
_ = target.perform(action, with: element)
}

return self
}
}
2 changes: 1 addition & 1 deletion SpecTools/Classes/SpecTools.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ extension PropertyProtocol {
extension UIGestureRecognizer: PropertyProtocol { }
extension UIView: PropertyProtocol { }
extension UIViewController: PropertyProtocol { }

extension UIBarButtonItem: PropertyProtocol { }

0 comments on commit 2e0c475

Please sign in to comment.