Skip to content

Instantly share code, notes, and snippets.

@chao2zhang
Created June 23, 2021 06:29
Show Gist options
  • Save chao2zhang/7d3e83024b0b909dbc310ea8e336462b to your computer and use it in GitHub Desktop.
Save chao2zhang/7d3e83024b0b909dbc310ea8e336462b to your computer and use it in GitHub Desktop.
Test reexcution
@Test
fun testReexecution() = runBlockingTest {
val source = MutableStateFlow(1)
val liveData = source.flatMapConcat {
flowOf(it, it * 10) // Could be actual network request
}.asLiveData()
val observer = object : Observer<Int> {
var observeHistory = mutableListOf<Int>()
override fun onChanged(t: Int) {
observeHistory.add(t)
}
}
// User navigates to the current page
liveData.observeForever(observer)
// User leaves the page
liveData.removeObserver(observer)
assertEquals(listOf(1, 10), observer.observeHistory)
// User comes back to the page after 5 seconds
testDispatcher.advanceTimeBy(5001L)
liveData.observeForever(observer)
liveData.removeObserver(observer)
assertEquals(listOf(1, 10, 1, 10), observer.observeHistory)
// Clean up
testDispatcher.advanceUntilIdle()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment