Skip to content

Commit

Permalink
Add tests to verify LoginScreen error states (#2589)
Browse files Browse the repository at this point in the history
* Add LoginScreen tests to verify different error states

* spotless

* spotless

---------

Co-authored-by: pld <peter@ona.io>
  • Loading branch information
DebbieArita and pld authored Jul 14, 2023
1 parent 6064c6f commit 08724d3
Showing 1 changed file with 147 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,19 @@ package org.smartregister.fhircore.quest.ui.login

import android.view.inputmethod.EditorInfo
import androidx.compose.ui.test.assertHasClickAction
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.performImeAction
import androidx.compose.ui.test.performTextInput
import androidx.test.platform.app.InstrumentationRegistry
import kotlinx.coroutines.ExperimentalCoroutinesApi
import org.junit.Rule
import org.junit.Test
import org.smartregister.fhircore.engine.configuration.app.ApplicationConfiguration
import org.smartregister.fhircore.engine.configuration.app.LoginConfig
import org.smartregister.fhircore.quest.R

@ExperimentalCoroutinesApi
class LoginScreenTest {
Expand All @@ -52,6 +56,8 @@ class LoginScreenTest {
loginConfig = LoginConfig(showLogo = true),
)

private val context = InstrumentationRegistry.getInstrumentation().targetContext

@Test
fun testLoginPage() {
composeRule.setContent {
Expand Down Expand Up @@ -110,4 +116,145 @@ class LoginScreenTest {
.performImeAction()
.equals(EditorInfo.IME_ACTION_DONE)
}

@Test
fun testLoginFailsWithUnknownTextErrorMessage() {
verifyUnknownTextErrorMessage(
LoginErrorState.UNKNOWN_HOST,
R.string.login_call_fail_error_message,
)
}

@Test
fun testLoginFailsWithInvalidCredentialsErrorMessage() {
verifyInvalidCredentialsErrorMessage(
LoginErrorState.INVALID_CREDENTIALS,
R.string.invalid_login_credentials,
)
}

@Test
fun testLoginFailsWithMultiUserLoginErrorMessage() {
verifyMultiUserLoginErrorMessage(
LoginErrorState.MULTI_USER_LOGIN_ATTEMPT,
R.string.multi_user_login_attempt,
)
}

@Test
fun testLoginFailsWithErrorFetchingUserMessage() {
verifyErrorFetchingUser(
LoginErrorState.ERROR_FETCHING_USER,
R.string.error_fetching_user_details,
)
}

@Test
fun testLoginFailsWithInvalidOfflineStateErrorMessage() {
verifyInvalidOfflineState(
LoginErrorState.INVALID_OFFLINE_STATE,
R.string.invalid_offline_login_state,
)
}

private fun verifyUnknownTextErrorMessage(loginErrorState: LoginErrorState, errorMessageId: Int) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyInvalidCredentialsErrorMessage(
loginErrorState: LoginErrorState,
errorMessageId: Int,
) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyMultiUserLoginErrorMessage(
loginErrorState: LoginErrorState,
errorMessageId: Int,
) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyErrorFetchingUser(loginErrorState: LoginErrorState, errorMessageId: Int) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}

private fun verifyInvalidOfflineState(loginErrorState: LoginErrorState, errorMessageId: Int) {
composeRule.setContent {
LoginPage(
applicationConfiguration = applicationConfiguration,
username = "user",
onUsernameChanged = { listenerObjectSpy.onUsernameUpdated() },
password = "password",
onPasswordChanged = { listenerObjectSpy.onPasswordUpdated() },
forgotPassword = { listenerObjectSpy.forgotPassword() },
onLoginButtonClicked = { listenerObjectSpy.attemptRemoteLogin() },
appVersionPair = Pair(1, "1.0.1"),
loginErrorState = loginErrorState,
)
}
composeRule
.onNodeWithText(context.getString(R.string.login_error, context.getString(errorMessageId)))
.assertIsDisplayed()
}
}

0 comments on commit 08724d3

Please sign in to comment.