Skip to content

Commit 71cf3f9

Browse files
author
C706049
committed
test: updated LoginScreenTest to use semantics instead of tags
1 parent 5726b32 commit 71cf3f9

File tree

2 files changed

+16
-17
lines changed

2 files changed

+16
-17
lines changed

app/src/main/java/com/monstarlab/features/login/ui/LoginScreen.kt

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import androidx.compose.material.Scaffold
1313
import androidx.compose.runtime.Composable
1414
import androidx.compose.ui.Alignment
1515
import androidx.compose.ui.Modifier
16-
import androidx.compose.ui.platform.testTag
1716
import androidx.compose.ui.text.input.PasswordVisualTransformation
1817
import com.monstarlab.core.error.displayableMessage
1918
import com.monstarlab.core.ui.previews.LightDarkPreview
@@ -46,9 +45,8 @@ fun LoginScreen(state: LoginState = LoginState(), actions: LoginActions = LoginA
4645
value = state.email,
4746
onValueChange = actions.onEmailChange,
4847
placeholder = "E-Mail",
49-
modifier = Modifier
50-
.fillMaxWidth()
51-
.testTag("email"),
48+
label = "E-Mail",
49+
modifier = Modifier.fillMaxWidth()
5250
)
5351

5452
Spacer(modifier = Modifier.size(Theme.dimensions.medium3))
@@ -59,19 +57,17 @@ fun LoginScreen(state: LoginState = LoginState(), actions: LoginActions = LoginA
5957
error = state.error?.displayableMessage,
6058
visualTransformation = PasswordVisualTransformation(),
6159
placeholder = "Password",
62-
modifier = Modifier
63-
.fillMaxWidth()
64-
.testTag("password"),
60+
label = "Password",
61+
modifier = Modifier.fillMaxWidth(),
6562
)
63+
6664
Spacer(modifier = Modifier.size(Theme.dimensions.medium3))
6765

6866
AppButton(
6967
text = "Login",
7068
onClick = actions.onLoginClick,
7169
isLoading = state.isLoading,
72-
modifier = Modifier
73-
.fillMaxWidth()
74-
.testTag("login"),
70+
modifier = Modifier.fillMaxWidth(),
7571
)
7672
}
7773
}

app/src/test/java/com/monstarlab/features/login/ui/LoginScreenTest.kt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ package com.monstarlab.features.login.ui
33
import androidx.compose.ui.test.assert
44
import androidx.compose.ui.test.assertIsDisplayed
55
import androidx.compose.ui.test.assertIsEnabled
6+
import androidx.compose.ui.test.hasClickAction
67
import androidx.compose.ui.test.hasText
78
import androidx.compose.ui.test.junit4.createComposeRule
8-
import androidx.compose.ui.test.onNodeWithTag
9+
import androidx.compose.ui.test.onNodeWithText
910
import androidx.compose.ui.test.performClick
1011
import androidx.compose.ui.test.performTextReplacement
1112
import androidx.test.ext.junit.runners.AndroidJUnit4
@@ -44,18 +45,18 @@ class LoginScreenTest {
4445
@Test
4546
fun testInitialState() {
4647
composeTestRule
47-
.onNodeWithTag("email")
48+
.onNodeWithText("E-Mail")
4849
.assertIsEnabled()
4950
.assertIsDisplayed()
5051
.assert(hasText("eve.holt@reqres.in"))
5152

5253
composeTestRule
53-
.onNodeWithTag("password")
54+
.onNodeWithText("Password")
5455
.assertIsEnabled()
5556
.assertIsDisplayed()
5657

5758
composeTestRule
58-
.onNodeWithTag("login")
59+
.onNode(hasText("Login") and hasClickAction())
5960
.assertIsEnabled()
6061
.assertIsDisplayed()
6162
}
@@ -72,7 +73,7 @@ class LoginScreenTest {
7273
@Test
7374
fun testEmailInput() {
7475
composeTestRule
75-
.onNodeWithTag("email")
76+
.onNodeWithText("E-Mail")
7677
.performTextReplacement("test@test.com")
7778

7879
verify { mockActions.onEmailChange("test@test.com") }
@@ -90,7 +91,7 @@ class LoginScreenTest {
9091
@Test
9192
fun testPasswordInput() {
9293
composeTestRule
93-
.onNodeWithTag("password")
94+
.onNodeWithText("Password")
9495
.performTextReplacement("secret")
9596

9697
verify { mockActions.onPasswordChange("secret") }
@@ -106,7 +107,9 @@ class LoginScreenTest {
106107
*/
107108
@Test
108109
fun testLoginButtonClick() {
109-
composeTestRule.onNodeWithTag("login").performClick()
110+
composeTestRule
111+
.onNode(hasText("Login") and hasClickAction())
112+
.performClick()
110113

111114
verify { mockActions.onLoginClick() }
112115
}

0 commit comments

Comments
 (0)