From bd8357a0c48a8412b1128b1d949689eff00e59da Mon Sep 17 00:00:00 2001 From: Andrew Haisting <142518658+ahaisting-livefront@users.noreply.github.com> Date: Mon, 23 Oct 2023 10:29:03 -0500 Subject: [PATCH] Clean up CreateAccountViewModelTest (#142) --- .../CreateAccountViewModelTest.kt | 96 ++++++------------- 1 file changed, 29 insertions(+), 67 deletions(-) diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/createaccount/CreateAccountViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/createaccount/CreateAccountViewModelTest.kt index a87b8f13a..c99013d8c 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/createaccount/CreateAccountViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/auth/feature/createaccount/CreateAccountViewModelTest.kt @@ -32,6 +32,12 @@ import org.junit.jupiter.api.Test 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 { every { captchaTokenResultFlow } returns flowOf() } @@ -203,33 +209,16 @@ class CreateAccountViewModelTest : BaseViewModelTest() { } returns RegisterResult.Success(captchaToken = "mock_token") } val viewModel = CreateAccountViewModel( - savedStateHandle = SavedStateHandle(), + savedStateHandle = validInputHandle, authRepository = repo, ) - viewModel.trySendAction(PasswordInputChange("longenoughpassword")) - viewModel.trySendAction(EmailInputChange(EMAIL)) - viewModel.trySendAction(PasswordInputChange(PASSWORD)) - viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD)) - viewModel.trySendAction(AcceptPoliciesToggle(true)) turbineScope { val stateFlow = viewModel.stateFlow.testIn(backgroundScope) val eventFlow = viewModel.eventFlow.testIn(backgroundScope) - assertEquals( - DEFAULT_STATE.copy( - emailInput = EMAIL, - passwordInput = PASSWORD, - confirmPasswordInput = PASSWORD, - isAcceptPoliciesToggled = true, - ), - stateFlow.awaitItem(), - ) + assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem()) viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) assertEquals( - DEFAULT_STATE.copy( - emailInput = EMAIL, - passwordInput = PASSWORD, - confirmPasswordInput = PASSWORD, - isAcceptPoliciesToggled = true, + VALID_INPUT_STATE.copy( loadingDialogState = LoadingDialogState.Shown( text = R.string.creating_account.asText(), ), @@ -243,16 +232,8 @@ class CreateAccountViewModelTest : BaseViewModelTest() { ), eventFlow.awaitItem(), ) - assertEquals( - DEFAULT_STATE.copy( - emailInput = EMAIL, - passwordInput = PASSWORD, - confirmPasswordInput = PASSWORD, - isAcceptPoliciesToggled = true, - loadingDialogState = LoadingDialogState.Hidden, - ), - stateFlow.awaitItem(), - ) + // Make sure loading dialog is hidden: + assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem()) } } @@ -270,30 +251,14 @@ class CreateAccountViewModelTest : BaseViewModelTest() { } returns RegisterResult.Error(errorMessage = "mock_error") } val viewModel = CreateAccountViewModel( - savedStateHandle = SavedStateHandle(), + savedStateHandle = validInputHandle, authRepository = repo, ) - viewModel.trySendAction(EmailInputChange(EMAIL)) - viewModel.trySendAction(PasswordInputChange(PASSWORD)) - viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD)) - viewModel.trySendAction(AcceptPoliciesToggle(true)) viewModel.stateFlow.test { - assertEquals( - DEFAULT_STATE.copy( - emailInput = EMAIL, - passwordInput = PASSWORD, - confirmPasswordInput = PASSWORD, - isAcceptPoliciesToggled = true, - ), - awaitItem(), - ) + assertEquals(VALID_INPUT_STATE, awaitItem()) viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) assertEquals( - DEFAULT_STATE.copy( - emailInput = EMAIL, - passwordInput = PASSWORD, - confirmPasswordInput = PASSWORD, - isAcceptPoliciesToggled = true, + VALID_INPUT_STATE.copy( loadingDialogState = LoadingDialogState.Shown( text = R.string.creating_account.asText(), ), @@ -301,11 +266,7 @@ class CreateAccountViewModelTest : BaseViewModelTest() { awaitItem(), ) assertEquals( - DEFAULT_STATE.copy( - emailInput = EMAIL, - passwordInput = PASSWORD, - confirmPasswordInput = PASSWORD, - isAcceptPoliciesToggled = true, + VALID_INPUT_STATE.copy( loadingDialogState = LoadingDialogState.Hidden, errorDialogState = BasicDialogState.Shown( title = R.string.an_error_has_occurred.asText(), @@ -335,13 +296,9 @@ class CreateAccountViewModelTest : BaseViewModelTest() { } returns RegisterResult.CaptchaRequired(captchaId = "mock_captcha_id") } val viewModel = CreateAccountViewModel( - savedStateHandle = SavedStateHandle(), + savedStateHandle = validInputHandle, authRepository = repo, ) - viewModel.trySendAction(EmailInputChange(EMAIL)) - viewModel.trySendAction(PasswordInputChange(PASSWORD)) - viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD)) - viewModel.trySendAction(AcceptPoliciesToggle(true)) viewModel.eventFlow.test { viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) assertEquals( @@ -369,13 +326,9 @@ class CreateAccountViewModelTest : BaseViewModelTest() { } returns RegisterResult.Success(captchaToken = "mock_captcha_token") } val viewModel = CreateAccountViewModel( - savedStateHandle = SavedStateHandle(), + savedStateHandle = validInputHandle, authRepository = repo, ) - viewModel.trySendAction(EmailInputChange(EMAIL)) - viewModel.trySendAction(PasswordInputChange(PASSWORD)) - viewModel.trySendAction(ConfirmPasswordInputChange(PASSWORD)) - viewModel.trySendAction(AcceptPoliciesToggle(true)) viewModel.eventFlow.test { viewModel.actionChannel.trySend(CreateAccountAction.SubmitClick) assertEquals( @@ -497,6 +450,8 @@ class CreateAccountViewModelTest : BaseViewModelTest() { } companion object { + private const val PASSWORD = "longenoughtpassword" + private const val EMAIL = "test@test.com" private val DEFAULT_STATE = CreateAccountState( passwordInput = "", emailInput = "", @@ -507,10 +462,17 @@ class CreateAccountViewModelTest : BaseViewModelTest() { errorDialogState = BasicDialogState.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 = "com.x8bit.bitwarden.data.auth.repository.util.CaptchaUtilsKt" - - private const val PASSWORD = "longenoughtpassword" - private const val EMAIL = "test@test.com" } }