Updated on 2026-08-14
This commit is contained in:
parent
63698bed23
commit
189cb19ff2
4 changed files with 138 additions and 6 deletions
|
|
@ -25,6 +25,7 @@ dependencies {
|
|||
implementation(projects.domain.walletManager)
|
||||
implementation(projects.libs.blockchainSdk)
|
||||
implementation(projects.libs.tangemSdkApi)
|
||||
implementation(projects.domain.account)
|
||||
implementation(projects.domain.models)
|
||||
implementation(projects.domain.tokens)
|
||||
implementation(projects.domain.card)
|
||||
|
|
|
|||
|
|
@ -1,13 +1,13 @@
|
|||
package com.tangem.domain.wallets.usecase
|
||||
|
||||
import arrow.core.Either
|
||||
import com.tangem.domain.tokens.repository.CurrenciesRepository
|
||||
import com.tangem.domain.account.repository.AccountsCRUDRepository
|
||||
import com.tangem.domain.models.wallet.UserWalletId
|
||||
import com.tangem.domain.wallets.repository.WalletsRepository
|
||||
|
||||
class SetNotificationsEnabledUseCase(
|
||||
private val walletsRepository: WalletsRepository,
|
||||
private val currenciesRepository: CurrenciesRepository,
|
||||
private val accountsCRUDRepository: AccountsCRUDRepository,
|
||||
) {
|
||||
|
||||
suspend operator fun invoke(userWalletId: UserWalletId, isEnabled: Boolean): Either<Throwable, Unit> =
|
||||
|
|
@ -16,7 +16,7 @@ class SetNotificationsEnabledUseCase(
|
|||
userWalletId = userWalletId,
|
||||
isEnabled = isEnabled,
|
||||
)
|
||||
currenciesRepository.syncTokens(userWalletId)
|
||||
accountsCRUDRepository.syncTokens(userWalletId)
|
||||
}.onLeft {
|
||||
walletsRepository.setNotificationsEnabled(
|
||||
userWalletId = userWalletId,
|
||||
|
|
|
|||
|
|
@ -0,0 +1,131 @@
|
|||
package com.tangem.domain.wallets.usecase
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import com.tangem.domain.account.repository.AccountsCRUDRepository
|
||||
import com.tangem.domain.models.wallet.UserWalletId
|
||||
import com.tangem.domain.wallets.repository.WalletsRepository
|
||||
import io.mockk.coEvery
|
||||
import io.mockk.coVerify
|
||||
import io.mockk.coVerifyOrder
|
||||
import io.mockk.just
|
||||
import io.mockk.mockk
|
||||
import io.mockk.runs
|
||||
import kotlinx.coroutines.test.runTest
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
|
||||
class SetNotificationsEnabledUseCaseTest {
|
||||
|
||||
private lateinit var useCase: SetNotificationsEnabledUseCase
|
||||
private lateinit var walletsRepository: WalletsRepository
|
||||
private lateinit var accountsCRUDRepository: AccountsCRUDRepository
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
walletsRepository = mockk()
|
||||
accountsCRUDRepository = mockk()
|
||||
useCase = SetNotificationsEnabledUseCase(
|
||||
walletsRepository = walletsRepository,
|
||||
accountsCRUDRepository = accountsCRUDRepository,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN notifications enabled successfully WHEN invoke THEN return Right with Unit`() = runTest {
|
||||
// GIVEN
|
||||
val userWalletId = UserWalletId("0A0B0C0D")
|
||||
val isEnabled = true
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs
|
||||
coEvery { accountsCRUDRepository.syncTokens(userWalletId) } just runs
|
||||
|
||||
// WHEN
|
||||
val result = useCase(userWalletId, isEnabled)
|
||||
|
||||
// THEN
|
||||
assertThat(result.isRight()).isTrue()
|
||||
coVerifyOrder {
|
||||
walletsRepository.setNotificationsEnabled(userWalletId, isEnabled)
|
||||
accountsCRUDRepository.syncTokens(userWalletId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN notifications disabled successfully WHEN invoke THEN return Right with Unit`() = runTest {
|
||||
// GIVEN
|
||||
val userWalletId = UserWalletId("0A0B0C0D")
|
||||
val isEnabled = false
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs
|
||||
coEvery { accountsCRUDRepository.syncTokens(userWalletId) } just runs
|
||||
|
||||
// WHEN
|
||||
val result = useCase(userWalletId, isEnabled)
|
||||
|
||||
// THEN
|
||||
assertThat(result.isRight()).isTrue()
|
||||
coVerifyOrder {
|
||||
walletsRepository.setNotificationsEnabled(userWalletId, isEnabled)
|
||||
accountsCRUDRepository.syncTokens(userWalletId)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN setNotificationsEnabled throws exception WHEN invoke THEN return Left and revert notifications`() = runTest {
|
||||
// GIVEN
|
||||
val userWalletId = UserWalletId("0A0B0C0D")
|
||||
val isEnabled = true
|
||||
val exception = RuntimeException("Network error")
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } throws exception
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs
|
||||
|
||||
// WHEN
|
||||
val result = useCase(userWalletId, isEnabled)
|
||||
|
||||
// THEN
|
||||
assertThat(result.isLeft()).isTrue()
|
||||
result.onLeft { throwable ->
|
||||
assertThat(throwable).isEqualTo(exception)
|
||||
}
|
||||
coVerify { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN syncTokens throws exception WHEN invoke THEN return Left and revert notifications`() = runTest {
|
||||
// GIVEN
|
||||
val userWalletId = UserWalletId("0A0B0C0D")
|
||||
val isEnabled = true
|
||||
val exception = RuntimeException("Sync error")
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } just runs
|
||||
coEvery { accountsCRUDRepository.syncTokens(userWalletId) } throws exception
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs
|
||||
|
||||
// WHEN
|
||||
val result = useCase(userWalletId, isEnabled)
|
||||
|
||||
// THEN
|
||||
assertThat(result.isLeft()).isTrue()
|
||||
result.onLeft { throwable ->
|
||||
assertThat(throwable).isEqualTo(exception)
|
||||
}
|
||||
coVerify { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) }
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `GIVEN disabling notifications fails WHEN invoke THEN return Left and revert to enabled`() = runTest {
|
||||
// GIVEN
|
||||
val userWalletId = UserWalletId("0A0B0C0D")
|
||||
val isEnabled = false
|
||||
val exception = RuntimeException("Network error")
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, isEnabled) } throws exception
|
||||
coEvery { walletsRepository.setNotificationsEnabled(userWalletId, !isEnabled) } just runs
|
||||
|
||||
// WHEN
|
||||
val result = useCase(userWalletId, isEnabled)
|
||||
|
||||
// THEN
|
||||
assertThat(result.isLeft()).isTrue()
|
||||
result.onLeft { throwable ->
|
||||
assertThat(throwable).isEqualTo(exception)
|
||||
}
|
||||
coVerify { walletsRepository.setNotificationsEnabled(userWalletId, true) }
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue