Add error dialog in VaultAddEditViewModel (#914)

This commit is contained in:
Ramsey Smith 2024-01-31 16:35:01 -07:00 committed by Álison Fernandes
parent 4f08d5ddbe
commit 1448322964
2 changed files with 96 additions and 33 deletions

View file

@ -979,12 +979,14 @@ class VaultAddEditViewModel @Inject constructor(
when (action.createCipherResult) {
is CreateCipherResult.Error -> {
// TODO Display error dialog BIT-501
sendEvent(
event = VaultAddEditEvent.ShowToast(
message = "Save Item Failure".asText(),
),
)
mutableStateFlow.update {
it.copy(
dialog = VaultAddEditState.DialogState.Generic(
title = R.string.an_error_has_occurred.asText(),
message = R.string.generic_error_message.asText(),
),
)
}
}
is CreateCipherResult.Success -> {
@ -994,6 +996,9 @@ class VaultAddEditViewModel @Inject constructor(
event = VaultAddEditEvent.ExitApp,
)
} else {
sendEvent(
event = VaultAddEditEvent.ShowToast(R.string.new_item_created.asText()),
)
sendEvent(
event = VaultAddEditEvent.NavigateBack,
)
@ -1022,6 +1027,9 @@ class VaultAddEditViewModel @Inject constructor(
}
is UpdateCipherResult.Success -> {
sendEvent(
event = VaultAddEditEvent.ShowToast(R.string.item_updated.asText()),
)
sendEvent(VaultAddEditEvent.NavigateBack)
}
}

View file

@ -455,6 +455,12 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
assertEquals(stateWithDialog, stateTurbine.awaitItem())
assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(
VaultAddEditEvent.ShowToast(
R.string.new_item_created.asText(),
),
eventTurbine.awaitItem(),
)
assertEquals(
VaultAddEditEvent.NavigateBack,
eventTurbine.awaitItem(),
@ -530,34 +536,76 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
}
@Test
fun `in add mode, SaveClick should update value to loading`() = runTest {
val stateWithName = createVaultAddItemState(
vaultAddEditType = VaultAddEditType.AddItem,
commonContentViewState = createCommonContentViewState(
name = "mockName-1",
),
)
mutableVaultDataFlow.value = DataState.Loaded(createVaultData())
val viewModel = createAddVaultItemViewModel(
createSavedStateHandleWithState(
state = stateWithName,
fun `in add mode, createCipherInOrganization success should ShowToast and NavigateBack`() =
runTest {
val stateWithName = createVaultAddItemState(
vaultAddEditType = VaultAddEditType.AddItem,
),
)
commonContentViewState = createCommonContentViewState(
name = "mockName-1",
),
)
coEvery {
vaultRepository.createCipherInOrganization(any(), any())
} returns CreateCipherResult.Success
viewModel.eventFlow.test {
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
assertEquals(VaultAddEditEvent.NavigateBack, awaitItem())
mutableVaultDataFlow.value = DataState.Loaded(createVaultData())
val viewModel = createAddVaultItemViewModel(
createSavedStateHandleWithState(
state = stateWithName,
vaultAddEditType = VaultAddEditType.AddItem,
),
)
coEvery {
vaultRepository.createCipherInOrganization(any(), any())
} returns CreateCipherResult.Success
viewModel.eventFlow.test {
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
assertEquals(
VaultAddEditEvent.ShowToast(
R.string.new_item_created.asText(),
),
awaitItem(),
)
assertEquals(VaultAddEditEvent.NavigateBack, awaitItem())
}
}
}
@Test
fun `in add mode, SaveClick createCipher error should emit ShowToast`() = runTest {
fun `in edit mode, updateCipher success should ShowToast and NavigateBack`() =
runTest {
val cipherView = createMockCipherView(1)
val stateWithName = createVaultAddItemState(
vaultAddEditType = VaultAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
commonContentViewState = createCommonContentViewState(
name = "mockName-1",
),
)
mutableVaultDataFlow.value = DataState.Loaded(createVaultData(cipherView = cipherView))
val viewModel = createAddVaultItemViewModel(
createSavedStateHandleWithState(
state = stateWithName,
vaultAddEditType = VaultAddEditType.AddItem,
),
)
coEvery {
vaultRepository.updateCipher(any(), any())
} returns UpdateCipherResult.Success
viewModel.eventFlow.test {
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
assertEquals(
VaultAddEditEvent.ShowToast(
R.string.item_updated.asText(),
),
awaitItem(),
)
assertEquals(VaultAddEditEvent.NavigateBack, awaitItem())
}
}
@Test
fun `in add mode, SaveClick createCipher error should show error dialog`() = runTest {
val stateWithName = createVaultAddItemState(
vaultAddEditType = VaultAddEditType.AddItem,
@ -577,10 +625,17 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
coEvery {
vaultRepository.createCipherInOrganization(any(), any())
} returns CreateCipherResult.Error
viewModel.eventFlow.test {
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
assertEquals(VaultAddEditEvent.ShowToast("Save Item Failure".asText()), awaitItem())
}
viewModel.actionChannel.trySend(VaultAddEditAction.Common.SaveClick)
assertEquals(
stateWithName.copy(
dialog = VaultAddEditState.DialogState.Generic(
title = R.string.an_error_has_occurred.asText(),
message = R.string.generic_error_message.asText(),
),
),
viewModel.stateFlow.value,
)
}
@Test