Unit testing Rx binding with UITextView - ios

I have a ControlProperty binding like this in my UIViewController:
textView.rx.text.orEmpty.asObservable()
.bind(to: viewModel.descriptions)
.disposed(by: disposeBag)
, where viewModel.descriptions is of type BehaviorRelay<String>.
In a TDD approach, I would like to write an (initially failing) test (as the initial step of the red-green-refactor cycle) to see that this binding exists. I tried with a test code like this:
sut.textView.insertText("DESC")
sut.textView.delegate?.textViewDidChange?(sut.textView)
expect(mockViewModel.lastDescriptions).to(equal("DESC"))
, where sut is my ViewController and mockViewModel contains code to subscribe to the descriptions BehaviorRelay:
class MockViewModel: MyViewModelProtocol {
var descriptions = BehaviorRelay<String>(value: "")
var lastDescriptions: String?
private var disposeBag = DisposeBag()
init() {
descriptions.asObservable()
.skip(1)
.subscribe(onNext: { [weak self] (title: String) in
self?.lastDescriptions = title
})
.disposed(by: disposeBag)
}
}
However, I cannot push the new value to the textView so that the descriptions BehaviorRelay gets it.
How can I achieve descriptions.asObservable() to fire when entering a value in the textView? If it were a UITextField then I would do:
textField.text = "DESC"
textField.sendActions((for: .editingChanged))
Thanks in advance for any help.

Related

Why doesn't the subscription of the relay work even though there is a value passed to it?

