Created
June 23, 2021 06:29
-
-
Save chao2zhang/7d3e83024b0b909dbc310ea8e336462b to your computer and use it in GitHub Desktop.
Test reexcution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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