Skip to content

Commit

Permalink
Simplify usages of turbineScope (#4167)
Browse files Browse the repository at this point in the history
  • Loading branch information
david-livefront authored Oct 25, 2024
1 parent f8bf864 commit 0960f61
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4309,18 +4309,11 @@ class VaultRepositoryTest {
)
} returns Throwable().asFailure()

turbineScope {
val expected = DecryptFido2CredentialAutofillViewResult.Error
val result = vaultRepository
.getDecryptedFido2CredentialAutofillViews(
cipherViewList = cipherViewList,
)
val result = vaultRepository.getDecryptedFido2CredentialAutofillViews(
cipherViewList = cipherViewList,
)

assertEquals(
expected,
result,
)
}
assertEquals(DecryptFido2CredentialAutofillViewResult.Error, result)
coVerify {
vaultSdkSource.decryptFido2CredentialAutofillViews(
userId = MOCK_USER_STATE.activeUserId,
Expand All @@ -4346,17 +4339,11 @@ class VaultRepositoryTest {
)
} returns autofillViewList.asSuccess()

turbineScope {
val result = vaultRepository
.getDecryptedFido2CredentialAutofillViews(
cipherViewList = cipherViewList,
)
val result = vaultRepository.getDecryptedFido2CredentialAutofillViews(
cipherViewList = cipherViewList,
)

assertEquals(
expected,
result,
)
}
assertEquals(expected, result)
coVerify {
vaultSdkSource.decryptFido2CredentialAutofillViews(
userId = MOCK_USER_STATE.activeUserId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package com.x8bit.bitwarden.ui.auth.feature.completeregistration

import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_0
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_1
Expand Down Expand Up @@ -154,9 +153,7 @@ class CompleteRegistrationViewModelTest : BaseViewModelTest() {
)
} returns RegisterResult.Success(captchaToken = CAPTCHA_BYPASS_TOKEN)
val viewModel = createCompleteRegistrationViewModel(VALID_INPUT_STATE)
turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem())
viewModel.trySendAction(CompleteRegistrationAction.CallToActionClick)
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.x8bit.bitwarden.ui.auth.feature.createaccount
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_0
import com.x8bit.bitwarden.data.auth.datasource.sdk.model.PasswordStrength.LEVEL_1
Expand Down Expand Up @@ -243,9 +242,7 @@ class CreateAccountViewModelTest : BaseViewModelTest() {
savedStateHandle = validInputHandle,
authRepository = repo,
)
turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem())
viewModel.trySendAction(CreateAccountAction.SubmitClick)
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.x8bit.bitwarden.ui.auth.feature.enterprisesignon
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.LoginResult
Expand Down Expand Up @@ -511,10 +510,7 @@ class EnterpriseSignOnViewModelTest : BaseViewModelTest() {
)
val ssoCallbackResult = SsoCallbackResult.Success(state = "abc", code = "lmn")

turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)

viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(initialState, stateFlow.awaitItem())

mutableSsoCallbackResultFlow.tryEmit(ssoCallbackResult)
Expand Down Expand Up @@ -566,10 +562,7 @@ class EnterpriseSignOnViewModelTest : BaseViewModelTest() {
)
val ssoCallbackResult = SsoCallbackResult.Success(state = "abc", code = "lmn")

turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)

viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(initialState, stateFlow.awaitItem())

mutableSsoCallbackResultFlow.tryEmit(ssoCallbackResult)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.x8bit.bitwarden.ui.auth.feature.startregistration
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.repository.AuthRepository
import com.x8bit.bitwarden.data.auth.repository.model.SendVerificationEmailResult
Expand Down Expand Up @@ -177,9 +176,7 @@ class StartRegistrationViewModelTest : BaseViewModelTest() {
environmentRepository = fakeEnvironmentRepository,
featureFlagManager = featureFlagManager,
)
turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(VALID_INPUT_STATE, stateFlow.awaitItem())
viewModel.trySendAction(ContinueClick)
assertEquals(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.x8bit.bitwarden.ui.vault.feature.addedit
import android.content.pm.SigningInfo
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.bitwarden.send.SendView
import com.bitwarden.vault.CipherView
import com.bitwarden.vault.CollectionView
Expand Down Expand Up @@ -611,25 +610,22 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
vaultRepository.createCipherInOrganization(any(), any())
} returns CreateCipherResult.Success

turbineScope {
val stateTurbine = viewModel.stateFlow.testIn(backgroundScope)
val eventTurbine = viewModel.eventFlow.testIn(backgroundScope)

viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
viewModel.trySendAction(VaultAddEditAction.Common.SaveClick)

assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(stateWithDialog, stateTurbine.awaitItem())
assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(stateWithName, stateFlow.awaitItem())
assertEquals(stateWithDialog, stateFlow.awaitItem())
assertEquals(stateWithName, stateFlow.awaitItem())

assertEquals(
VaultAddEditEvent.ShowToast(
R.string.new_item_created.asText(),
),
eventTurbine.awaitItem(),
eventFlow.awaitItem(),
)
assertEquals(
VaultAddEditEvent.NavigateBack,
eventTurbine.awaitItem(),
eventFlow.awaitItem(),
)
}
coVerify(exactly = 1) {
Expand Down Expand Up @@ -680,19 +676,16 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
vaultRepository.createCipherInOrganization(any(), any())
} returns CreateCipherResult.Success

