Skip to content

Commit 48c8b42

Browse files
committed
Added tests for UIBarButtonItem+Action
1 parent 594ebf8 commit 48c8b42

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

Demo/Demo.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
155834ABB4C21A1B43CBB038 /* Pods_Demo.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0FAA789ED8DE56E04E8476BF /* Pods_Demo.framework */; };
11+
3D21522B1C0385D000563245 /* BarButtonTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3D21522A1C0385D000563245 /* BarButtonTests.swift */; };
1112
5E9E831F1BF79B3000C85B46 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9E831E1BF79B3000C85B46 /* AppDelegate.swift */; };
1213
5E9E83211BF79B3000C85B46 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5E9E83201BF79B3000C85B46 /* ViewController.swift */; };
1314
5E9E83241BF79B3000C85B46 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 5E9E83221BF79B3000C85B46 /* Main.storyboard */; };
@@ -32,6 +33,7 @@
3233
0DF46C3E339DF3F38052433E /* Pods_DemoTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_DemoTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3334
0FAA789ED8DE56E04E8476BF /* Pods_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
3435
2F79F878C5E2083EFB22AA39 /* Pods-DemoTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-DemoTests/Pods-DemoTests.debug.xcconfig"; sourceTree = "<group>"; };
36+
3D21522A1C0385D000563245 /* BarButtonTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BarButtonTests.swift; sourceTree = "<group>"; };
3537
5C9BC35B3BC2F649D5FF6AE4 /* Pods-DemoTests.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-DemoTests.release.xcconfig"; path = "Pods/Target Support Files/Pods-DemoTests/Pods-DemoTests.release.xcconfig"; sourceTree = "<group>"; };
3638
5E9E831B1BF79B3000C85B46 /* Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Demo.app; sourceTree = BUILT_PRODUCTS_DIR; };
3739
5E9E831E1BF79B3000C85B46 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
@@ -115,6 +117,7 @@
115117
children = (
116118
5E9E83331BF79B3000C85B46 /* ActionTests.swift */,
117119
5EA1F72E1BFFBEE700F90655 /* ButtonTests.swift */,
120+
3D21522A1C0385D000563245 /* BarButtonTests.swift */,
118121
5E9E83351BF79B3000C85B46 /* Info.plist */,
119122
);
120123
path = DemoTests;
@@ -339,6 +342,7 @@
339342
isa = PBXSourcesBuildPhase;
340343
buildActionMask = 2147483647;
341344
files = (
345+
3D21522B1C0385D000563245 /* BarButtonTests.swift in Sources */,
342346
5E9E83341BF79B3000C85B46 /* ActionTests.swift in Sources */,
343347
5EA1F72F1BFFBEE700F90655 /* ButtonTests.swift in Sources */,
344348
);
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
import Quick
2+
import Nimble
3+
import RxSwift
4+
import RxBlocking
5+
import Action
6+
7+
class BarButtonTests: QuickSpec {
8+
override func spec() {
9+
10+
it("is nil by default") {
11+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
12+
expect(subject.rx_action).to( beNil() )
13+
}
14+
15+
it("respects setter") {
16+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
17+
18+
let action = emptyAction()
19+
20+
subject.rx_action = action
21+
22+
expect(subject.rx_action) === action
23+
}
24+
25+
it("disables the button while executing") {
26+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
27+
28+
var observer: AnyObserver<Void>!
29+
let action = CocoaAction(workFactory: { _ in
30+
return create { (obsv) -> Disposable in
31+
observer = obsv
32+
return NopDisposable.instance
33+
}
34+
})
35+
36+
subject.rx_action = action
37+
38+
action.execute()
39+
expect(subject.enabled).toEventually( beFalse() )
40+
41+
observer.onCompleted()
42+
expect(subject.enabled).toEventually( beTrue() )
43+
}
44+
45+
it("disables the button if the Action is disabled") {
46+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
47+
48+
subject.rx_action = emptyAction(just(false))
49+
50+
expect(subject.enabled) == false
51+
}
52+
53+
it("doesn't execute a disabled action when tapped") {
54+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
55+
56+
var executed = false
57+
subject.rx_action = CocoaAction(enabledIf: just(false), workFactory: { _ in
58+
executed = true
59+
return empty()
60+
})
61+
62+
subject.target?.performSelector(subject.action, withObject: subject)
63+
64+
expect(executed) == false
65+
}
66+
67+
it("executes the action when tapped") {
68+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
69+
70+
var executed = false
71+
let action = CocoaAction(workFactory: { _ in
72+
executed = true
73+
return empty()
74+
})
75+
subject.rx_action = action
76+
77+
subject.target?.performSelector(subject.action, withObject: subject)
78+
79+
expect(executed) == true
80+
}
81+
82+
it("disposes of old action subscriptions when re-set") {
83+
let subject = UIBarButtonItem(barButtonSystemItem: .Save, target: nil, action: nil)
84+
85+
var disposed = false
86+
autoreleasepool {
87+
let disposeBag = DisposeBag()
88+
89+
let action = emptyAction()
90+
subject.rx_action = action
91+
92+
action
93+
.elements
94+
.subscribe(onNext: nil, onError: nil, onCompleted: nil, onDisposed: {
95+
disposed = true
96+
})
97+
.addDisposableTo(disposeBag)
98+
}
99+
100+
subject.rx_action = nil
101+
102+
expect(disposed) == true
103+
}
104+
}
105+
}

0 commit comments

Comments
 (0)