mirror of
https://github.com/bitwarden/android.git
synced 2026-11-19 17:40:12 -06:00
BIT-2273: Maintain data when combining data state (#1298)
This commit is contained in:
committed by
Álison Fernandes
parent
4326630d10
commit
b730330196
+178
@@ -9,6 +9,184 @@ import org.junit.jupiter.api.Test
|
||||
|
||||
class DataStateExtensionsTest {
|
||||
|
||||
@Test
|
||||
fun `map on Loaded should call transform and return appropriate value for Loaded`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.Loaded(mapValue)
|
||||
val dataState = DataState.Loaded("Loaded")
|
||||
|
||||
val result = dataState.map {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `map on Loading should not call transform and return Loading`() {
|
||||
val dataState = DataState.Loading
|
||||
|
||||
val result = dataState.map {
|
||||
5
|
||||
}
|
||||
|
||||
assertEquals(dataState, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `map on Pending should call transform and return Pending`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.Pending(mapValue)
|
||||
val dataState = DataState.Pending("Pending")
|
||||
|
||||
val result = dataState.map {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `map on Error should not call transform with null data and return Error`() {
|
||||
val error = Throwable()
|
||||
val dataState = DataState.Error(error, null)
|
||||
|
||||
val result = dataState.map {
|
||||
5
|
||||
}
|
||||
|
||||
assertEquals(dataState, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `map on Error should call transform with nonnull data and return Error`() {
|
||||
val error = Throwable()
|
||||
val mapValue = 5
|
||||
val expected = DataState.Error(error, mapValue)
|
||||
val dataState = DataState.Error(error, "Error")
|
||||
|
||||
val result = dataState.map {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `map on NoNetwork should not call transform with null data and return NoNetwork`() {
|
||||
val dataState = DataState.NoNetwork(null)
|
||||
|
||||
val result = dataState.map {
|
||||
5
|
||||
}
|
||||
|
||||
assertEquals(dataState, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `map on NoNetwork should call transform with nonnull data and return NoNetwork`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.NoNetwork(mapValue)
|
||||
val dataState = DataState.NoNetwork("NoNetwork")
|
||||
|
||||
val result = dataState.map {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on Loaded should call transform and return appropriate value for Loaded`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.Loaded(mapValue)
|
||||
val dataState = DataState.Loaded("Loaded")
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on Loading should not call transform and return Loading`() {
|
||||
val dataState = DataState.Loading
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
5
|
||||
}
|
||||
|
||||
assertEquals(dataState, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on Pending should call transform and return Pending`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.Pending(mapValue)
|
||||
val dataState = DataState.Pending("Pending")
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on Error should call transform with null data and return Error`() {
|
||||
val error = Throwable()
|
||||
val mapValue = 5
|
||||
val expected = DataState.Error(error, mapValue)
|
||||
val dataState = DataState.Error(error, null)
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on Error should call transform with nonnull data and return Error`() {
|
||||
val error = Throwable()
|
||||
val mapValue = 5
|
||||
val expected = DataState.Error(error, mapValue)
|
||||
val dataState = DataState.Error(error, "Error")
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on NoNetwork should call transform with null data and return NoNetwork`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.NoNetwork(mapValue)
|
||||
val dataState = DataState.NoNetwork(null)
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `mapNullable on NoNetwork should call transform with nonnull data and return NoNetwork`() {
|
||||
val mapValue = 5
|
||||
val expected = DataState.NoNetwork(5)
|
||||
val dataState = DataState.NoNetwork("NoNetwork")
|
||||
|
||||
val result = dataState.mapNullable {
|
||||
mapValue
|
||||
}
|
||||
|
||||
assertEquals(expected, result)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `takeUtilLoaded should complete after a Loaded state is emitted`() = runTest {
|
||||
val mutableStateFlow = MutableStateFlow<DataState<Unit>>(DataState.Loading)
|
||||
|
||||
+153
@@ -22,6 +22,7 @@ import com.x8bit.bitwarden.data.vault.repository.model.DownloadAttachmentResult
|
||||
import com.x8bit.bitwarden.data.vault.repository.model.RestoreCipherResult
|
||||
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.vault.feature.item.model.TotpCodeItemData
|
||||
import com.x8bit.bitwarden.ui.vault.feature.item.util.createCommonContent
|
||||
import com.x8bit.bitwarden.ui.vault.feature.item.util.createLoginContent
|
||||
@@ -1949,6 +1950,158 @@ class VaultItemViewModelTest : BaseViewModelTest() {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
inner class VaultItemFlow {
|
||||
@BeforeEach
|
||||
fun setup() {
|
||||
mutableUserStateFlow.value = DEFAULT_USER_STATE
|
||||
mutableAuthCodeItemFlow.value = DataState.Loaded(data = null)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with Loading should update the dialog state to loading`() {
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Loading
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(viewState = VaultItemState.ViewState.Loading),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with Loaded and nonnull data should update the ViewState`() {
|
||||
val viewState = mockk<VaultItemState.ViewState>()
|
||||
val cipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(isPremiumUser = true, totpCodeItemData = null)
|
||||
} returns viewState
|
||||
}
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Loaded(data = cipherView)
|
||||
|
||||
assertEquals(DEFAULT_STATE.copy(viewState = viewState), viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with Loaded and null data should update the ViewState to Error`() {
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Loaded(data = null)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = VaultItemState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with Pending and nonnull data should update the ViewState`() {
|
||||
val viewState = mockk<VaultItemState.ViewState>()
|
||||
val cipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(isPremiumUser = true, totpCodeItemData = null)
|
||||
} returns viewState
|
||||
}
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Pending(data = cipherView)
|
||||
|
||||
assertEquals(DEFAULT_STATE.copy(viewState = viewState), viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `on VaultDataReceive with Pending and null data should update the ViewState to Error`() {
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Pending(data = null)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = VaultItemState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with Error and nonnull data should update the ViewState`() {
|
||||
val viewState = mockk<VaultItemState.ViewState>()
|
||||
val cipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(isPremiumUser = true, totpCodeItemData = null)
|
||||
} returns viewState
|
||||
}
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Error(error = Throwable(), data = cipherView)
|
||||
|
||||
assertEquals(DEFAULT_STATE.copy(viewState = viewState), viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with Error and null data should update the ViewState to Error`() {
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.Error(error = Throwable(), data = null)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = VaultItemState.ViewState.Error(
|
||||
message = R.string.generic_error_message.asText(),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `on VaultDataReceive with NoNetwork and nonnull data should update the ViewState`() {
|
||||
val viewState = mockk<VaultItemState.ViewState>()
|
||||
val cipherView = mockk<CipherView> {
|
||||
every {
|
||||
toViewState(isPremiumUser = true, totpCodeItemData = null)
|
||||
} returns viewState
|
||||
}
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.NoNetwork(data = cipherView)
|
||||
|
||||
assertEquals(DEFAULT_STATE.copy(viewState = viewState), viewModel.stateFlow.value)
|
||||
}
|
||||
|
||||
@Suppress("MaxLineLength")
|
||||
@Test
|
||||
fun `on VaultDataReceive with NoNetwork and null data should update the ViewState to Error`() {
|
||||
val viewModel = createViewModel(state = null)
|
||||
|
||||
mutableVaultItemFlow.value = DataState.NoNetwork(data = null)
|
||||
|
||||
assertEquals(
|
||||
DEFAULT_STATE.copy(
|
||||
viewState = VaultItemState.ViewState.Error(
|
||||
message = R.string.internet_connection_required_title
|
||||
.asText()
|
||||
.concat(
|
||||
" ".asText(),
|
||||
R.string.internet_connection_required_message.asText(),
|
||||
),
|
||||
),
|
||||
),
|
||||
viewModel.stateFlow.value,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Suppress("LongParameterList")
|
||||
private fun createViewModel(
|
||||
state: VaultItemState?,
|
||||
|
||||
Reference in New Issue
Block a user