improve BookmarksViewModelTest coverage

Change-Id: Ide13abd9a125a337ba9c9fff2136221e16ebd46e
pull/1400/head
nosho.hiroaki 8 months ago committed by hiroaki404
parent 4a0723441f
commit d372d6ecec

@ -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,53 @@ 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 whenResourceViewed_setResourcesViewed() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
// Give
newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
val itemBeforeViewed = viewModel.feedUiState.value
check(itemBeforeViewed is Success)
check(!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)
collectJob.cancel()
}
@Test
fun whenUndoBookmarkRemoval_thenBookmarkIsRestored() = runTest {
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }
// Give
newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
viewModel.removeFromSavedResources(newsResourcesTestData[0].id)
check(viewModel.shouldDisplayUndoBookmark)
val itemBeforeUndo = viewModel.feedUiState.value
check(itemBeforeUndo is Success)
check(itemBeforeUndo.feed.isEmpty())
// When
viewModel.undoBookmarkRemoval()
// Then
assertFalse(viewModel.shouldDisplayUndoBookmark)
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertEquals(item.feed.size, 1)
collectJob.cancel()
} }
} }

Loading…
Cancel
Save