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 1 commit
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 whenResourceViewed_setResourcesViewed() = runTest {
hiroaki404 marked this conversation as resolved.
Show resolved Hide resolved
val collectJob = launch(UnconfinedTestDispatcher()) { viewModel.feedUiState.collect() }

// Give
hiroaki404 marked this conversation as resolved.
Show resolved Hide resolved
newsRepository.sendNewsResources(newsResourcesTestData)
userDataRepository.setNewsResourceBookmarked(newsResourcesTestData[0].id, true)
val itemBeforeViewed = viewModel.feedUiState.value
check(itemBeforeViewed is Success)
check(!itemBeforeViewed.feed.first().hasBeenViewed)
hiroaki404 marked this conversation as resolved.
Show resolved Hide resolved

// 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
hiroaki404 marked this conversation as resolved.
Show resolved Hide resolved
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())
hiroaki404 marked this conversation as resolved.
Show resolved Hide resolved

// When
viewModel.undoBookmarkRemoval()

// Then
assertFalse(viewModel.shouldDisplayUndoBookmark)
val item = viewModel.feedUiState.value
assertIs<Success>(item)
assertEquals(item.feed.size, 1)
hiroaki404 marked this conversation as resolved.
Show resolved Hide resolved

collectJob.cancel()
}
Expand Down