diff --git a/WordPress/src/test/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModelTest.kt b/WordPress/src/test/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModelTest.kt index 24e429460212..98939094fa6d 100644 --- a/WordPress/src/test/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModelTest.kt +++ b/WordPress/src/test/java/org/wordpress/android/ui/voicetocontent/VoiceToContentViewModelTest.kt @@ -1,19 +1,21 @@ package org.wordpress.android.ui.voicetocontent +import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.flow.flow +import kotlinx.coroutines.flow.toList +import kotlinx.coroutines.launch import org.junit.Before -import org.junit.runner.RunWith import org.mockito.Mock -import org.mockito.junit.MockitoJUnitRunner import org.mockito.kotlin.verifyNoInteractions import org.mockito.kotlin.whenever import org.wordpress.android.BaseUnitTest import org.wordpress.android.ui.mysite.SelectedSiteRepository +import org.wordpress.android.util.audio.RecordingUpdate import org.wordpress.android.viewmodel.ContextProvider import kotlin.test.Test @ExperimentalCoroutinesApi -@RunWith(MockitoJUnitRunner::class) class VoiceToContentViewModelTest : BaseUnitTest() { @Mock lateinit var voiceToContentFeatureUtils: VoiceToContentFeatureUtils @@ -35,8 +37,22 @@ class VoiceToContentViewModelTest : BaseUnitTest() { private lateinit var viewModel: VoiceToContentViewModel - // private lateinit var uiState: MutableList + private var uiStateChanges = mutableListOf() + private val uiState + get() = viewModel.state.value + private fun testUiStateChanges( + block: suspend CoroutineScope.() -> T + ) { + test { + uiStateChanges.clear() + val job = launch(testDispatcher()) { + viewModel.state.toList(uiStateChanges) + } + this.block() + job.cancel() + } + } /* private val jetpackAIAssistantFeature = JetpackAIAssistantFeature( hasFeature = true, isOverLimit = false, @@ -55,7 +71,7 @@ class VoiceToContentViewModelTest : BaseUnitTest() { @Before fun setup() { // Mock the recording updates to return a non-null flow before ViewModel instantiation - // whenever(recordingUseCase.recordingUpdates()).thenReturn(createRecordingUpdateFlow()) + whenever(recordingUseCase.recordingUpdates()).thenReturn(createRecordingUpdateFlow()) viewModel = VoiceToContentViewModel( testDispatcher(), @@ -66,20 +82,13 @@ class VoiceToContentViewModelTest : BaseUnitTest() { contextProvider, prepareVoiceToContentUseCase ) -// uiState = mutableListOf() -// viewModel.uiState.observeForever { event -> -// event?.let { result -> -// uiState.add(result) -// } -// } } - // -// // Helper function to create a consistent flow -// private fun createRecordingUpdateFlow() = flow { -// emit(RecordingUpdate(0, 0, false)) -// } -// + // Helper function to create a consistent flow + private fun createRecordingUpdateFlow() = flow { + emit(RecordingUpdate(0, 0, false)) + } + @Test fun `when site is null, then execute posts error state `() = test { whenever(selectedSiteRepository.getSelectedSite()).thenReturn(null)