Skip to content

Commit

Permalink
Call start processing in view model instead
Browse files Browse the repository at this point in the history
  • Loading branch information
jameswoo-stripe committed Oct 27, 2023
1 parent 01e06a8 commit a5b1121
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ internal fun USBankAccountEmitters(
enabled = hasRequiredFields && !screenState.isProcessing,
merchantName = viewModel.formattedMerchantName(),
onPrimaryButtonClick = viewModel::handlePrimaryButtonClick,
onPrimaryButtonStateChanged = viewModel::handlePrimaryButtonStateChanged
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
}

fun handlePrimaryButtonClick(screenState: USBankAccountFormScreenState) {
handlePrimaryButtonStateChanged(PrimaryButton.State.StartProcessing)
when (screenState) {
is USBankAccountFormScreenState.BillingDetailsCollection -> {
collectBankAccount(args.clientSecret)
Expand Down Expand Up @@ -337,7 +338,7 @@ internal class USBankAccountFormViewModel @Inject internal constructor(
}
}

fun handlePrimaryButtonStateChanged(primaryButtonState: PrimaryButton.State) {
private fun handlePrimaryButtonStateChanged(primaryButtonState: PrimaryButton.State) {
_currentScreenState.update {
it.copy(isProcessing = primaryButtonState.isProcessing)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ internal fun USBankAccountFormArguments.handleScreenStateChanged(
enabled: Boolean,
merchantName: String,
onPrimaryButtonClick: (USBankAccountFormScreenState) -> Unit,
onPrimaryButtonStateChanged: (PrimaryButton.State) -> Unit,
) {
screenState.error?.let {
onError(context.getString(it))
Expand All @@ -22,7 +21,6 @@ internal fun USBankAccountFormArguments.handleScreenStateChanged(
updatePrimaryButton(
text = screenState.primaryButtonText,
onClick = { onPrimaryButtonClick(screenState) },
onStateChanged = { onPrimaryButtonStateChanged(it) },
enabled = enabled,
shouldShowProcessingWhenClicked = showProcessingWhenClicked
)
Expand All @@ -38,7 +36,6 @@ internal fun USBankAccountFormArguments.handleScreenStateChanged(
private fun USBankAccountFormArguments.updatePrimaryButton(
text: String,
onClick: () -> Unit,
onStateChanged: (PrimaryButton.State) -> Unit,
shouldShowProcessingWhenClicked: Boolean,
enabled: Boolean,
) {
Expand All @@ -47,7 +44,6 @@ private fun USBankAccountFormArguments.updatePrimaryButton(
label = text,
onClick = {
if (shouldShowProcessingWhenClicked) {
onStateChanged(PrimaryButton.State.StartProcessing)
onUpdatePrimaryButtonState(PrimaryButton.State.StartProcessing)
}
onClick()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1666,6 +1666,7 @@ class CustomerSheetViewModelTest {
last4 = "6789",
primaryButtonText = "Continue",
mandateText = null,
isProcessing = false,
),
)
val viewModel = createViewModel(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,21 @@ class USBankAccountFormViewModelTest {
)
}

@Test
fun `When the primary button is pressed, the primary button state moves to processing`() = runTest {
val viewModel = createViewModel()

viewModel.currentScreenState.test {
assertThat(awaitItem().isProcessing)
.isFalse()

viewModel.handlePrimaryButtonClick(viewModel.currentScreenState.value)

assertThat(awaitItem().isProcessing)
.isTrue()
}
}

private fun createViewModel(
args: USBankAccountFormViewModel.Args = defaultArgs
): USBankAccountFormViewModel {
Expand Down

0 comments on commit a5b1121

Please sign in to comment.