diff --git a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt index 19ab0ad85..aa42adae2 100644 --- a/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt +++ b/feature/bookmarks/src/test/kotlin/com/google/samples/apps/nowinandroid/feature/bookmarks/BookmarksViewModelTest.kt @@ -31,7 +31,9 @@ import org.junit.Before import org.junit.Rule import org.junit.Test import kotlin.test.assertEquals +import kotlin.test.assertFalse import kotlin.test.assertIs +import kotlin.test.assertTrue /** * To learn more about how this test handles Flows created with stateIn, see @@ -86,5 +88,49 @@ class BookmarksViewModelTest { val item = viewModel.feedUiState.value assertIs(item) 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(itemBeforeViewed) + assertFalse(itemBeforeViewed.feed.first().hasBeenViewed) + + // When + viewModel.setNewsResourceViewed(newsResourcesTestData[0].id, true) + + // Then + val item = viewModel.feedUiState.value + assertIs(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(itemBeforeUndo) + assertEquals(0, itemBeforeUndo.feed.size) + + // When + viewModel.undoBookmarkRemoval() + + // Then + assertFalse(viewModel.shouldDisplayUndoBookmark) + val item = viewModel.feedUiState.value + assertIs(item) + assertEquals(1, item.feed.size) } }