|
| 1 | +package com.stripe.android.financialconnections.lite |
| 2 | + |
| 3 | +import androidx.arch.core.executor.testing.InstantTaskExecutorRule |
| 4 | +import androidx.lifecycle.SavedStateHandle |
| 5 | +import app.cash.turbine.test |
| 6 | +import com.stripe.android.core.Logger |
| 7 | +import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityArgs |
| 8 | +import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityArgs.ForData |
| 9 | +import com.stripe.android.financialconnections.launcher.FinancialConnectionsSheetActivityResult.Failed |
| 10 | +import com.stripe.android.financialconnections.lite.FinancialConnectionsLiteViewModel.ViewEffect |
| 11 | +import com.stripe.android.financialconnections.lite.TextFixtures.configuration |
| 12 | +import com.stripe.android.financialconnections.lite.TextFixtures.financialConnectionsSessionNoAccounts |
| 13 | +import com.stripe.android.financialconnections.lite.TextFixtures.syncResponse |
| 14 | +import com.stripe.android.financialconnections.lite.repository.FinancialConnectionsLiteRepository |
| 15 | +import kotlinx.coroutines.ExperimentalCoroutinesApi |
| 16 | +import kotlinx.coroutines.test.StandardTestDispatcher |
| 17 | +import kotlinx.coroutines.test.TestScope |
| 18 | +import kotlinx.coroutines.test.runTest |
| 19 | +import org.junit.Rule |
| 20 | +import org.junit.Test |
| 21 | +import kotlin.test.assertEquals |
| 22 | + |
| 23 | +@OptIn(ExperimentalCoroutinesApi::class) |
| 24 | +class FinancialConnectionsLiteViewModelTest { |
| 25 | + |
| 26 | + @get:Rule |
| 27 | + val instantTaskExecutorRule = InstantTaskExecutorRule() |
| 28 | + |
| 29 | + private val testDispatcher = StandardTestDispatcher() |
| 30 | + private val testScope = TestScope(testDispatcher) |
| 31 | + |
| 32 | + private val logger: Logger = Logger.noop() |
| 33 | + |
| 34 | + private fun viewModel( |
| 35 | + args: FinancialConnectionsSheetActivityArgs = ForData(configuration), |
| 36 | + repository: FinancialConnectionsLiteRepository |
| 37 | + ) = FinancialConnectionsLiteViewModel( |
| 38 | + logger = logger, |
| 39 | + savedStateHandle = SavedStateHandle( |
| 40 | + mapOf(FinancialConnectionsSheetActivityArgs.EXTRA_ARGS to args) |
| 41 | + ), |
| 42 | + repository = repository, |
| 43 | + workContext = testDispatcher, |
| 44 | + applicationId = "com.example" |
| 45 | + ) |
| 46 | + |
| 47 | + @Test |
| 48 | + fun `test view model initializes with correct state`() = testScope.runTest { |
| 49 | + val viewModel = viewModel( |
| 50 | + repository = TestFinancialConnectionsLiteRepository( |
| 51 | + synchronizeResponse = Result.success(syncResponse), |
| 52 | + sessionResponse = Result.success(financialConnectionsSessionNoAccounts) |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + testDispatcher.scheduler.advanceUntilIdle() |
| 57 | + |
| 58 | + val state = viewModel.state.value |
| 59 | + assertEquals(state!!.successUrl, syncResponse.manifest.successUrl) |
| 60 | + assertEquals(state.cancelUrl, syncResponse.manifest.cancelUrl) |
| 61 | + assertEquals(state.hostedAuthUrl, "${syncResponse.manifest.hostedAuthUrl}&launched_by=android_sdk") |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun `test view effect emits error if sync fails`() = testScope.runTest { |
| 66 | + // Arrange: Return failure for synchronize response |
| 67 | + val expectedError = RuntimeException("Network Error") |
| 68 | + viewModel( |
| 69 | + repository = TestFinancialConnectionsLiteRepository( |
| 70 | + synchronizeResponse = Result.failure(expectedError), |
| 71 | + sessionResponse = Result.success(financialConnectionsSessionNoAccounts) |
| 72 | + ) |
| 73 | + ).viewEffects.test { |
| 74 | + testDispatcher.scheduler.advanceUntilIdle() |
| 75 | + val viewEffect = awaitItem() as ViewEffect.FinishWithResult |
| 76 | + val failedResult = viewEffect.result as Failed |
| 77 | + assertEquals(expectedError, (failedResult.error)) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
0 commit comments