package eu.friedcutlet.yokan.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.ViewModelStore import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.UnconfinedTestDispatcher import kotlinx.coroutines.test.resetMain import kotlinx.coroutines.test.setMain /** Installs an unconfined Main dispatcher and owns created view models; [close] clears them and restores Main. */ @OptIn(ExperimentalCoroutinesApi::class) internal class ViewModelTestHost { val dispatcher = UnconfinedTestDispatcher() private val store = ViewModelStore() private var count = 0 init { Dispatchers.setMain(dispatcher) } fun create(init: () -> T): T = init().also { store.put("vm${count++}", it) } fun close() { store.clear() Dispatchers.resetMain() } }