Permalink
Browse files

Fix #1714.

  • Loading branch information...
kzaher committed Nov 1, 2018
1 parent 02ba785 commit 78c500c9edea08ac4253d2d1cd0b9cf3a6cf370e
Showing with 25 additions and 4 deletions.
  1. +5 −2 RxCocoa/iOS/UISearchBar+Rx.swift
  2. +20 −2 Tests/RxCocoaTests/UISearchBar+RxTests.swift
@@ -29,10 +29,13 @@ extension Reactive where Base: UISearchBar {
public var value: ControlProperty<String?> {
let source: Observable<String?> = Observable.deferred { [weak searchBar = self.base as UISearchBar] () -> Observable<String?> in
let text = searchBar?.text
let textDidChange = (searchBar?.rx.delegate.methodInvoked(#selector(UISearchBarDelegate.searchBar(_:textDidChange:))) ?? Observable.empty())
let didEndEditing = (searchBar?.rx.delegate.methodInvoked(#selector(UISearchBarDelegate.searchBarTextDidEndEditing(_:))) ?? Observable.empty())
return (searchBar?.rx.delegate.methodInvoked(#selector(UISearchBarDelegate.searchBar(_:textDidChange:))) ?? Observable.empty())
return Observable.merge(textDidChange, didEndEditing)
.map { a in
return a[1] as? String
return searchBar?.text ?? ""
}
.startWith(text)
}
@@ -26,7 +26,7 @@ extension UISearchBarTests {
ensurePropertyDeallocated(createView, "a") { (view: UISearchBar) in view.rx.value.orEmpty }
}
func testText_changeEventWorks() {
func testText_changeEventWorksForTextDidChange() {
let searchBar = self.newSearchBar()
var latestText: String! = nil
@@ -39,7 +39,25 @@ extension UISearchBarTests {
XCTAssertEqual(latestText, "")
searchBar.text = "newValue"
searchBar.delegate!.searchBar!(searchBar, textDidChange: "newValue")
searchBar.delegate!.searchBar!(searchBar, textDidChange: "doesntMatter")
XCTAssertEqual(latestText, "newValue")
}
func testText_changeEventWorksForDidEndEditing() {
let searchBar = self.newSearchBar()
var latestText: String! = nil
// search bar should dispose this itself
_ = searchBar.rx.text.subscribe(onNext: { text in
latestText = text
})
XCTAssertEqual(latestText, "")
searchBar.text = "newValue"
searchBar.delegate!.searchBarTextDidEndEditing!(searchBar)
XCTAssertEqual(latestText, "newValue")
}

0 comments on commit 78c500c

Please sign in to comment.