turbineScope {
val stateTurbine = viewModel.stateFlow.testIn(backgroundScope)
val eventTurbine = viewModel.eventFlow.testIn(backgroundScope)

viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
viewModel.trySendAction(VaultAddEditAction.Common.SaveClick)

assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(stateWithDialog, stateTurbine.awaitItem())
assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(stateWithName, stateFlow.awaitItem())
assertEquals(stateWithDialog, stateFlow.awaitItem())
assertEquals(stateWithName, stateFlow.awaitItem())

assertEquals(
VaultAddEditEvent.ExitApp,
eventTurbine.awaitItem(),
eventFlow.awaitItem(),
)
}
assertNull(specialCircumstanceManager.specialCircumstance)
Expand Down Expand Up @@ -821,17 +814,14 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
} returns mockAttestationOptions
every { authRepository.activeUserId } returns "mockUserId"

turbineScope {
val stateTurbine = viewModel.stateFlow.testIn(backgroundScope)
val eventTurbine = viewModel.eventFlow.testIn(backgroundScope)

viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
viewModel.trySendAction(VaultAddEditAction.Common.SaveClick)

assertEquals(stateWithNewLogin, stateTurbine.awaitItem())
assertEquals(stateWithSavingDialog, stateTurbine.awaitItem())
assertEquals(stateWithNewLogin, stateFlow.awaitItem())
assertEquals(stateWithSavingDialog, stateFlow.awaitItem())
assertEquals(
VaultAddEditEvent.Fido2UserVerification(isRequired = true),
eventTurbine.awaitItem(),
eventFlow.awaitItem(),
)
}
}
Expand Down Expand Up @@ -899,22 +889,19 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {
} returns mockAttestationOptions
every { authRepository.activeUserId } returns mockUserId

turbineScope {
val stateTurbine = viewModel.stateFlow.testIn(backgroundScope)
val eventTurbine = viewModel.eventFlow.testIn(backgroundScope)

viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
viewModel.trySendAction(VaultAddEditAction.Common.SaveClick)

assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(stateWithSavingDialog, stateTurbine.awaitItem())
assertEquals(stateWithName, stateFlow.awaitItem())
assertEquals(stateWithSavingDialog, stateFlow.awaitItem())
assertEquals(
VaultAddEditEvent.ShowToast(R.string.item_updated.asText()),
eventTurbine.awaitItem(),
eventFlow.awaitItem(),
)
assertEquals(stateWithName, stateTurbine.awaitItem())
assertEquals(stateWithName, stateFlow.awaitItem())
assertEquals(
VaultAddEditEvent.CompleteFido2Registration(mockCreateResult),
eventTurbine.awaitItem(),
eventFlow.awaitItem(),
)
coVerify(exactly = 1) {
fido2CredentialManager.registerFido2Credential(
Expand Down Expand Up @@ -1059,11 +1046,10 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {

viewModel.trySendAction(VaultAddEditAction.Common.SaveClick)

turbineScope {
val eventTurbine = viewModel.eventFlow.testIn(backgroundScope)
viewModel.eventFlow.test {
assertEquals(
VaultAddEditEvent.Fido2UserVerification(isRequired = false),
eventTurbine.awaitItem(),
awaitItem(),
)
}
}
Expand Down Expand Up @@ -1105,11 +1091,10 @@ class VaultAddEditViewModelTest : BaseViewModelTest() {

viewModel.trySendAction(VaultAddEditAction.Common.SaveClick)

turbineScope {
val eventTurbine = viewModel.eventFlow.testIn(backgroundScope)
viewModel.eventFlow.test {
assertEquals(
VaultAddEditEvent.Fido2UserVerification(isRequired = true),
eventTurbine.awaitItem(),
awaitItem(),
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.x8bit.bitwarden.ui.vault.feature.importlogins

import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.vault.repository.VaultRepository
import com.x8bit.bitwarden.data.vault.repository.model.SyncVaultDataResult
Expand Down Expand Up @@ -95,9 +94,7 @@ class ImportLoginsViewModelTest : BaseViewModelTest() {
@Test
fun `ConfirmImportLater sets dialog state to null and sends NavigateBack event`() = runTest {
val viewModel = createViewModel()
turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
// Initial state
assertEquals(DEFAULT_STATE, stateFlow.awaitItem())

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.x8bit.bitwarden.ui.vault.feature.item
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import app.cash.turbine.test
import app.cash.turbine.turbineScope
import com.bitwarden.vault.CipherView
import com.x8bit.bitwarden.R
import com.x8bit.bitwarden.data.auth.datasource.disk.model.OnboardingStatus
Expand Down Expand Up @@ -653,9 +652,7 @@ class VaultItemViewModelTest : BaseViewModelTest() {
val loginState = DEFAULT_STATE.copy(viewState = loginViewState)
val viewModel = createViewModel(state = loginState)

turbineScope {
val stateFlow = viewModel.stateFlow.testIn(backgroundScope)
val eventFlow = viewModel.eventFlow.testIn(backgroundScope)
viewModel.stateEventFlow(backgroundScope) { stateFlow, eventFlow ->
assertEquals(loginState, stateFlow.awaitItem())
viewModel.trySendAction(
VaultItemAction.Common.MasterPasswordSubmit(
Expand Down

0 comments on commit 0960f61

Please sign in to comment.