From ecc5d4ce30bc2b5418ecb0b677581734a052fc81 Mon Sep 17 00:00:00 2001 From: Phil Cappelli <150719757+phil-livefront@users.noreply.github.com> Date: Fri, 31 Jan 2025 10:02:17 -0500 Subject: [PATCH] PM-16861 - Update Behavior When Tapping Same Generator Tab Already Viewing (#4653) --- .../feature/generator/GeneratorViewModel.kt | 2 ++ .../generator/GeneratorViewModelTest.kt | 33 +++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt index 2ba4a7a842..70af7986f7 100644 --- a/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt +++ b/app/src/main/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModel.kt @@ -801,6 +801,8 @@ class GeneratorViewModel @Inject constructor( //region Main Type Option Handlers private fun handleMainTypeOptionSelect(action: GeneratorAction.MainTypeOptionSelect) { + if (action.mainTypeOption == state.selectedType.mainTypeOption) return + when (action.mainTypeOption) { GeneratorState.MainTypeOption.PASSWORD -> { loadPasscodeOptions(selectedType = GeneratorState.MainType.Password()) diff --git a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt index 7c99befc20..0ff89ea10c 100644 --- a/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt +++ b/app/src/test/java/com/x8bit/bitwarden/ui/tools/feature/generator/GeneratorViewModelTest.kt @@ -716,22 +716,51 @@ class GeneratorViewModelTest : BaseViewModelTest() { ) } + @Test + fun `MainTypeOptionSelect shouldn't update the state if its already selected`() = runTest { + val viewModel = createViewModel() + fakeGeneratorRepository.setMockGeneratePassphraseResult( + GeneratedPassphraseResult.Success("updatedText"), + ) + + val expectedState = initialPasscodeState.copy( + selectedType = GeneratorState.MainType.Passphrase(), + generatedText = "updatedText", + ) + + val action = GeneratorAction.MainTypeOptionSelect(GeneratorState.MainTypeOption.PASSPHRASE) + + viewModel.trySendAction(action) + assertEquals(expectedState, viewModel.stateFlow.value) + + fakeGeneratorRepository.setMockGeneratePassphraseResult( + GeneratedPassphraseResult.Success("updatedTextAgain"), + ) + + viewModel.trySendAction(action) + assertEquals(expectedState, viewModel.stateFlow.value) + } + @Test fun `MainTypeOptionSelect PASSWORD should switch to Passcode`() = runTest { val viewModel = createViewModel() + viewModel.trySendAction( + GeneratorAction.MainTypeOptionSelect(GeneratorState.MainTypeOption.USERNAME), + ) + fakeGeneratorRepository.setMockGeneratePasswordResult( GeneratedPasswordResult.Success("updatedText"), ) val action = GeneratorAction.MainTypeOptionSelect(GeneratorState.MainTypeOption.PASSWORD) - viewModel.trySendAction(action) - val expectedState = initialPasscodeState.copy( selectedType = GeneratorState.MainType.Password(), generatedText = "updatedText", ) + viewModel.trySendAction(action) + assertEquals(expectedState, viewModel.stateFlow.value) }