mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-458, BIT-459: Add screens for adding and editing folders (#795)
This commit is contained in:
committed by
Álison Fernandes
parent
9338a51d68
commit
f023650730
+114
-3
@@ -1,13 +1,21 @@
|
||||
package com.x8bit.bitwarden.ui.platform.feature.settings.folders
|
||||
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotDisplayed
|
||||
import androidx.compose.ui.test.isNotDisplayed
|
||||
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.ui.platform.base.BaseComposeTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.feature.settings.folders.model.FolderDisplayItem
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
@@ -15,6 +23,8 @@ import org.junit.Test
|
||||
class FoldersScreenTest : BaseComposeTest() {
|
||||
|
||||
private var onNavigateBackCalled = false
|
||||
private var onNavigateToEditFolderScreenId: String? = null
|
||||
private var onNavigateToAddFolderScreenCalled = false
|
||||
|
||||
private val mutableEventFlow = bufferedMutableSharedFlow<FoldersEvent>()
|
||||
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE)
|
||||
@@ -28,11 +38,33 @@ class FoldersScreenTest : BaseComposeTest() {
|
||||
composeTestRule.setContent {
|
||||
FoldersScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateToEditFolderScreen = { onNavigateToEditFolderScreenId = it },
|
||||
onNavigateToAddFolderScreen = { onNavigateToAddFolderScreenCalled = true },
|
||||
onNavigateBack = { onNavigateBackCalled = true },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateBack should call onNavigateBack`() {
|
||||
mutableEventFlow.tryEmit(FoldersEvent.NavigateBack)
|
||||
assertTrue(onNavigateBackCalled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToAddFolderScreen should call onNavigateToAddFolderScreen`() {
|
||||
mutableEventFlow.tryEmit(FoldersEvent.NavigateToAddFolderScreen)
|
||||
assertTrue(onNavigateToAddFolderScreenCalled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateToEditFolderScreen should call onNavigateToEditFolderScreen`() {
|
||||
val tesId = "TestId"
|
||||
|
||||
mutableEventFlow.tryEmit(FoldersEvent.NavigateToEditFolderScreen(tesId))
|
||||
assertEquals(tesId, onNavigateToEditFolderScreenId)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `close button click should send CloseButtonClick`() {
|
||||
composeTestRule.onNodeWithContentDescription("Close").performClick()
|
||||
@@ -48,11 +80,90 @@ class FoldersScreenTest : BaseComposeTest() {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateBack should call onNavigateBack`() {
|
||||
mutableEventFlow.tryEmit(FoldersEvent.NavigateBack)
|
||||
assertTrue(onNavigateBackCalled)
|
||||
fun `error text should display according to state`() {
|
||||
val message = "An error has occurred"
|
||||
|
||||
mutableStateFlow.update { DEFAULT_LOADED_STATE }
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(message)
|
||||
.assertIsNotDisplayed()
|
||||
|
||||
mutableStateFlow.update {
|
||||
FoldersState(
|
||||
viewState = FoldersState.ViewState.Error(
|
||||
message = "An error has occurred".asText(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(message)
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folders should be displayed according to state`() {
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
viewState = FoldersState.ViewState.Content(emptyList()),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("There are no folders to list.")
|
||||
.isNotDisplayed()
|
||||
|
||||
mutableStateFlow.update { DEFAULT_LOADED_STATE }
|
||||
composeTestRule
|
||||
.onNodeWithText(text = "Test Folder 1")
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(text = "Test Folder 2")
|
||||
.assertIsDisplayed()
|
||||
|
||||
mutableStateFlow.update { DEFAULT_STATE }
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(text = "Test Folder 1")
|
||||
.assertDoesNotExist()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(text = "Test Folder 2")
|
||||
.assertDoesNotExist()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clicking on a folder should send FolderClick action`() {
|
||||
mutableStateFlow.update { DEFAULT_LOADED_STATE }
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(text = "Test Folder 1")
|
||||
.assertIsDisplayed()
|
||||
.performClick()
|
||||
|
||||
verify {
|
||||
viewModel.trySendAction(FoldersAction.FolderClick("Id"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private val DEFAULT_STATE =
|
||||
FoldersState(viewState = FoldersState.ViewState.Loading)
|
||||
|
||||
private val DEFAULT_LOADED_STATE =
|
||||
FoldersState(
|
||||
viewState = FoldersState.ViewState.Content(
|
||||
folderList = listOf(
|
||||
FolderDisplayItem(
|
||||
id = "Id",
|
||||
name = "Test Folder 1",
|
||||
),
|
||||
FolderDisplayItem(
|
||||
id = "Id 2",
|
||||
name = "Test Folder 2",
|
||||
),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
+125
-1
@@ -1,10 +1,15 @@
|
||||
package com.x8bit.bitwarden.ui.platform.feature.settings.folders
|
||||
|
||||
import app.cash.turbine.test
|
||||
import com.bitwarden.core.DateTime
|
||||
import com.bitwarden.core.FolderView
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
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.feature.settings.folders.model.FolderDisplayItem
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
@@ -14,7 +19,8 @@ import org.junit.jupiter.api.Test
|
||||
|
||||
class FoldersViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val mutableFoldersStateFlow = MutableStateFlow(DataState.Loaded(listOf<FolderView>()))
|
||||
private val mutableFoldersStateFlow =
|
||||
MutableStateFlow<DataState<List<FolderView>>>(DataState.Loaded(listOf()))
|
||||
|
||||
private val vaultRepository: VaultRepository = mockk {
|
||||
every { foldersStateFlow } returns mutableFoldersStateFlow
|
||||
@@ -41,7 +47,125 @@ class FoldersViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `FolderClick should emit NavigateToAddFolderScreen`() = runTest {
|
||||
val testId = "TestId"
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(FoldersAction.FolderClick(testId))
|
||||
assertEquals(
|
||||
FoldersEvent.NavigateToEditFolderScreen(testId),
|
||||
awaitItem(),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Error should update state to error`() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
value = DataState.Error(
|
||||
data = listOf(),
|
||||
error = IllegalStateException(),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
createFolderState(
|
||||
viewState = FoldersState.ViewState.Error(
|
||||
R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Loaded with data should update state to content`() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
DataState.Loaded(
|
||||
listOf(DEFAULT_FOLDER_VIEW),
|
||||
),
|
||||
)
|
||||
assertEquals(
|
||||
createFolderState(
|
||||
viewState = FoldersState.ViewState.Content(
|
||||
folderList = listOf(DEFAULT__DISPLAY_FOLDER),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Loading should update the state to Loading`() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
DataState.Loading,
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
createFolderState(),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow NoNetwork should update the state to Error`() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
value = DataState.NoNetwork(
|
||||
data = listOf(),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
createFolderState(
|
||||
viewState = FoldersState.ViewState.Error(
|
||||
R.string.internet_connection_required_title
|
||||
.asText()
|
||||
.concat(R.string.internet_connection_required_message.asText()),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `FolderStateFlow Pending should update the state to Content`() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
value = DataState.Pending(
|
||||
listOf(DEFAULT_FOLDER_VIEW),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
createFolderState(
|
||||
viewState = FoldersState.ViewState.Content(
|
||||
folderList = listOf(DEFAULT__DISPLAY_FOLDER),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
private fun createViewModel(): FoldersViewModel = FoldersViewModel(
|
||||
vaultRepository = vaultRepository,
|
||||
)
|
||||
|
||||
private fun createFolderState(
|
||||
viewState: FoldersState.ViewState = FoldersState.ViewState.Loading,
|
||||
) = FoldersState(
|
||||
viewState = viewState,
|
||||
)
|
||||
}
|
||||
|
||||
private val DEFAULT_FOLDER_VIEW = FolderView("1", "test", revisionDate = DateTime.now())
|
||||
private val DEFAULT__DISPLAY_FOLDER = FolderDisplayItem("1", "test")
|
||||
|
||||
+220
@@ -0,0 +1,220 @@
|
||||
package com.x8bit.bitwarden.ui.platform.feature.settings.folders.addedit
|
||||
|
||||
import androidx.compose.ui.test.assert
|
||||
import androidx.compose.ui.test.assertIsDisplayed
|
||||
import androidx.compose.ui.test.assertIsNotDisplayed
|
||||
import androidx.compose.ui.test.filterToOne
|
||||
import androidx.compose.ui.test.hasAnyAncestor
|
||||
import androidx.compose.ui.test.isDialog
|
||||
import androidx.compose.ui.test.isPopup
|
||||
import androidx.compose.ui.test.onAllNodesWithText
|
||||
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.ui.platform.base.BaseComposeTest
|
||||
import com.x8bit.bitwarden.ui.platform.base.util.asText
|
||||
import com.x8bit.bitwarden.ui.platform.feature.settings.folders.model.FolderAddEditType
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.update
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.jupiter.api.Assertions.assertTrue
|
||||
|
||||
class FolderAddEditScreenTest : BaseComposeTest() {
|
||||
|
||||
private var onNavigateBackCalled = false
|
||||
|
||||
private val mutableEventFlow = bufferedMutableSharedFlow<FolderAddEditEvent>()
|
||||
private val mutableStateFlow = MutableStateFlow(DEFAULT_STATE_ADD)
|
||||
val viewModel = mockk<FolderAddEditViewModel>(relaxed = true) {
|
||||
every { eventFlow } returns mutableEventFlow
|
||||
every { stateFlow } returns mutableStateFlow
|
||||
}
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
composeTestRule.setContent {
|
||||
FolderAddEditScreen(
|
||||
viewModel = viewModel,
|
||||
onNavigateBack = { onNavigateBackCalled = true },
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NavigateBack should call onNavigateBack`() {
|
||||
mutableEventFlow.tryEmit(FolderAddEditEvent.NavigateBack)
|
||||
assertTrue(onNavigateBackCalled)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clicking save button should send SaveClick action`() {
|
||||
composeTestRule
|
||||
.onNodeWithText("Save")
|
||||
.performClick()
|
||||
|
||||
verify {
|
||||
viewModel.trySendAction(
|
||||
FolderAddEditAction.SaveClick,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `clicking overflow menu and delete, and cancel should dismiss the dialog`() {
|
||||
val deleteText = "Do you really want to delete? This cannot be undone."
|
||||
|
||||
// Open the overflow menu
|
||||
composeTestRule
|
||||
.onNodeWithContentDescription("More")
|
||||
.performClick()
|
||||
// Click on the delete item in the dropdown
|
||||
composeTestRule
|
||||
.onAllNodesWithText("Delete")
|
||||
.filterToOne(hasAnyAncestor(isPopup()))
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(deleteText)
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("Cancel")
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(deleteText)
|
||||
.assertIsNotDisplayed()
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `clicking overflow menu and delete, and delete confirmation again should send a DeleteClick Action`() {
|
||||
val deleteText = "Do you really want to delete? This cannot be undone."
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithContentDescription("More")
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("Delete")
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(deleteText)
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("Delete")
|
||||
.performClick()
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(deleteText)
|
||||
.assertIsNotDisplayed()
|
||||
|
||||
verify {
|
||||
viewModel.trySendAction(
|
||||
FolderAddEditAction.DeleteClick,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `error text should display according to state`() {
|
||||
val message = "An error has occurred"
|
||||
|
||||
mutableStateFlow.update { DEFAULT_STATE_ADD }
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(message)
|
||||
.assertIsNotDisplayed()
|
||||
|
||||
mutableStateFlow.update {
|
||||
DEFAULT_STATE_ADD.copy(
|
||||
viewState = FolderAddEditState.ViewState.Error(message.asText()),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText(message)
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `error dialog should display according to state`() {
|
||||
composeTestRule.onNode(isDialog()).assertDoesNotExist()
|
||||
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
dialog = FolderAddEditState.DialogState.Error(
|
||||
message = "Error Message".asText(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("An error has occurred.")
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
composeTestRule
|
||||
.onNodeWithText("Error Message")
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule.onNode(isDialog()).assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `loading dialog should display according to state`() {
|
||||
composeTestRule.onNode(isDialog()).assertDoesNotExist()
|
||||
|
||||
mutableStateFlow.update {
|
||||
it.copy(
|
||||
dialog = FolderAddEditState.DialogState.Loading(
|
||||
label = "Loading".asText(),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("Loading")
|
||||
.assert(hasAnyAncestor(isDialog()))
|
||||
.assertIsDisplayed()
|
||||
|
||||
composeTestRule.onNode(isDialog()).assertIsDisplayed()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `content should be displayed according to the state`() {
|
||||
composeTestRule
|
||||
.onNodeWithText("TestName")
|
||||
.assertIsNotDisplayed()
|
||||
|
||||
mutableStateFlow.update {
|
||||
DEFAULT_STATE_EDIT.copy(
|
||||
viewState = FolderAddEditState.ViewState.Content(
|
||||
folderName = "TestName",
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
composeTestRule
|
||||
.onNodeWithText("TestName")
|
||||
.assertIsDisplayed()
|
||||
}
|
||||
}
|
||||
|
||||
private val DEFAULT_STATE_ADD = FolderAddEditState(
|
||||
folderAddEditType = FolderAddEditType.AddItem,
|
||||
viewState = FolderAddEditState.ViewState.Loading,
|
||||
dialog = null,
|
||||
)
|
||||
private val DEFAULT_STATE_EDIT = FolderAddEditState(
|
||||
folderAddEditType = FolderAddEditType.EditItem("1"),
|
||||
viewState = FolderAddEditState.ViewState.Loading,
|
||||
dialog = null,
|
||||
)
|
||||
+341
@@ -0,0 +1,341 @@
|
||||
package com.x8bit.bitwarden.ui.platform.feature.settings.folders.addedit
|
||||
|
||||
import androidx.lifecycle.SavedStateHandle
|
||||
import app.cash.turbine.test
|
||||
import com.bitwarden.core.DateTime
|
||||
import com.bitwarden.core.FolderView
|
||||
import com.x8bit.bitwarden.R
|
||||
import com.x8bit.bitwarden.data.platform.repository.model.DataState
|
||||
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
|
||||
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.feature.settings.folders.model.FolderAddEditType
|
||||
import io.mockk.every
|
||||
import io.mockk.mockk
|
||||
import io.mockk.verify
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.jupiter.api.Assertions.assertEquals
|
||||
import org.junit.jupiter.api.Test
|
||||
|
||||
class FolderAddEditViewModelTest : BaseViewModelTest() {
|
||||
|
||||
private val mutableFoldersStateFlow =
|
||||
MutableStateFlow<DataState<FolderView?>>(DataState.Loading)
|
||||
|
||||
private val vaultRepository: VaultRepository = mockk {
|
||||
every { getVaultFolderStateFlow(DEFAULT_EDIT_ITEM_ID) } returns mutableFoldersStateFlow
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial add state should be correct`() = runTest {
|
||||
val folderAddEditType = FolderAddEditType.AddItem
|
||||
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = folderAddEditType,
|
||||
),
|
||||
),
|
||||
)
|
||||
assertEquals(DEFAULT_STATE, viewModel.stateFlow.value)
|
||||
verify(exactly = 0) {
|
||||
vaultRepository.getVaultItemStateFlow(DEFAULT_EDIT_ITEM_ID)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `initial edit state should be correct`() = runTest {
|
||||
val folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID)
|
||||
|
||||
val initState = DEFAULT_STATE.copy(
|
||||
folderAddEditType = folderAddEditType,
|
||||
)
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = folderAddEditType,
|
||||
),
|
||||
),
|
||||
)
|
||||
assertEquals(
|
||||
initState.copy(viewState = FolderAddEditState.ViewState.Loading),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
verify(exactly = 1) {
|
||||
vaultRepository.getVaultFolderStateFlow(DEFAULT_EDIT_ITEM_ID)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `CloseClick should emit NavigateBack`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(FolderAddEditAction.CloseClick)
|
||||
assertEquals(FolderAddEditEvent.NavigateBack, awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `DeleteClick should emit ShowToast`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(FolderAddEditAction.DeleteClick)
|
||||
assertEquals(FolderAddEditEvent.ShowToast("Not yet implemented.".asText()), awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `DismissDialog should emit update dialog state to null`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
viewModel.trySendAction(FolderAddEditAction.DismissDialog)
|
||||
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
dialog = null,
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
expectedState,
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `NameTextChange should update name`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
val expectedState = DEFAULT_STATE.copy(
|
||||
viewState = FolderAddEditState.ViewState.Content(
|
||||
folderName = "NewName",
|
||||
),
|
||||
)
|
||||
|
||||
viewModel.trySendAction(FolderAddEditAction.NameTextChange("NewName"))
|
||||
|
||||
assertEquals(
|
||||
expectedState,
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `SaveClick should emit ShowToast`() = runTest {
|
||||
val viewModel = createViewModel()
|
||||
viewModel.eventFlow.test {
|
||||
viewModel.trySendAction(FolderAddEditAction.SaveClick)
|
||||
assertEquals(FolderAddEditEvent.ShowToast("Not yet implemented.".asText()), awaitItem())
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Error should update state to error`() {
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem((DEFAULT_EDIT_ITEM_ID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
value = DataState.Error(
|
||||
data = null,
|
||||
error = IllegalStateException(),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
|
||||
viewState = FolderAddEditState.ViewState.Error(
|
||||
R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Loaded with data should update state to content`() {
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem((DEFAULT_EDIT_ITEM_ID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
DataState.Loaded(
|
||||
FolderView(
|
||||
DEFAULT_EDIT_ITEM_ID,
|
||||
DEFAULT_FOLDER_NAME,
|
||||
revisionDate = DateTime.now(),
|
||||
),
|
||||
),
|
||||
)
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
|
||||
viewState = FolderAddEditState.ViewState.Content(
|
||||
folderName = DEFAULT_FOLDER_NAME,
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Loaded with empty data should update state to error`() {
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem((DEFAULT_EDIT_ITEM_ID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(DataState.Loaded(null))
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
|
||||
viewState = FolderAddEditState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Loading should update the state to Loading`() {
|
||||
val viewModel = createViewModel()
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
DataState.Loading,
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE,
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow NoNetwork should update the state to Error`() {
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem((DEFAULT_EDIT_ITEM_ID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
value = DataState.NoNetwork(
|
||||
data = null,
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
|
||||
viewState = FolderAddEditState.ViewState.Error(
|
||||
R.string.internet_connection_required_title
|
||||
.asText()
|
||||
.concat(R.string.internet_connection_required_message.asText()),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Pending should update the state to Content`() {
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem((DEFAULT_EDIT_ITEM_ID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(
|
||||
value = DataState.Pending(
|
||||
FolderView(
|
||||
DEFAULT_EDIT_ITEM_ID,
|
||||
DEFAULT_FOLDER_NAME,
|
||||
revisionDate = DateTime.now(),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
|
||||
viewState = FolderAddEditState.ViewState.Content(
|
||||
folderName = DEFAULT_FOLDER_NAME,
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `folderStateFlow Pending with empty data should update state to error`() {
|
||||
val viewModel = createViewModel(
|
||||
savedStateHandle = createSavedStateHandleWithState(
|
||||
state = DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem((DEFAULT_EDIT_ITEM_ID)),
|
||||
),
|
||||
),
|
||||
)
|
||||
|
||||
mutableFoldersStateFlow.tryEmit(DataState.Pending(null))
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
folderAddEditType = FolderAddEditType.EditItem(DEFAULT_EDIT_ITEM_ID),
|
||||
viewState = FolderAddEditState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
private fun createSavedStateHandleWithState(
|
||||
state: FolderAddEditState? = DEFAULT_STATE,
|
||||
) = SavedStateHandle().apply {
|
||||
val folderAddEditType = state?.folderAddEditType
|
||||
?: FolderAddEditType.AddItem
|
||||
|
||||
set("state", state)
|
||||
set(
|
||||
"folder_add_edit_type",
|
||||
when (folderAddEditType) {
|
||||
FolderAddEditType.AddItem -> "add"
|
||||
is FolderAddEditType.EditItem -> "edit"
|
||||
},
|
||||
)
|
||||
set("folder_edit_id", (folderAddEditType as? FolderAddEditType.EditItem)?.folderId)
|
||||
}
|
||||
|
||||
private fun createViewModel(
|
||||
savedStateHandle: SavedStateHandle = createSavedStateHandleWithState(),
|
||||
): FolderAddEditViewModel = FolderAddEditViewModel(
|
||||
savedStateHandle = savedStateHandle,
|
||||
vaultRepository = vaultRepository,
|
||||
)
|
||||
}
|
||||
|
||||
private val DEFAULT_STATE = FolderAddEditState(
|
||||
viewState = FolderAddEditState.ViewState.Loading,
|
||||
dialog = FolderAddEditState.DialogState.Loading("Loading".asText()),
|
||||
folderAddEditType = FolderAddEditType.AddItem,
|
||||
)
|
||||
|
||||
private const val DEFAULT_EDIT_ITEM_ID = "edit_id"
|
||||
private const val DEFAULT_FOLDER_NAME = "test_name"
|
||||
Reference in New Issue
Block a user