From 57d3634c3e5e76d8d541732721b92a6ae27c9079 Mon Sep 17 00:00:00 2001 From: Jonatan Rhodin Date: Wed, 20 Dec 2023 16:28:10 +0100 Subject: [PATCH] Remove error message when changing the voucher input --- .../viewmodel/VoucherDialogViewModel.kt | 2 ++ .../viewmodel/VoucherDialogViewModelTest.kt | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt index 0cc55b992c7d..b26429f18b61 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModel.kt @@ -66,6 +66,8 @@ class VoucherDialogViewModel( } fun onVoucherInputChange(voucherString: String) { + // Remove any errors when the user starts typing again + vmState.update { VoucherDialogState.Default } if (VoucherRegexHelper.validate(voucherString)) { val trimmedVoucher = VoucherRegexHelper.trim(voucherString) voucherInput.value = diff --git a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt index 01ce6659cb38..bfa068a381e3 100644 --- a/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt +++ b/android/app/src/test/kotlin/net/mullvad/mullvadvpn/viewmodel/VoucherDialogViewModelTest.kt @@ -122,6 +122,31 @@ class VoucherDialogViewModelTest { } } + @Test + fun testResetStateAfterChangingInput() = runTest { + val voucher = DUMMY_INVALID_VOUCHER + val dummyStringResource = DUMMY_STRING_RESOURCE + + // Arrange + every { mockServiceConnectionManager.voucherRedeemer() } returns mockVoucherRedeemer + every { mockResources.getString(any()) } returns dummyStringResource + every { mockVoucherSubmission.timeAdded } returns 0 + coEvery { mockVoucherRedeemer.submit(voucher) } returns + VoucherSubmissionResult.Error(VoucherSubmissionError.OtherError) + + // Act, Assert + viewModel.uiState.test { + assertEquals(viewModel.uiState.value, awaitItem()) + serviceConnectionState.value = + ServiceConnectionState.ConnectedReady(mockServiceConnectionContainer) + viewModel.onRedeem(voucher) + assertTrue { awaitItem().voucherViewModelState is VoucherDialogState.Verifying } + assertTrue { awaitItem().voucherViewModelState is VoucherDialogState.Error } + viewModel.onVoucherInputChange(DUMMY_VALID_VOUCHER) + assertTrue { awaitItem().voucherViewModelState is VoucherDialogState.Default } + } + } + companion object { private const val DUMMY_VALID_VOUCHER = "dummy_valid_voucher" private const val DUMMY_INVALID_VOUCHER = "dummy_invalid_voucher"