Skip to content

Commit 6059a9e

Browse files
committed
Merge branch 'component-ui-tests' into main
2 parents 1e7dca2 + 8549b13 commit 6059a9e

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
}

app/src/main/java/dev/shorthouse/coinwatch/ui/component/ErrorState.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ fun ErrorState(
7575
) {
7676
Image(
7777
painter = painterResource(R.drawable.error_state),
78-
contentDescription = null,
78+
contentDescription = stringResource(R.string.cd_error_state),
7979
modifier = Modifier.size(250.dp)
8080
)
8181

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,5 @@
5050
<string name="cant_find_coin_prompt">Can\'t find the coin you\'re looking for?</string>
5151
<string name="search_coin_prompt">Try using the search function!</string>
5252
<string name="cd_list_scroll_top">Scroll to top</string>
53+
<string name="cd_error_state">Error</string>
5354
</resources>

0 commit comments

Comments
 (0)