Clean up CreateAccountViewModelTest (#142)

This commit is contained in:
Andrew Haisting 2023-10-23 10:29:03 -05:00 committed by Álison Fernandes
parent 06384e17ab
commit bd8357a0c4

View file

@ -32,6 +32,12 @@ import org.junit.jupiter.api.Test
class CreateAccountViewModelTest : BaseViewModelTest() { class CreateAccountViewModelTest : BaseViewModelTest() {
/**
* Saved state handle that has valid inputs. Useful for tests that want to test things
* after the user has entered all valid inputs.
*/
private val validInputHandle = SavedStateHandle(mapOf("state" to VALID_INPUT_STATE))
private val mockAuthRepository = mockk<AuthRepository> { private val mockAuthRepository = mockk<AuthRepository> {
every { captchaTokenResultFlow } returns flowOf() every { captchaTokenResultFlow } returns flowOf()
} }
@ -203,33 +209,16 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
} returns RegisterResult.Success(captchaToken = "mock_token") } returns RegisterResult.Success(captchaToken = "mock_token")
} }
val viewModel = CreateAccountViewModel( val viewModel = CreateAccountViewModel(
savedStateHandle = SavedStateHandle(), savedStateHandle = validInputHandle,
authRepository = repo, authRepository = repo,
) )
viewModel.trySendAction(PasswordInputChange("longenoughpassword"))
viewModel.trySendAction(EmailInputChange(EMAIL))
viewModel.trySendAction(PasswordInputChange(PASSWORD))
viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD))
viewModel.trySendAction(AcceptPoliciesToggle(true))
turbineScope { turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope) val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope) val eventFlow = viewModel.eventFlow.testIn(backgroundScope)
assertEquals( assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem())
DEFAULT_STATE.copy(
emailInput = EMAIL,
passwordInput = PASSWORD,
confirmPasswordInput = PASSWORD,
isAcceptPoliciesToggled = true,
),
stateFlow.awaitItem(),
)
viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick)
assertEquals( assertEquals(
DEFAULT_STATE.copy( VALID_INPUT_STATE.copy(
emailInput = EMAIL,
passwordInput = PASSWORD,
confirmPasswordInput = PASSWORD,
isAcceptPoliciesToggled = true,
loadingDialogState = LoadingDialogState.Shown( loadingDialogState = LoadingDialogState.Shown(
text = R.string.creating_account.asText(), text = R.string.creating_account.asText(),
), ),
@ -243,16 +232,8 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
), ),
eventFlow.awaitItem(), eventFlow.awaitItem(),
) )
assertEquals( // Make sure loading dialog is hidden:
DEFAULT_STATE.copy( assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem())
emailInput = EMAIL,
passwordInput = PASSWORD,
confirmPasswordInput = PASSWORD,
isAcceptPoliciesToggled = true,
loadingDialogState = LoadingDialogState.Hidden,
),
stateFlow.awaitItem(),
)
} }
} }
@ -270,30 +251,14 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
} returns RegisterResult.Error(errorMessage = "mock_error") } returns RegisterResult.Error(errorMessage = "mock_error")
} }
val viewModel = CreateAccountViewModel( val viewModel = CreateAccountViewModel(
savedStateHandle = SavedStateHandle(), savedStateHandle = validInputHandle,
authRepository = repo, authRepository = repo,
) )
viewModel.trySendAction(EmailInputChange(EMAIL))
viewModel.trySendAction(PasswordInputChange(PASSWORD))
viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD))
viewModel.trySendAction(AcceptPoliciesToggle(true))
viewModel.stateFlow.test { viewModel.stateFlow.test {
assertEquals( assertEquals(VALID_INPUT_STATE, awaitItem())
DEFAULT_STATE.copy(
emailInput = EMAIL,
passwordInput = PASSWORD,
confirmPasswordInput = PASSWORD,
isAcceptPoliciesToggled = true,
),
awaitItem(),
)
viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick)
assertEquals( assertEquals(
DEFAULT_STATE.copy( VALID_INPUT_STATE.copy(
emailInput = EMAIL,
passwordInput = PASSWORD,
confirmPasswordInput = PASSWORD,
isAcceptPoliciesToggled = true,
loadingDialogState = LoadingDialogState.Shown( loadingDialogState = LoadingDialogState.Shown(
text = R.string.creating_account.asText(), text = R.string.creating_account.asText(),
), ),
@ -301,11 +266,7 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
awaitItem(), awaitItem(),
) )
assertEquals( assertEquals(
DEFAULT_STATE.copy( VALID_INPUT_STATE.copy(
emailInput = EMAIL,
passwordInput = PASSWORD,
confirmPasswordInput = PASSWORD,
isAcceptPoliciesToggled = true,
loadingDialogState = LoadingDialogState.Hidden, loadingDialogState = LoadingDialogState.Hidden,
errorDialogState = BasicDialogState.Shown( errorDialogState = BasicDialogState.Shown(
title = R.string.an_error_has_occurred.asText(), title = R.string.an_error_has_occurred.asText(),
@ -335,13 +296,9 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
} returns RegisterResult.CaptchaRequired(captchaId = "mock_captcha_id") } returns RegisterResult.CaptchaRequired(captchaId = "mock_captcha_id")
} }
val viewModel = CreateAccountViewModel( val viewModel = CreateAccountViewModel(
savedStateHandle = SavedStateHandle(), savedStateHandle = validInputHandle,
authRepository = repo, authRepository = repo,
) )
viewModel.trySendAction(EmailInputChange(EMAIL))
viewModel.trySendAction(PasswordInputChange(PASSWORD))
viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD))
viewModel.trySendAction(AcceptPoliciesToggle(true))
viewModel.eventFlow.test { viewModel.eventFlow.test {
viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick)
assertEquals( assertEquals(
@ -369,13 +326,9 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
} returns RegisterResult.Success(captchaToken = "mock_captcha_token") } returns RegisterResult.Success(captchaToken = "mock_captcha_token")
} }
val viewModel = CreateAccountViewModel( val viewModel = CreateAccountViewModel(
savedStateHandle = SavedStateHandle(), savedStateHandle = validInputHandle,
authRepository = repo, authRepository = repo,
) )
viewModel.trySendAction(EmailInputChange(EMAIL))
viewModel.trySendAction(PasswordInputChange(PASSWORD))
viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD))
viewModel.trySendAction(AcceptPoliciesToggle(true))
viewModel.eventFlow.test { viewModel.eventFlow.test {
viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick)
assertEquals( assertEquals(
@ -497,6 +450,8 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
} }
companion object { companion object {
private const val PASSWORD = "longenoughtpassword"
private const val EMAIL = "test@test.com"
private val DEFAULT_STATE = CreateAccountState( private val DEFAULT_STATE = CreateAccountState(
passwordInput = "", passwordInput = "",
emailInput = "", emailInput = "",
@ -507,10 +462,17 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
errorDialogState = BasicDialogState.Hidden, errorDialogState = BasicDialogState.Hidden,
loadingDialogState = LoadingDialogState.Hidden, loadingDialogState = LoadingDialogState.Hidden,
) )
private val VALID_INPUT_STATE = CreateAccountState(
passwordInput = PASSWORD,
emailInput = EMAIL,
confirmPasswordInput = PASSWORD,
passwordHintInput = "",
isCheckDataBreachesToggled = false,
isAcceptPoliciesToggled = true,
errorDialogState = BasicDialogState.Hidden,
loadingDialogState = LoadingDialogState.Hidden,
)
private const val LOGIN_RESULT_PATH = private const val LOGIN_RESULT_PATH =
"com.x8bit.bitwarden.data.auth.repository.util.CaptchaUtilsKt" "com.x8bit.bitwarden.data.auth.repository.util.CaptchaUtilsKt"
private const val PASSWORD = "longenoughtpassword"
private const val EMAIL = "test@test.com"
} }
} }