Merge pull request #1400 from hiroaki404/test/improve_bookmark_viewmodel_coverage

improve BookmarksViewModelTest coverage
pull/1238/merge
Don Turner 5 days ago committed by GitHub
commit 47f93efa19
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -31,7 +31,9 @@ import org.junit.Before
import org.junit.Rule import org.junit.Rule
import org.junit.Test import org.junit.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import kotlin.test.assertFalse
import kotlin.test.assertIs import kotlin.test.assertIs
import kotlin.test.assertTrue
/** /**
* To learn more about how this test handles Flows created with stateIn, see * To learn more about how this test handles Flows created with stateIn, see
@ -86,5 +88,49 @@ class BookmarksViewModelTest {
val item = viewModel.feedUiState.value val item = viewModel.feedUiState.value
assertIs<Success>(item) assertIs<Success>(item)
assertEquals(item.feed.size, 0) assertEquals(item.feed.size, 0)
assertTrue(viewModel.shouldDisplayUndoBookmark)
}
@Test
fun feedUiState_resourceIsViewed_setResourcesViewed() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
// Given
newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
val itemBeforeViewed = viewModel.feedUiState.value
assertIs<Success>(itemBeforeViewed)
assertFalse(itemBeforeViewed.feed.first().hasBeenViewed)
// When
viewModel.setNewsResourceViewed(newsResourcesTestData[0].id, true)
// Then
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertTrue(item.feed.first().hasBeenViewed)
}
@Test
fun feedUiState_undoneBookmarkRemoval_bookmarkIsRestored() = runTest {
backgroundScope.launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
// Given
newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
viewModel.removeFromSavedResources(newsResourcesTestData[0].id)
assertTrue(viewModel.shouldDisplayUndoBookmark)
val itemBeforeUndo = viewModel.feedUiState.value
assertIs<Success>(itemBeforeUndo)
assertEquals(0, itemBeforeUndo.feed.size)
// When
viewModel.undoBookmarkRemoval()
// Then
assertFalse(viewModel.shouldDisplayUndoBookmark)
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertEquals(1, item.feed.size)
} }
} }

Loading…
Cancel
Save