I have set up a relay like this:
class CustomAlertViewController: UIViewController {
private let disposeBag = DisposeBag()
var alertTextRelay = BehaviorRelay<String>(value: "")
alertTextField.rx.value.orEmpty.changed.subscribe(onNext: { [weak self] value in
print("THIS IS RIGHT", value)
self?.alertTextRelay.accept(value)
print("THIS IS ALSO RIGHT", self?.alertTextRelay.value)
}).disposed(by: disposeBag)
...
I get the text writen in the textField and accept it to the relay and check so that the value is really there. But the subscription never trigger:
class RecievingClass: UIViewController {
let disposeBag = DisposeBag()
let instance = CustomAlertViewController()
instance.alertTextRelay.subscribe(onNext: { value in
self.myText = value
}, onError: { error in
print(error)
}, onCompleted: {
print("completed")
}).disposed(by: disposeBag)
...
Nothing in the subscription is triggered. Why? If it helps, CustomAlertViewController is used as a custom alert (a view on an overlay). Also, there are a lot of static functions in CustomAlertViewController. Not sure if that's relevant. Let me know if I can provide anything else.
Try to change this alertTextField.rx.value.orEmpty.changed.subscribe on this alertTextField.rx.text.orEmpty.asObservable(). And add [weak self] for RecievingClass in other way you get memory leak.

How to subscribe on the value changed control event of a UISwitch Using Rxswift

I want to use Rxswift and not IBActions to solve my issue below,
I have a UISwitch and I want to subscribe to the value changed event in
it,
I usually subscribe on Buttons using this manner
#IBOutlet weak var myButton: UIButton!
myButton
.rx
.tapGesture()
.when(.recognized)
.subscribe(onNext : {_ in /*do action here */})
Does anyone know how to subscribe to UISwitch control events?
I found the answer Im looking for, in order to subscribe on and control event we should do the below :
#IBOutlet weak var mySwitch : UISwitch!
mySwitch
.rx
.controlEvent(.valueChanged)
.withLatestFrom(mySwitch.rx.value)
.subscribe(onNext : { bool in
// this is the value of mySwitch
})
.disposed(by: disposeBag)
Below are some caveats you would use for UISwitch:
1. Make sure the event subscribes to unique changes so use distinctUntilChanged
2. Rigorous switching the switch can cause unexpected behavior so use debounce.
Example:
anySwitch.rx
.isOn.changed //when state changed
.debounce(0.8, scheduler: MainScheduler.instance) //handle rigorous user switching
.distinctUntilChanged().asObservable() //take signal if state is different than before. This is optional depends on your use case
.subscribe(onNext:{[weak self] value in
//your code
}).disposed(by: disposeBag)
There are couple of ways to do that. But this one is how I usually do it:
Try this out.
self.mySwitch.rx.isOn.subscribe { isOn in
print(isOn)
}.disposed(by: self.disposeBag)
I hope this helps.
EDIT:
Another would be subscribing to the value rx property of the UISwitch, like so:
mySwitch.rx.value.subscribe { (isOn) in
print(isOn)
}.disposed(by: self.disposeBag)
Now, as for your comment:
this worked for me thanks , but I preferred subscribing on the control
event it self, not the value.
We could do this below, I'm not sure though if there's a better way than this. Since UISwitch is a UIControl object, you can subscribe to its .valueChanged event, like so:
mySwitch.rx.controlEvent([.valueChanged]).subscribe { _ in
print("isOn? : \(mySwitch.isOn)")
}.disposed(by: self.disposeBag)
More info: https://developer.apple.com/documentation/uikit/uiswitch
The following code works for me, building on prex's answer. Please feel free to correct or suggest any improvements.
RxSwift/RxCocoa 4.4, iOS 12, Xcode 10
private let _disposeBag = DisposeBag()
let viewModel: ViewModel ...
let switchControl: UIControl ...
let observable = switchControl
.rx.isOn
.changed // We want user-initiated changes only.
.distinctUntilChanged()
.share(replay: 1, scope: .forever)
observable
.subscribe(onNext: { [weak self] value in
self?.viewModel.setSomeOption(value)
})
.disposed(by: _disposeBag)
In this example, we are notified when the user (and only the user) changes the state of the UISwitch. We then update viewModel.
In turn, any observers of the viewModel, will be told of the change of state, allowing them to update UI, etc.
e.g.
class ViewModel
{
private var _someOption = BehaviorRelay(value: false)
func setSomeOption(_ value: Bool) { _someOption.accept(value) }
/// Creates and returns an Observable on the main thread.
func observeSomeOption() -> Observable<Bool>
{
return _someOption
.asObservable()
.observeOn(MainScheduler())
}
}
...
// In viewDidLoad()
self.viewModel
.observeSomeOption()
.subscribe(onNext: { [weak self] value in
self?.switchControl.isOn = value
// could show/hide other UI here.
})
.disposed(by: _disposeBag)
See: https://stackoverflow.com/a/53479618/1452758

Unit-test RxSwift observable in ViewController

I'm quite new to RxSwift. I have a view controller that has a typeahead/autocomplete feature (i.e., user types in a UITextField and as soon as they enter at least 2 characters a network request is made to search for matching suggestions). The controller's viewDidLoad calls the following method to set up an Observable:
class TypeaheadResultsViewController: UIViewController {
var searchTextFieldObservable: Observable<String>!
#IBOutlet weak var searchTextField: UITextField!
private let disposeBag = DisposeBag()
var results: [TypeaheadResult]?
override func viewDidLoad() {
super.viewDidLoad()
//... unrelated setup stuff ...
setupSearchTextObserver()
}
func setupSearchTextObserver() {
searchTextFieldObservable =
self.searchTextField
.rx
.text
.throttle(0.5, scheduler: MainScheduler.instance)
.map { $0 ?? "" }
searchTextFieldObservable
.filter { $0.count >= 2 }
.flatMapLatest { searchTerm in self.search(for: searchTerm) }
.subscribe(
onNext: { [weak self] searchResults in
self?.resetResults(results: searchResults)
},
onError: { [weak self] error in
print(error)
self?.activityIndicator.stopAnimating()
}
)
.disposed(by: disposeBag)
// This is the part I want to test:
searchTextFieldObservable
.filter { $0.count < 2 }
.subscribe(
onNext: { [weak self] _ in
self?.results = nil
}
)
.disposed(by: disposeBag)
}
}
This seems to work fine, but I'm struggling to figure out how to unit test the behavior of searchTextFieldObservable.
To keep it simple, I just want a unit test to verify that results is set to nil when searchTextField has fewer than 2 characters after a change event.
I have tried several different approaches. My test currently looks like this:
class TypeaheadResultsViewControllerTests: XCTestCase {
var ctrl: TypeaheadResultsViewController!
override func setUp() {
super.setUp()
let storyboard = UIStoryboard(name: "MainStoryboard", bundle: nil)
ctrl = storyboard.instantiateViewController(withIdentifier: "TypeaheadResultsViewController") as! TypeaheadResultsViewController
}
override func tearDown() {
ctrl = nil
super.tearDown()
}
/// Verify that the searchTextObserver sets the results array
/// to nil when there are less than two characters in the searchTextView
func testManualChange() {
// Given: The view is loaded (this triggers viewDidLoad)
XCTAssertNotNil(ctrl.view)
XCTAssertNotNil(ctrl.searchTextField)
XCTAssertNotNil(ctrl.searchTextFieldObservable)
// And: results is not empty
ctrl.results = [ TypeaheadResult(value: "Something") ]
let tfObservable = ctrl.searchTextField.rx.text.subscribeOn(MainScheduler.instance)
//ctrl.searchTextField.rx.text.onNext("e")
ctrl.searchTextField.insertText("e")
//ctrl.searchTextField.text = "e"
do {
guard let result =
try tfObservable.toBlocking(timeout: 5.0).first() else {
return }
XCTAssertEqual(result, "e") // passes
XCTAssertNil(ctrl.results) // fails
} catch {
print(error)
}
}
Basically, I'm wondering how to manually/programmatically fire an event on searchTextFieldObservable (or, preferably, on the searchTextField) to trigger the code in the 2nd subscription marked "This is the part I want to test:".
The first step is to separate the logic from the effects. Once you do that, it will be easy to test your logic. In this case, the chain you want to test is:
self.searchTextField.rx.text
.throttle(0.5, scheduler: MainScheduler.instance)
.map { $0 ?? "" }
.filter { $0.count < 2 }
.subscribe(
onNext: { [weak self] _ in
self?.results = nil
}
)
.disposed(by: disposeBag)
The effects are only the source and the sink (another place to look out for effects is in any flatMaps in the chain.) So lets separate them out:
(I put this in an extension because I know how much most people hate free functions)
extension ObservableConvertibleType where E == String? {
func resetResults(scheduler: SchedulerType) -> Observable<Void> {
return asObservable()
.throttle(0.5, scheduler: scheduler)
.map { $0 ?? "" }
.filter { $0.count < 2 }
.map { _ in }
}
}
And the code in the view controller becomes:
self.searchTextField.rx.text
.resetResults(scheduler: MainScheduler.instance)
.subscribe(
onNext: { [weak self] in
self?.results = nil
}
)
.disposed(by: disposeBag)
Now, let's think about what we actually need to test here. For my part, I don't feel the need to test self?.results = nil or self.searchTextField.rx.text so the View controller can be ignored for testing.
So it's just a matter of testing the operator... There's a great article that recently came out: https://www.raywenderlich.com/7408-testing-your-rxswift-code However, frankly I don't see anything that needs testing here. I can trust that throttle, map and filter work as designed because they were tested in the RxSwift library and the closures passed in are so basic that I don't see any point in testing them either.
The problem is that self.ctrl.searchTextField.rx.text.onNext("e") won't trigger searchTextFieldObservable onNext subscription.
The subscription is also not triggered if you set the text value directly like this self.ctrl.searchTextField.text = "e".
The subscription will trigger (and your test should succeed) if you set the textField value like this: self.ctrl.searchTextField.insertText("e").
I think the reason for this is that UITextField.rx.text observes methods from UIKeyInput.
I prefer to keep UIViewControllers far away from my unit tests. Therefore, I suggest moving this logic to a view model.
As your bounty explanation details, basically what you are trying to do is mock the textField's text property, so that it fires events when you want it to. I would suggest replacing it with a mock value altogether. If you make textField.rx.text.bind(viewModel.query) the responsibility of the view controller, then you can focus on the view model for the unit test and manually alter the query variable as needed.
class ViewModel {
let query: Variable<String?> = Variable(nil)
let results: Variable<[TypeaheadResult]> = Variable([])
let disposeBag = DisposeBag()
init() {
query
.asObservable()
.flatMap { query in
return query.count >= 2 ? search(for: $0) : .just([])
}
.bind(results)
.disposed(by: disposeBag)
}
func search(query: String) -> Observable<[TypeaheadResult]> {
// ...
}
}
The test case:
class TypeaheadResultsViewControllerTests: XCTestCase {
func testManualChange() {
let viewModel = ViewModel()
viewModel.results.value = [/* .., .., .. */]
// this triggers the subscription, but does not trigger the search
viewModel.query.value = "1"
// assert the results list is empty
XCTAssertEqual(viewModel.results.value, [])
}
}
If you also want to test the connection between the textField and the view model, UI tests are a much better fit.
Note that this example omits:
Dependency injection of the network layer in the view model.
The binding of the view controller's textField value to query (i.e., textField.rx.text.asDriver().drive(viewModel.query)).
The observing of the results variable by the view controller (i.e., viewModel.results.asObservable.subscribe(/* ... */)).
There might be some typos in here, did not run it past the compiler.
If you look at the underlying implementation for rx.text, you'll see it relies on controlPropertyWithDefaultEvents which fires the following UIControl events: .allEditingEvents and .valueChanged.
Simply setting the text, it won't fire any events, so your observable is not triggered. You have to send an action explicitly:
textField.text = "Something"
textField.sendActions(for: .valueChanged) // or .allEditingEvents
If you are testing within a framework, sendActions won't work because the framework is missing the UIApplication. You can do this instead
extension UIControl {
func simulate(event: UIControl.Event) {
allTargets.forEach { target in
actions(forTarget: target, forControlEvent: event)?.forEach {
(target as NSObject).perform(Selector($0))
}
}
}
}
...
textField.text = "Something"
textField.simulate(event: .valueChanged) // or .allEditingEvents

How to test the UI Binding between RxSwift Variable and RxCocoa Observable?

I have a simple ViewModel with one property:
class ViewModel {
var name = Variable<String>("")
}
And I'm binding it to its UITextField in my ViewController:
class ViewController : UIViewController {
var viewModel : ViewModel!
#IBOutlet weak var nameField : UITextField!
private let disposeBag = DisposeBag()
override func viewDidLoad() {
super.viewDidLoad()
// Binding
nameField.rx.text
.orEmpty
.bind(to: viewModel.name)
.disposed(by: disposeBag)
}
}
Now, I want to Unit Test it.
I'm able to fill the nameField.text property though Rx using .onNext event - nameField.rx.text.onNext("My Name Here").
But the viewModel.name isn't being filled. Why? How can I make this Rx behavior work in my XCTestCase?
Please see below the result of my last test run:
I believe the issue you are having is that the binding is not explicitly scheduled on the main thread. I recommend using a Driver in this case:
class ViewModel {
var name = Variable<String>("")
}
class ViewController: UIViewController {
let textField = UITextField()
let disposeBag = DisposeBag()
let vm = ViewModel()
override func viewDidLoad() {
super.viewDidLoad()
textField.rx.text
.orEmpty
.asDriver()
.drive(vm.name)
.disposed(by: disposeBag)
}
// ..
With a Driver, the binding will always happen on the main thread and it will not error out.
Also, in your test I would call skip on the binding:
sut.nameTextField.rx.text.skip(1).onNext(name)
because the Driver will replay the first element when it subscribes.
Finally, I suggest using RxTest and a TestScheduler instead of GCD.
Let me know if this helps.
rx.text relies on the following UIControlEvents: .allEditingEvents and .valueChanged. Thus, explicitly send a onNext events will not send action for these event. You should send action manually.
sut.nameField.rx.text.onNext(name)
sut.nameField.sendActions(for: .valueChanged)

RxSwift: Two way binding

I used official two-way-binding solution
func <-> <T>(property: ControlProperty<T>, variable: Variable<T>) -> Disposable{
let bindToUIDisposable = variable.asObservable()
.bindTo(property)
let bindToVariable = property
.subscribe(onNext: { n in
variable.value = n
}, onCompleted: {
bindToUIDisposable.dispose()
})
return Disposables.create(bindToUIDisposable, bindToVariable)
}
Usage: (textField.rx.text <-> object.property).addDisposableTo(disposeBag)
Property definition: var property = Variable<String?>(nil)
In onNext method all ok and variable changed its value, but my object.property doesn't changed.
Is there any way to set variable current value into ControlProperty inside of <-> method, bcs I need set initial value, before subscribe starts?
Working in Swift 4
Example of two way binding between String & Textfield with MVVM architecture:
In ViewController:
#IBOutlet weak var emailTextfield: UITextField!
var viewModel : CCRegisterViewModel?
In ViewModel:
var email = Variable<String>("")
Use this code in ViewController
viewModel?.email.asObservable()
.bind(to: emailTextfield.rx.text)
.disposed(by: disposeBag)
emailTextfield.rx.text.orEmpty.bind(to:viewModel!.email)
.disposed(by: disposeBag)
My fault. I replaced object with another instance after binding
This code works well and control property receive initial value from variable

Resources