s3rius
04/19/2023, 8:47 PMPager
. I have a ViewModel
that wants to listen to the pager's current index, so I hoisted the PagerState
into the ViewModel
and observe it via snapshotFlow
. Basically this:
class VM {
val pagerState = PagerState(initialPage = 0)
private val currentIndex = snapshowFlow { pagerState.currentPage }
// do something with currentIndex
}
This works just fine.
Now I'm trying to unit test my `ViewModel`:
fun test() = runTest {
val vm = createVm()
vm.pagerState.scrollTo(n) // <- blocks indefinitely
// assert that vm does the correct thing
}
Until now I had no trouble testing Compose State classes, but it seems PagerState
has some internal mechanism that waits for a layout to happen before scrollTo
finishes. Since this isn't happening in my test, the function blocks indefinitely.
I could handle the current-page logic entirely myself but I would end up having to synchronize with the PagerState and end up with two sources of truth.
Any ideas?jossiwolf
04/19/2023, 9:14 PMmattinger
04/19/2023, 9:37 PMs3rius
04/19/2023, 9:52 PMlevima
04/21/2023, 8:05 AM