Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve BookmarksViewModelTest coverage #1400

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -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
Expand Down Expand Up @@ -88,6 +90,54 @@ class BookmarksViewModelTest {
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertEquals(item.feed.size, 0)
assertTrue(viewModel.shouldDisplayUndoBookmark)

collectJob.cancel()
}

@Test
fun feedUiState_resourceIsViewed_setResourcesViewed() = runTest {
val collectJob = 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)

collectJob.cancel()
}

@Test
fun feedUiState_undoneBookmarkRemoval_bookmarkIsRestored() = runTest {
val collectJob = 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)

collectJob.cancel()
}
Expand Down