|
| 1 | +package dev.shorthouse.coinwatch.ui.component |
| 2 | + |
| 3 | +import androidx.compose.ui.test.assertHasClickAction |
| 4 | +import androidx.compose.ui.test.assertIsDisplayed |
| 5 | +import androidx.compose.ui.test.junit4.createComposeRule |
| 6 | +import androidx.compose.ui.test.onNodeWithContentDescription |
| 7 | +import androidx.compose.ui.test.onNodeWithText |
| 8 | +import androidx.compose.ui.test.performClick |
| 9 | +import com.google.common.truth.Truth.assertThat |
| 10 | +import dev.shorthouse.coinwatch.ui.theme.AppTheme |
| 11 | +import org.junit.Rule |
| 12 | +import org.junit.Test |
| 13 | + |
| 14 | +class ErrorStateTest { |
| 15 | + |
| 16 | + @get:Rule |
| 17 | + val composeTestRule = createComposeRule() |
| 18 | + |
| 19 | + @Test |
| 20 | + fun when_displayingBaseErrorState_should_displayExpectedComponents() { |
| 21 | + composeTestRule.setContent { |
| 22 | + AppTheme { |
| 23 | + ErrorState( |
| 24 | + message = null, |
| 25 | + onRetry = {} |
| 26 | + ) |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + composeTestRule.apply { |
| 31 | + onNodeWithContentDescription("Error").assertIsDisplayed() |
| 32 | + onNodeWithText("An error has occurred").assertIsDisplayed() |
| 33 | + onNodeWithText("Retry").assertIsDisplayed().assertHasClickAction() |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + fun when_messageIsProvided_should_displayMessage() { |
| 39 | + composeTestRule.setContent { |
| 40 | + AppTheme { |
| 41 | + ErrorState( |
| 42 | + message = "Error message", |
| 43 | + onRetry = {} |
| 44 | + ) |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + composeTestRule.apply { |
| 49 | + onNodeWithText("Error message").assertIsDisplayed() |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun when_retryClicked_should_callOnRetry() { |
| 55 | + var onRetryCalled = false |
| 56 | + |
| 57 | + composeTestRule.setContent { |
| 58 | + AppTheme { |
| 59 | + ErrorState( |
| 60 | + message = null, |
| 61 | + onRetry = { onRetryCalled = true } |
| 62 | + ) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + composeTestRule.apply { |
| 67 | + onNodeWithText("Retry").performClick() |
| 68 | + assertThat(onRetryCalled).isTrue() |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + fun when_navigateUpProvided_should_displayBackButton() { |
| 74 | + composeTestRule.setContent { |
| 75 | + AppTheme { |
| 76 | + ErrorState( |
| 77 | + message = null, |
| 78 | + onRetry = {}, |
| 79 | + onNavigateUp = {} |
| 80 | + ) |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + composeTestRule.apply { |
| 85 | + onNodeWithContentDescription("Back").assertIsDisplayed().assertHasClickAction() |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + fun when_navigateUpClicked_should_callOnNavigateUp() { |
| 91 | + var onNavigateUpCalled = false |
| 92 | + |
| 93 | + composeTestRule.setContent { |
| 94 | + AppTheme { |
| 95 | + ErrorState( |
| 96 | + message = null, |
| 97 | + onRetry = {}, |
| 98 | + onNavigateUp = { onNavigateUpCalled = true } |
| 99 | + ) |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + composeTestRule.apply { |
| 104 | + onNodeWithContentDescription("Back").performClick() |
| 105 | + assertThat(onNavigateUpCalled).isTrue() |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments