Repository navigation
Expand file tree
/
Copy pathLoginScreenTest.kt
More file actions
116 lines (103 loc) · 3.91 KB
/
Copy pathLoginScreenTest.kt
File metadata and controls
116 lines (103 loc) · 3.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.company.template.auth
import androidx.compose.ui.test.assertIsDisplayed
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 androidx.compose.ui.test.performTextInput
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.company.template.ui.theme.TemplateTheme
import org.junit.Assert.assertTrue
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(AndroidJUnit4::class)
class LoginScreenTest {
@get:Rule
val composeTestRule = createComposeRule()
private var capturedEmail = ""
private var capturedPassword = ""
private fun setContent(
loginForm: LoginFormState = LoginFormState(),
uiState: AuthUiState = AuthUiState.Idle,
onEmailChange: (String) -> Unit = { capturedEmail = it },
onPasswordChange: (String) -> Unit = { capturedPassword = it },
onSignIn: () -> Unit = {},
onSignInWithGoogle: () -> Unit = {},
onNavigateToRegister: () -> Unit = {},
) {
composeTestRule.setContent {
TemplateTheme {
LoginScreen(
loginForm = loginForm,
uiState = uiState,
onEmailChange = onEmailChange,
onPasswordChange = onPasswordChange,
onSignIn = onSignIn,
onSignInWithGoogle = onSignInWithGoogle,
onNavigateToRegister = onNavigateToRegister,
)
}
}
}
@Test
fun loginScreen_displaysSignInHeading() {
setContent()
// The heading "Sign In" is displayed (there may also be a button with the same label)
composeTestRule.onNodeWithText("Sign In", useUnmergedTree = false).assertIsDisplayed()
}
@Test
fun loginScreen_displaysGoogleSignInButton() {
setContent()
composeTestRule.onNodeWithText("Continue with Google").assertIsDisplayed()
}
@Test
fun loginScreen_googleButton_invokesCallback() {
var called = false
setContent(onSignInWithGoogle = { called = true })
composeTestRule.onNodeWithText("Continue with Google").performClick()
assertTrue(called)
}
@Test
fun loginScreen_displaysErrorMessage() {
setContent(uiState = AuthUiState.Error("Invalid credentials"))
composeTestRule.onNodeWithText("Invalid credentials").assertIsDisplayed()
}
@Test
fun loginScreen_displaysRegisterLink() {
setContent()
composeTestRule.onNodeWithText("Register").assertIsDisplayed()
}
@Test
fun loginScreen_clickRegister_invokesCallback() {
var navigateCalled = false
setContent(onNavigateToRegister = { navigateCalled = true })
composeTestRule.onNodeWithText("Register").performClick()
assertTrue(navigateCalled)
}
@Test
fun loginScreen_clickSignIn_invokesCallback() {
var signInCalled = false
setContent(
loginForm = LoginFormState(email = "test@example.com", password = "secret"),
onSignIn = { signInCalled = true },
)
// Use the test tag to target the button specifically, not the heading
composeTestRule.onNodeWithTag(LoginTestTags.SIGN_IN_BUTTON).performClick()
assertTrue(signInCalled)
}
@Test
fun loginScreen_typingEmail_updatesCallback() {
var updated = ""
setContent(onEmailChange = { updated = it })
composeTestRule.onNodeWithText("Email").performTextInput("test@example.com")
assertTrue(updated.isNotEmpty())
}
@Test
fun loginScreen_typingPassword_updatesCallback() {
var updated = ""
setContent(onPasswordChange = { updated = it })
composeTestRule.onNodeWithText("Password").performTextInput("secret123")
assertTrue(updated.isNotEmpty())
}
}