Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added completable execute function #170

Merged
merged 3 commits into from
Jan 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions mvrx/src/main/kotlin/com/airbnb/mvrx/BaseMvRxViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ abstract class BaseMvRxViewModel<S : MvRxState>(
stateReducer: S.(Async<T>) -> S
) = execute({ it }, null, stateReducer)

/**
* Helper to map a Completable to an Async property on the state object.
*/
fun Completable.execute(
stateReducer: S.(Async<Unit>) -> S
) = toSingle { Unit }.execute(stateReducer)

/**
* Execute an observable and wrap its progression with AsyncData reduced to the global state.
*
Expand Down
49 changes: 49 additions & 0 deletions mvrx/src/test/kotlin/com/airbnb/mvrx/ViewModelSubscriberTest.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.airbnb.mvrx

import android.arch.lifecycle.Lifecycle
import io.reactivex.Completable
import io.reactivex.Maybe
import io.reactivex.Observable
import io.reactivex.Single
Expand All @@ -16,7 +17,10 @@ data class ViewModelTestState(
val bar: Int = 0,
val bam: Int = 0,
val list: List<Int> = emptyList(),
// for Single and Observable tests
val async: Async<String> = Uninitialized,
// for Completable tests
val asyncUnit: Async<Unit> = Uninitialized,
val prop6: Int = 0,
val prop7: Int = 0
) : MvRxState
Expand Down Expand Up @@ -60,6 +64,41 @@ class ViewModelTestViewModel(initialState: ViewModelTestState) : TestMvRxViewMod
onCleared()
}

fun testCompletableSuccess() {
var callCount = 0
selectSubscribe(ViewModelTestState::asyncUnit) {
callCount++
assertEquals(when (callCount) {
1 -> Uninitialized
2 -> Loading()
3 -> Success(Unit)
else -> throw IllegalArgumentException("Unexpected call count $callCount")
}, it)
}
Completable.create { emitter ->
emitter.onComplete()
}.execute { copy(asyncUnit = it) }
assertEquals(3, callCount)
}

fun testCompletableFail() {
var callCount = 0
val error = IllegalStateException("Fail")
selectSubscribe(ViewModelTestState::asyncUnit) {
callCount++
assertEquals(when (callCount) {
1 -> Uninitialized
2 -> Loading()
3 -> Fail<Unit>(error)
else -> throw IllegalArgumentException("Unexpected call count $callCount")
}, it)
}
Completable.create {
throw error
}.execute { copy(asyncUnit = it) }
assertEquals(3, callCount)
}

fun testSingleSuccess() {
var callCount = 0
selectSubscribe(ViewModelTestState::async) {
Expand Down Expand Up @@ -408,6 +447,16 @@ class ViewModelSubscriberTest : BaseTest() {
assertTrue(disposable.isDisposed)
}

@Test
fun testCompletableSuccess() {
viewModel.testCompletableSuccess()
}

@Test
fun testCompletableFail() {
viewModel.testCompletableFail()
}

@Test
fun testSingleSuccess() {
viewModel.testSingleSuccess()
Expand Down