Skip to content

Commit

Permalink
Update tests to be more stable with scoping
Browse files Browse the repository at this point in the history
  • Loading branch information
gumil committed Jan 23, 2019
1 parent 88dec30 commit b152fb7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ import io.gumil.kaskade.Kaskade
import io.gumil.kaskade.State
import io.gumil.kaskade.livedata.stateDamLiveData
import kotlinx.android.parcel.Parcelize
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job

internal class DogViewModel(
private val dogApi: RandomDogApi,
dispatcher: CoroutineDispatcher = Dispatchers.Main
scope: CoroutineScope? = null
) : ViewModel() {

constructor() : this(ApiFactory.create())

private val job = Job()
private val job = scope?.coroutineContext?.get(Job) ?: Job()

private val uiScope = CoroutineScope(dispatcher + job)
private val uiScope = scope ?: CoroutineScope(Dispatchers.Main + job)

private lateinit var kaskade: Kaskade<DogAction, DogState>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import io.mockk.every
import io.mockk.mockk
import io.mockk.verify
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import org.junit.Before
import kotlinx.coroutines.runBlocking
import org.junit.Rule
import org.junit.Test

Expand All @@ -29,52 +28,56 @@ internal class DogViewModelTest {
every { getDog() } returns mockDeferredDog
}

private lateinit var viewModel: DogViewModel

private val mockObserver = mockk<Observer<DogState>>(relaxed = true)

@Before
fun setUp() {
viewModel = DogViewModel(mockApi, Dispatchers.Default)
viewModel.restore()
viewModel.state.observeForever(mockObserver)
verify(exactly = 1) { mockObserver.onChanged(DogState.Loading) }
verify(exactly = 1) { mockObserver.onChanged(DogState.OnLoaded(mockUrl)) }
}

@Test
fun `process Refresh should emit Loading and OnLoaded states`() {
viewModel.process(DogAction.Refresh)

val mockObserver = mockk<Observer<DogState>>(relaxed = true)
runBlocking {
val viewModel = DogViewModel(mockApi, this)
viewModel.restore()
viewModel.state.observeForever(mockObserver)
viewModel.process(DogAction.Refresh)
}
verify(exactly = 2) { mockObserver.onChanged(DogState.Loading) }
verify(exactly = 2) { mockObserver.onChanged(DogState.OnLoaded(mockUrl)) }
}

@Test
fun `process OnError action should emit Error state`() {
val mockObserver = mockk<Observer<DogState>>(relaxed = true)
val exception = Exception()

viewModel.process(DogAction.OnError(exception))

runBlocking {
val viewModel = DogViewModel(mockApi, this)
viewModel.restore()
viewModel.state.observeForever(mockObserver)
viewModel.process(DogAction.OnError(exception))
}
verify(exactly = 1) { mockObserver.onChanged(DogState.Loading) }
verify(exactly = 1) { mockObserver.onChanged(DogState.OnLoaded(mockUrl)) }
verify(exactly = 1) { mockObserver.onChanged(DogState.Error(exception)) }
}

@Test
fun `process GetDog action should emit OnLoaded state`() {
viewModel.process(DogAction.GetDog)

val mockObserver = mockk<Observer<DogState>>(relaxed = true)
runBlocking {
val viewModel = DogViewModel(mockApi, this)
viewModel.restore()
viewModel.state.observeForever(mockObserver)
viewModel.process(DogAction.GetDog)
}
verify(exactly = 1) { mockObserver.onChanged(DogState.Loading) }
verify(exactly = 2) { mockObserver.onChanged(DogState.OnLoaded(mockUrl)) }
}

@Test
fun `restore with state should only emit passed state`() {
val viewModel = DogViewModel(mockApi, Dispatchers.Default)
val mockObserver = mockk<Observer<DogState>>(relaxed = true)

val state = DogState.OnLoaded(mockUrl)
viewModel.restore(state)
viewModel.state.observeForever(mockObserver)

runBlocking {
val viewModel = DogViewModel(mockApi, this)
viewModel.restore(state)
viewModel.state.observeForever(mockObserver)
}
verify(exactly = 0) { mockObserver.onChanged(DogState.Loading) }
verify(exactly = 1) { mockObserver.onChanged(state) }
}
Expand Down

0 comments on commit b152fb7

Please sign in to comment.