Add support for deleting an attachment (#768)

This commit is contained in:
David Perez
2024-06-20 17:08:07 +01:00
committed by Álison Fernandes
parent dc3081c5d6
commit 04d60a50ff
7 changed files with 266 additions and 17 deletions
@@ -11,6 +11,7 @@ import androidx.compose.ui.test.onNodeWithContentDescription
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import com.x8bit.bitwarden.data.platform.repository.util.bufferedMutableSharedFlow
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
import com.x8bit.bitwarden.ui.platform.base.BaseComposeTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
@@ -76,7 +77,7 @@ class AttachmentsScreenTest : BaseComposeTest() {
@Test
fun `on choose file click should send ChooseFileClick`() {
mutableStateFlow.update {
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
it.copy(viewState = DEFAULT_CONTENT_WITHOUT_ATTACHMENTS)
}
composeTestRule.onNodeWithTextAfterScroll("Choose file").performClick()
@@ -96,7 +97,7 @@ class AttachmentsScreenTest : BaseComposeTest() {
composeTestRule.onNode(isProgressBar).assertDoesNotExist()
mutableStateFlow.update {
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
it.copy(viewState = DEFAULT_CONTENT_WITHOUT_ATTACHMENTS)
}
composeTestRule.onNode(isProgressBar).assertDoesNotExist()
}
@@ -113,7 +114,7 @@ class AttachmentsScreenTest : BaseComposeTest() {
composeTestRule.onNodeWithText(errorMessage).assertDoesNotExist()
mutableStateFlow.update {
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
it.copy(viewState = DEFAULT_CONTENT_WITHOUT_ATTACHMENTS)
}
composeTestRule.onNodeWithText(errorMessage).assertDoesNotExist()
}
@@ -121,7 +122,7 @@ class AttachmentsScreenTest : BaseComposeTest() {
@Test
fun `content with no items should be displayed according to state`() {
mutableStateFlow.update {
it.copy(viewState = AttachmentsState.ViewState.Content(emptyList()))
it.copy(viewState = DEFAULT_CONTENT_WITHOUT_ATTACHMENTS)
}
composeTestRule
.onNodeWithTextAfterScroll("There are no attachments.")
@@ -197,15 +198,60 @@ class AttachmentsScreenTest : BaseComposeTest() {
viewModel.trySendAction(AttachmentsAction.DeleteClick(cipherId))
}
}
@Test
fun `error dialog should be displayed according to state`() {
val errorMessage = "Fail"
composeTestRule.onNode(isDialog()).assertDoesNotExist()
composeTestRule.onNodeWithText(errorMessage).assertDoesNotExist()
mutableStateFlow.update {
it.copy(
dialogState = AttachmentsState.DialogState.Error(
title = null,
message = errorMessage.asText(),
),
)
}
composeTestRule
.onNodeWithText(errorMessage)
.assert(hasAnyAncestor(isDialog()))
.assertIsDisplayed()
}
@Test
fun `loading dialog should be displayed according to state`() {
val loadingMessage = "deleting"
composeTestRule.onNode(isDialog()).assertDoesNotExist()
composeTestRule.onNodeWithText(loadingMessage).assertDoesNotExist()
mutableStateFlow.update {
it.copy(dialogState = AttachmentsState.DialogState.Loading(loadingMessage.asText()))
}
composeTestRule
.onNodeWithText(loadingMessage)
.assertIsDisplayed()
.assert(hasAnyAncestor(isDialog()))
}
}
private val DEFAULT_STATE: AttachmentsState = AttachmentsState(
cipherId = "cipherId-1234",
viewState = AttachmentsState.ViewState.Loading,
dialogState = null,
)
private val DEFAULT_CONTENT_WITHOUT_ATTACHMENTS: AttachmentsState.ViewState.Content =
AttachmentsState.ViewState.Content(
originalCipher = createMockCipherView(number = 1),
attachments = emptyList(),
)
private val DEFAULT_CONTENT_WITH_ATTACHMENTS: AttachmentsState.ViewState.Content =
AttachmentsState.ViewState.Content(
originalCipher = createMockCipherView(number = 1),
attachments = listOf(
AttachmentsState.AttachmentItem(
id = "cipherId-1234",
@@ -7,11 +7,13 @@ import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.platform.repository.model.DataState
import com.x8bit.bitwarden.data.vault.datasource.sdk.model.createMockCipherView
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.DeleteAttachmentResult
import com.x8bit.bitwarden.ui.platform.base.BaseViewModelTest
import com.x8bit.bitwarden.ui.platform.base.util.asText
import com.x8bit.bitwarden.ui.platform.base.util.concat
import com.x8bit.bitwarden.ui.platform.manager.intent.IntentManager
import com.x8bit.bitwarden.ui.vault.feature.attachments.util.toViewState
import io.mockk.coEvery
import io.mockk.every
import io.mockk.mockk
import io.mockk.mockkStatic
@@ -71,6 +73,19 @@ class AttachmentsViewModelTest : BaseViewModelTest() {
}
}
@Test
fun `DismissDialogClick should emit DismissDialogClick`() = runTest {
val initialState = DEFAULT_STATE.copy(
dialogState = AttachmentsState.DialogState.Loading("Loading".asText()),
)
val viewModel = createViewModel(initialState)
viewModel.stateFlow.test {
assertEquals(initialState, awaitItem())
viewModel.trySendAction(AttachmentsAction.DismissDialogClick)
assertEquals(initialState.copy(dialogState = null), awaitItem())
}
}
@Test
fun `ChooseFileClick should emit ShowToast`() = runTest {
val viewModel = createViewModel()
@@ -91,12 +106,65 @@ class AttachmentsViewModelTest : BaseViewModelTest() {
}
@Test
fun `DeleteClick should emit ShowToast`() = runTest {
val attachmentId = "attachmentId-1234"
fun `DeleteClick with deleteCipherAttachment error should display error dialog`() = runTest {
val cipherId = "mockId-1"
val attachmentId = "mockId-1"
val cipherView = createMockCipherView(number = 1)
val initialState = DEFAULT_STATE.copy(viewState = DEFAULT_CONTENT_WITH_ATTACHMENTS)
coEvery {
vaultRepository.deleteCipherAttachment(
cipherId = cipherId,
attachmentId = attachmentId,
cipherView = cipherView,
)
} returns DeleteAttachmentResult.Error
mutableVaultItemStateFlow.tryEmit(DataState.Loaded(cipherView))
val viewModel = createViewModel()
viewModel.stateFlow.test {
assertEquals(initialState, awaitItem())
viewModel.actionChannel.trySend(AttachmentsAction.DeleteClick(attachmentId))
assertEquals(
initialState.copy(
dialogState = AttachmentsState.DialogState.Loading(
message = R.string.deleting.asText(),
),
),
awaitItem(),
)
assertEquals(
initialState.copy(
dialogState = AttachmentsState.DialogState.Error(
title = R.string.an_error_has_occurred.asText(),
message = R.string.generic_error_message.asText(),
),
),
awaitItem(),
)
}
}
@Test
fun `DeleteClick with deleteCipherAttachment success should emit ShowToast`() = runTest {
val cipherId = "mockId-1"
val attachmentId = "mockId-1"
val cipherView = createMockCipherView(number = 1)
coEvery {
vaultRepository.deleteCipherAttachment(
cipherId = cipherId,
attachmentId = attachmentId,
cipherView = cipherView,
)
} returns DeleteAttachmentResult.Success
mutableVaultItemStateFlow.tryEmit(DataState.Loaded(cipherView))
val viewModel = createViewModel()
viewModel.eventFlow.test {
viewModel.trySendAction(AttachmentsAction.DeleteClick(attachmentId))
assertEquals(AttachmentsEvent.ShowToast("Not Yet Implemented".asText()), awaitItem())
viewModel.actionChannel.trySend(AttachmentsAction.DeleteClick(cipherId))
assertEquals(
AttachmentsEvent.ShowToast(R.string.attachment_deleted.asText()),
awaitItem(),
)
}
}
@@ -213,23 +281,25 @@ class AttachmentsViewModelTest : BaseViewModelTest() {
vaultRepo = vaultRepository,
savedStateHandle = SavedStateHandle().apply {
set("state", initialState)
set("cipher_id", initialState?.cipherId ?: "cipherId-1234")
set("cipher_id", initialState?.cipherId ?: "mockId-1")
},
)
}
private val DEFAULT_STATE: AttachmentsState = AttachmentsState(
cipherId = "cipherId-1234",
cipherId = "mockId-1",
viewState = AttachmentsState.ViewState.Loading,
dialogState = null,
)
private val DEFAULT_CONTENT_WITH_ATTACHMENTS: AttachmentsState.ViewState.Content =
AttachmentsState.ViewState.Content(
originalCipher = createMockCipherView(number = 1),
attachments = listOf(
AttachmentsState.AttachmentItem(
id = "cipherId-1234",
title = "cool_file.png",
displaySize = "10 MB",
id = "mockId-1",
title = "mockFileName-1",
displaySize = "mockSizeName-1",
),
),
)
@@ -16,6 +16,7 @@ class CipherViewExtensionsTest {
assertEquals(
AttachmentsState.ViewState.Content(
originalCipher = cipherView,
attachments = listOf(
AttachmentsState.AttachmentItem(
id = "mockId-1",
@@ -37,7 +38,10 @@ class CipherViewExtensionsTest {
val result = cipherView.toViewState()
assertEquals(
AttachmentsState.ViewState.Content(attachments = emptyList()),
AttachmentsState.ViewState.Content(
originalCipher = cipherView,
attachments = emptyList(),
),
result,
)
}
@@ -55,7 +59,10 @@ class CipherViewExtensionsTest {
val result = cipherView.toViewState()
assertEquals(
AttachmentsState.ViewState.Content(attachments = emptyList()),
AttachmentsState.ViewState.Content(
originalCipher = cipherView,
attachments = emptyList(),
),
result,
)
}