Skip to content

Instantly share code, notes, and snippets.

@raulh82vlc
Last active October 31, 2020 15:38
Show Gist options
  • Save raulh82vlc/c69a37d3a0d1da1e3f7edd432fb9e9f5 to your computer and use it in GitHub Desktop.
Save raulh82vlc/c69a37d3a0d1da1e3f7edd432fb9e9f5 to your computer and use it in GitHub Desktop.
StateFlowHandlerTest with a couple of tests cases that pass correctly for StateFlow
/*
* Copyright (C) 2020 Raul Hernandez Lopez
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.raulh82vlc.tweetsearch.search.ui
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.verify
import com.nhaarman.mockitokotlin2.whenever
import com.raulh82vlc.tweetsearch.common.threading.TaskThreading
import com.raulh82vlc.tweetsearch.search.addTweets
import com.raulh82vlc.tweetsearch.search.data.datasource.db.model.Tweet
import com.raulh82vlc.tweetsearch.search.data.datasource.db.model.TweetsUIState
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.test.TestCoroutineDispatcher
import org.junit.After
import org.junit.Before
import org.junit.Test
@ExperimentalCoroutinesApi
class StateFlowHandlerTest {
private val testDispatcher = TestCoroutineDispatcher()
private val taskThreading: TaskThreading = mock()
private val stateFlow = MutableStateFlow<TweetsUIState?>(null)
private val fragment: SearchTweetFragment = mock()
private lateinit var stateFlowHandler: StateFlowHandler
private fun getStateFlow(): StateFlow<TweetsUIState?> = stateFlow
@Before
fun before() {
whenever(taskThreading.ui()).thenReturn(testDispatcher)
stateFlowHandler = StateFlowHandler(taskThreading)
}
@After
fun tearDown() {
testDispatcher.cleanupTestCoroutines()
}
@Test
fun `show Loading UI state`() = runBlockingTest {
// set value for stateflow with a loading state
stateFlow.value = TweetsUIState.LoadingUIState
// process state collection
stateFlowHandler.processStateCollection(getStateFlow(), fragment)
// verify loader shows
verify(fragment).showLoader()
}
@Test
fun `show List results UI state`() = runBlockingTest {
// set value for stateFlow with a state with tweets
val tweets: List<Tweet> = addTweets()
stateFlow.value = TweetsUIState.ListResultsUIState(tweets)
// process state collection for a filled list
stateFlowHandler.processStateCollection(getStateFlow(), fragment)
// verify list is updated and shows, rest is hidden
verify(fragment).hideLoader()
verify(fragment).hideError()
verify(fragment).showList()
verify(fragment).updateList(tweets)
verify(fragment).showAnimation()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment