Skip to content

Commit

Permalink
Reduce flakes in PaymentOptionsViewModelTest. (#10228)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaynewstrom-stripe authored Feb 20, 2025
1 parent 4fe2960 commit 78b3179
Showing 1 changed file with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import com.stripe.android.uicore.forms.FormFieldEntry
import com.stripe.android.utils.FakeLinkConfigurationCoordinator
import com.stripe.android.utils.NullCardAccountRangeRepositoryFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
Expand All @@ -59,6 +60,7 @@ import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.RobolectricTestRunner
import kotlin.coroutines.CoroutineContext
import kotlin.test.Test
import com.stripe.android.R as PaymentsCoreR

Expand All @@ -69,6 +71,7 @@ internal class PaymentOptionsViewModelTest {
val rule = InstantTaskExecutorRule()

private val testDispatcher = UnconfinedTestDispatcher()
private val standardTestDispatcher = StandardTestDispatcher()

private val eventReporter = mock<EventReporter>()
private val customerRepository = mock<CustomerRepository>()
Expand Down Expand Up @@ -655,8 +658,10 @@ internal class PaymentOptionsViewModelTest {
}

@Test
fun `Correctly updates state when removing payment method in edit screen succeeds`() = runTest(testDispatcher) {
Dispatchers.setMain(testDispatcher)
fun `Correctly updates state when removing payment method in edit screen succeeds`() = runTest(
standardTestDispatcher
) {
Dispatchers.setMain(standardTestDispatcher)

val cards = PaymentMethodFactory.cards(3)
val paymentMethodToRemove = cards.first()
Expand All @@ -671,6 +676,7 @@ internal class PaymentOptionsViewModelTest {

val viewModel = createViewModel(
args = args,
workContext = standardTestDispatcher,
)

turbineScope {
Expand Down Expand Up @@ -813,7 +819,8 @@ internal class PaymentOptionsViewModelTest {
private fun createViewModel(
args: PaymentOptionContract.Args = PAYMENT_OPTION_CONTRACT_ARGS,
linkState: LinkState? = args.state.paymentMethodMetadata.linkState,
linkConfigurationCoordinator: LinkConfigurationCoordinator = FakeLinkConfigurationCoordinator()
linkConfigurationCoordinator: LinkConfigurationCoordinator = FakeLinkConfigurationCoordinator(),
workContext: CoroutineContext = testDispatcher,
) = TestViewModelFactory.create(linkConfigurationCoordinator) { linkHandler, savedStateHandle ->
PaymentOptionsViewModel(
args = args.copy(
Expand All @@ -825,7 +832,7 @@ internal class PaymentOptionsViewModelTest {
),
eventReporter = eventReporter,
customerRepository = customerRepository,
workContext = testDispatcher,
workContext = workContext,
savedStateHandle = savedStateHandle,
linkHandler = linkHandler,
cardAccountRangeRepositoryFactory = NullCardAccountRangeRepositoryFactory,
Expand Down

0 comments on commit 78b3179

Please sign in to comment.