11package com.automattic.simplenote.viewmodels
22
33import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+ import com.automattic.simplenote.CoroutineTestRule
45import com.automattic.simplenote.repositories.CollaboratorsActionResult
56import com.automattic.simplenote.repositories.CollaboratorsRepository
67import com.automattic.simplenote.viewmodels.CollaboratorsViewModel.Event
78import com.automattic.simplenote.viewmodels.CollaboratorsViewModel.UiState
89import kotlinx.coroutines.ExperimentalCoroutinesApi
910import kotlinx.coroutines.flow.flow
10- import kotlinx.coroutines.test.runBlockingTest
11+ import kotlinx.coroutines.test.UnconfinedTestDispatcher
12+ import kotlinx.coroutines.test.runTest
1113import org.junit.Assert.assertEquals
1214import org.junit.Before
1315import org.junit.Rule
@@ -19,29 +21,30 @@ import org.mockito.kotlin.whenever
1921class CollaboratorsViewModelTest {
2022 @get:Rule
2123 val rule = InstantTaskExecutorRule ()
24+ @get:Rule
25+ val coroutinesTestRule = CoroutineTestRule (UnconfinedTestDispatcher ())
2226
2327 private val mockCollaboratorsRepository = mock(CollaboratorsRepository ::class .java)
2428 private val viewModel = CollaboratorsViewModel (mockCollaboratorsRepository)
2529
2630 private val noteId = " key1"
2731
28-
2932 @Before
30- fun setup () = runBlockingTest {
33+ fun setup () = runTest {
3134 whenever(mockCollaboratorsRepository.getCollaborators(noteId))
3235 .thenReturn(
CollaboratorsActionResult .
CollaboratorsList (
listOf (
" [email protected] " ,
" [email protected] " )))
3336 }
3437
3538 @Test
36- fun loadCollaboratorsShouldUpdateUiStateWithList () = runBlockingTest {
39+ fun loadCollaboratorsShouldUpdateUiStateWithList () = runTest {
3740 viewModel.loadCollaborators(noteId)
3841
3942 val expectedCollaborators
= UiState .
CollaboratorsList (
listOf (
" [email protected] " ,
" [email protected] " ))
4043 assertEquals(expectedCollaborators, viewModel.uiState.value)
4144 }
4245
4346 @Test
44- fun loadEmptyCollaboratorsShouldUpdateUiStateWithEmpty () = runBlockingTest {
47+ fun loadEmptyCollaboratorsShouldUpdateUiStateWithEmpty () = runTest {
4548 whenever(mockCollaboratorsRepository.getCollaborators(noteId))
4649 .thenReturn(CollaboratorsActionResult .CollaboratorsList (emptyList()))
4750
@@ -51,7 +54,7 @@ class CollaboratorsViewModelTest {
5154 }
5255
5356 @Test
54- fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteInTrash () = runBlockingTest {
57+ fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteInTrash () = runTest {
5558 whenever(mockCollaboratorsRepository.getCollaborators(noteId))
5659 .thenReturn(CollaboratorsActionResult .NoteInTrash )
5760
@@ -61,7 +64,7 @@ class CollaboratorsViewModelTest {
6164 }
6265
6366 @Test
64- fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteDeleted () = runBlockingTest {
67+ fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteDeleted () = runTest {
6568 whenever(mockCollaboratorsRepository.getCollaborators(noteId))
6669 .thenReturn(CollaboratorsActionResult .NoteDeleted )
6770
@@ -71,7 +74,7 @@ class CollaboratorsViewModelTest {
7174 }
7275
7376 @Test
74- fun removeCollaboratorShouldReturnListEmails () = runBlockingTest {
77+ fun removeCollaboratorShouldReturnListEmails () = runTest {
7578 viewModel.loadCollaborators(noteId)
7679 whenever(mockCollaboratorsRepository.removeCollaborator(noteId,
" [email protected] " ))
7780 .thenReturn(
CollaboratorsActionResult .
CollaboratorsList (
listOf (
" [email protected] " )))
@@ -83,7 +86,7 @@ class CollaboratorsViewModelTest {
8386 }
8487
8588 @Test
86- fun removeLastCollaboratorShouldReturnEmpty () = runBlockingTest {
89+ fun removeLastCollaboratorShouldReturnEmpty () = runTest {
8790 viewModel.loadCollaborators(noteId)
8891 whenever(mockCollaboratorsRepository.removeCollaborator(noteId,
" [email protected] " ))
8992 .thenReturn(CollaboratorsActionResult .CollaboratorsList (emptyList()))
@@ -94,7 +97,7 @@ class CollaboratorsViewModelTest {
9497 }
9598
9699 @Test
97- fun removeCollaboratorForNoteInTrashShouldTriggerEvent () = runBlockingTest {
100+ fun removeCollaboratorForNoteInTrashShouldTriggerEvent () = runTest {
98101 viewModel.loadCollaborators(noteId)
99102 whenever(mockCollaboratorsRepository.removeCollaborator(noteId,
" [email protected] " ))
100103 .thenReturn(CollaboratorsActionResult .NoteInTrash )
@@ -105,7 +108,7 @@ class CollaboratorsViewModelTest {
105108 }
106109
107110 @Test
108- fun removeCollaboratorForNoteDeletedShouldTriggerEvent () = runBlockingTest {
111+ fun removeCollaboratorForNoteDeletedShouldTriggerEvent () = runTest {
109112 viewModel.loadCollaborators(noteId)
110113 whenever(mockCollaboratorsRepository.removeCollaborator(noteId,
" [email protected] " ))
111114 .thenReturn(CollaboratorsActionResult .NoteDeleted )
@@ -140,7 +143,7 @@ class CollaboratorsViewModelTest {
140143 }
141144
142145 @Test
143- fun collaboratorAddedAfterStoppedListeningChangesShouldNotUpdateUiState () = runBlockingTest {
146+ fun collaboratorAddedAfterStoppedListeningChangesShouldNotUpdateUiState () = runTest {
144147 viewModel.loadCollaborators(noteId)
145148 viewModel.startListeningChanges()
146149 viewModel.stopListeningChanges()
@@ -154,7 +157,7 @@ class CollaboratorsViewModelTest {
154157 }
155158
156159 @Test
157- fun collaboratorAddedShouldUpdateUiState () = runBlockingTest {
160+ fun collaboratorAddedShouldUpdateUiState () = runTest {
158161 viewModel.loadCollaborators(noteId)
159162 whenever(mockCollaboratorsRepository.collaboratorsChanged(noteId)).thenReturn(flow { emit(true ) })
160163
0 commit comments