Skip to content

Commit

Permalink
Adding missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rygelouv committed Feb 19, 2024
1 parent a5d3532 commit 34d2ef6
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ class SummaryDetailsViewModel @Inject constructor(
}

fun onReadClick() {
postEvent(SummaryDetailsUiEvent.NavigateTo.ToReadSummary(summary.id))
// shouldShowAuthSuggestionOrProceed {}
shouldShowAuthSuggestionOrProceed {
postEvent(SummaryDetailsUiEvent.NavigateTo.ToReadSummary(summary.id))
}
}

Check warning on line 170 in app/src/main/java/app/books/tanga/feature/summary/details/SummaryDetailsViewModel.kt

View check run for this annotation

Codecov / codecov/patch

app/src/main/java/app/books/tanga/feature/summary/details/SummaryDetailsViewModel.kt#L169-L170

Added lines #L169 - L170 were not covered by tests

private fun shouldShowAuthSuggestionOrProceed(action: () -> Unit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,4 +183,40 @@ class SummaryDetailsViewModelTest {
cancelAndConsumeRemainingEvents()
}
}

@Test
fun onReadClick_withSession() = runTest {
val summaryId = SummaryId("1")
val summary = Fixtures.dummySummary1

coEvery { summaryInteractor.getSummary(summaryId) } returns Result.success(summary)
coEvery { favoriteInteractor.deleteFavoriteBySummaryId(summaryId) } returns Result.success(Unit)
coEvery { favoriteInteractor.isFavorite(summaryId) } returns Result.success(true) // Set as favorite
coEvery { summaryInteractor.getRecommendationsForSummary(any()) } returns Result.success(emptyList())

coEvery { sessionManager.hasSession() } returns true

viewModel.loadSummary(summaryId)

viewModel.onReadClick()

viewModel.events.test {
val event = expectMostRecentItem()
Assertions.assertTrue(event is SummaryDetailsUiEvent.NavigateTo.ToReadSummary)
cancelAndConsumeRemainingEvents()
}
}

@Test
fun onReadClick_whenSessionManagerHasNoSession() = runTest {
coEvery { sessionManager.hasSession() } returns false

viewModel.onReadClick()

viewModel.events.test {
val event = expectMostRecentItem()
Assertions.assertTrue(event is SummaryDetailsUiEvent.ShowAuthSuggestion)
cancelAndConsumeRemainingEvents()
}
}
}
29 changes: 29 additions & 0 deletions core-ui/src/androidTest/java/app/books/tanga/coreui/ButtonsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import androidx.compose.ui.test.junit4.createComposeRule
import androidx.compose.ui.test.onNodeWithTag
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import app.books.tanga.coreui.components.Button
import app.books.tanga.coreui.components.SearchButton
import app.books.tanga.coreui.components.SummaryActionButton
import app.books.tanga.coreui.components.TangaButton
import app.books.tanga.coreui.components.TangaButtonLeftIcon
import app.books.tanga.coreui.components.TangaButtonRightIcon
import app.books.tanga.coreui.components.TangaFloatingActionButton
import app.books.tanga.coreui.components.TangaLinedButton
import app.books.tanga.coreui.components.TangaPlayAudioFab
import app.books.tanga.coreui.resources.TextResource
import junit.framework.TestCase.assertFalse
import org.junit.Rule
import org.junit.Test
Expand Down Expand Up @@ -106,4 +110,29 @@ class ButtonsTest {
composeTestRule.onNodeWithText("Search").performClick()
assert(clicked)
}

@Test
fun tangaPlayAudioFab_handlesClick() {
var clicked = false
composeTestRule.setContent {
TangaPlayAudioFab(onNavigateToAudioPlayer = { clicked = true })
}

composeTestRule.onNodeWithTag("fab_button").performClick()

assert(clicked)
}

@Test
fun tangaFloatingActionButton_handlesClick() {
var clicked = false
val button = Button(text = TextResource.fromText("Button"), onClick = { clicked = true })
composeTestRule.setContent {
TangaFloatingActionButton(button = button)
}

composeTestRule.onNodeWithTag("fab_button").performClick()

assert(clicked)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ fun TangaFloatingActionButton(
modifier: Modifier = Modifier
) {
ExtendedFloatingActionButton(
modifier = modifier,
modifier = modifier.testTag("fab_button"),
onClick = { button.onClick() },
containerColor = MaterialTheme.colorScheme.tertiary,
shape = RoundedCornerShape(48.dp),
Expand Down

0 comments on commit 34d2ef6

Please sign in to comment.