Skip to content

Commit

Permalink
Updates tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosmuvi-stripe committed Mar 20, 2024
1 parent 56b2041 commit f5bc4d0
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,11 @@ internal class FinancialConnectionsSheetActivity : AppCompatActivity() {
)

is FinishWithResult -> {
viewEffect.finishToast?.let {
viewEffect.finishToast?.let { resId ->
Toast.makeText(
this@FinancialConnectionsSheetActivity, it, Toast.LENGTH_LONG
this@FinancialConnectionsSheetActivity,
resId,
Toast.LENGTH_LONG
).show()
}
finishWithResult(viewEffect.result)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.stripe.android.financialconnections

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.TestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.setMain
import org.junit.rules.TestWatcher
import org.junit.runner.Description

@ExperimentalCoroutinesApi
class CoroutineTestRule(
private val testDispatcher: TestDispatcher,
) : TestWatcher() {

override fun starting(description: Description) {
super.starting(description)
Dispatchers.setMain(testDispatcher)
}

override fun finished(description: Description) {
Dispatchers.resetMain()
super.finished(description)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package com.stripe.android.financialconnections

import android.content.Intent
import android.net.Uri
import androidx.lifecycle.SavedStateHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import com.airbnb.mvrx.test.MavericksTestRule
import com.airbnb.mvrx.withState
import com.google.common.truth.Truth.assertThat
import com.stripe.android.core.Logger
import com.stripe.android.core.exception.APIException
Expand Down Expand Up @@ -34,6 +33,7 @@ import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.eq
Expand All @@ -47,7 +47,7 @@ import org.mockito.kotlin.whenever
class FinancialConnectionsSheetViewModelTest {

@get:Rule
val mavericksRule = MavericksTestRule(testDispatcher = UnconfinedTestDispatcher())
val rule: TestRule = CoroutineTestRule(UnconfinedTestDispatcher())

private val eventReporter = mock<FinancialConnectionsEventReporter>()
private val configuration = FinancialConnectionsSheet.Configuration(
Expand All @@ -65,7 +65,8 @@ class FinancialConnectionsSheetViewModelTest {
private val synchronizeFinancialConnectionsSession =
mock<SynchronizeFinancialConnectionsSession>()
private val defaultInitialState = FinancialConnectionsSheetState(
FinancialConnectionsSheetActivityArgs.ForData(configuration)
args = FinancialConnectionsSheetActivityArgs.ForData(configuration),
savedState = null
)

@Test
Expand Down Expand Up @@ -105,7 +106,7 @@ class FinancialConnectionsSheetViewModelTest {
val viewModel = createViewModel(defaultInitialState)

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
require(it.viewEffect is FinishWithResult)
require(it.viewEffect.result is Failed)
Expand Down Expand Up @@ -160,7 +161,7 @@ class FinancialConnectionsSheetViewModelTest {
)

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isEqualTo(
Expand Down Expand Up @@ -188,7 +189,7 @@ class FinancialConnectionsSheetViewModelTest {
)

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isInstanceOf(Failed::class.java)
}
Expand Down Expand Up @@ -232,8 +233,7 @@ class FinancialConnectionsSheetViewModelTest {
// end auth flow
viewModel.handleOnNewIntent(cancelIntent())

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
assertThat(it.viewEffect).isEqualTo(FinishWithResult(Canceled))
}
Expand All @@ -260,7 +260,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.handleOnNewIntent(cancelIntent())

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
require(it.viewEffect is FinishWithResult)
require(it.viewEffect.result is Failed)
assertThat(it.viewEffect.result.error).isInstanceOf(CustomManualEntryRequiredError::class.java)
Expand All @@ -281,7 +281,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.handleOnNewIntent(errorIntent)

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isInstanceOf(Failed::class.java)
Expand All @@ -304,7 +304,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.handleOnNewIntent(successIntent())

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isEqualTo(
Expand All @@ -331,7 +331,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.handleOnNewIntent(Intent().apply { data = Uri.parse(nativeRedirectUrl) })

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.INTERMEDIATE_DEEPLINK)
val viewEffect = it.viewEffect as OpenAuthFlowWithUrl
assertThat(viewEffect.url).isEqualTo(aggregatorUrl)
Expand All @@ -357,7 +357,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.handleOnNewIntent(Intent().apply { data = Uri.parse(returnUrl) })

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.INTERMEDIATE_DEEPLINK)
val viewEffect = it.viewEffect as OpenAuthFlowWithUrl
assertThat(viewEffect.url).isEqualTo(
Expand All @@ -381,7 +381,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.handleOnNewIntent(successIntent())

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isEqualTo(Failed(apiException))
Expand All @@ -402,7 +402,7 @@ class FinancialConnectionsSheetViewModelTest {

// Then
// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.NONE)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isEqualTo(Failed(APIException()))
Expand All @@ -429,7 +429,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.onResume()

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.ON_EXTERNAL_ACTIVITY)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isEqualTo(Canceled)
Expand All @@ -456,7 +456,7 @@ class FinancialConnectionsSheetViewModelTest {
viewModel.onBrowserActivityResult()

// Then
withState(viewModel) {
viewModel.stateFlow.value.let {
assertThat(it.webAuthFlowStatus).isEqualTo(AuthFlowStatus.ON_EXTERNAL_ACTIVITY)
val viewEffect = it.viewEffect as FinishWithResult
assertThat(viewEffect.result).isEqualTo(Canceled)
Expand All @@ -476,7 +476,7 @@ class FinancialConnectionsSheetViewModelTest {
val viewModel = createViewModel(defaultInitialState)

// Then
withState(viewModel) { assertThat(it.manifest).isEqualTo(syncResponse.manifest) }
assertThat(viewModel.stateFlow.value.manifest).isEqualTo(syncResponse.manifest)
}
}

Expand Down Expand Up @@ -517,6 +517,7 @@ class FinancialConnectionsSheetViewModelTest {
nativeRouter = mock(),
analyticsTracker = analyticsTracker,
browserManager = browserManager,
savedStateHandle = SavedStateHandle(),
logger = Logger.noop()
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.stripe.android.financialconnections.features.institutionpicker

import com.airbnb.mvrx.Uninitialized
import com.airbnb.mvrx.test.MavericksTestRule
import com.airbnb.mvrx.withState
import com.stripe.android.core.Logger
import com.stripe.android.financialconnections.ApiKeyFixtures
import com.stripe.android.financialconnections.CoroutineTestRule
import com.stripe.android.financialconnections.FinancialConnectionsSheet
import com.stripe.android.financialconnections.TestFinancialConnectionsAnalyticsTracker
import com.stripe.android.financialconnections.core.Result
import com.stripe.android.financialconnections.domain.FeaturedInstitutions
import com.stripe.android.financialconnections.domain.GetOrFetchSync
import com.stripe.android.financialconnections.domain.PostAuthorizationSession
Expand All @@ -22,10 +21,12 @@ import com.stripe.android.financialconnections.navigation.Destination
import com.stripe.android.financialconnections.utils.TestHandleError
import com.stripe.android.financialconnections.utils.TestNavigationManager
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.advanceUntilIdle
import kotlinx.coroutines.test.runTest
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TestRule
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.verifyNoInteractions
Expand All @@ -38,7 +39,7 @@ import kotlin.test.assertTrue
internal class InstitutionPickerViewModelTest {

@get:Rule
val mavericksTestRule = MavericksTestRule()
val rule: TestRule = CoroutineTestRule(UnconfinedTestDispatcher())

private val searchInstitutions = mock<SearchInstitutions>()
private val featuredInstitutions = mock<FeaturedInstitutions>()
Expand Down Expand Up @@ -92,9 +93,9 @@ internal class InstitutionPickerViewModelTest {

val viewModel = buildViewModel(InstitutionPickerState())

withState(viewModel) { state ->
viewModel.stateFlow.value.let { state ->
assertEquals(state.payload()!!.featuredInstitutions, institutionResponse)
assertIs<Uninitialized>(state.searchInstitutions)
assertIs<Result.Uninitialized>(state.searchInstitutions)
}
}

Expand All @@ -108,10 +109,8 @@ internal class InstitutionPickerViewModelTest {

val viewModel = buildViewModel(InstitutionPickerState())

withState(viewModel) { state ->
// payload with empty list
assertTrue(state.payload()!!.featuredInstitutions.data.isEmpty())
}
// payload with empty list
assertTrue(viewModel.stateFlow.value.payload()!!.featuredInstitutions.data.isEmpty())
}

@Test
Expand All @@ -121,7 +120,7 @@ internal class InstitutionPickerViewModelTest {

val viewModel = buildViewModel(InstitutionPickerState())

withState(viewModel) { state ->
viewModel.stateFlow.value.let { state ->
assertTrue(state.payload() == null)
handleError.assertError(
error = error,
Expand Down Expand Up @@ -170,7 +169,7 @@ internal class InstitutionPickerViewModelTest {
viewModel.onQueryChanged(query)
advanceUntilIdle()

withState(viewModel) { state ->
viewModel.stateFlow.value.let { state ->
assertEquals(state.payload()!!.featuredInstitutions, featuredResults)
assertEquals(state.searchInstitutions()!!, searchResults)
eventTracker.assertContainsEvent(
Expand All @@ -193,7 +192,7 @@ internal class InstitutionPickerViewModelTest {
viewModel.onQueryChanged(query)
advanceUntilIdle()

withState(viewModel) { state ->
viewModel.stateFlow.value.let { state ->
verifyNoInteractions(searchInstitutions)
assertTrue(eventTracker.sentEvents.none { it.eventName == "linked_accounts.search.succeeded" })
assertEquals(state.searchInstitutions()!!.data, emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ internal class FinancialConnectionsSheetNativeStateTest {
reducedManualEntryProminenceInErrors = false,
merchantLogos = emptyList()
)
)
),
savedState = null
).reducedBranding,
).isTrue()
}
Expand All @@ -39,7 +40,8 @@ internal class FinancialConnectionsSheetNativeStateTest {
reducedManualEntryProminenceInErrors = false,
merchantLogos = emptyList()
)
)
),
savedState = null
).reducedBranding,
).isFalse()
}
Expand Down
Loading

0 comments on commit f5bc4d0

Please sign in to comment.