Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -213,24 +213,33 @@ internal class TransferConfirmViewModel(
}
}

private fun validateTransfer() = when {
state.amount.isBlank() -> updateValidationError(Res.string.feature_make_transfer_error_empty_amount)
private fun validateTransfer() {
if (state.amount.isBlank()) {
updateValidationError(Res.string.feature_make_transfer_error_empty_amount)
return
}

state.amount.toDoubleOrNull() == null -> updateValidationError(Res.string.feature_make_transfer_error_invalid_amount)
val transferAmount = state.amount.toDoubleOrNull()
if (transferAmount == null || transferAmount <= 0.0) {
updateValidationError(Res.string.feature_make_transfer_error_invalid_amount)
return
}

state.description.isBlank() -> updateValidationError(Res.string.feature_make_transfer_error_empty_description)
when {
state.description.isBlank() -> updateValidationError(Res.string.feature_make_transfer_error_empty_description)

state.selectedAccount == null -> updateValidationError(Res.string.feature_make_transfer_error_select_account)
state.selectedAccount == null -> updateValidationError(Res.string.feature_make_transfer_error_select_account)

state.selectedAccount?.accountId == state.toAccountId -> {
updateValidationError(Res.string.feature_make_transfer_error_same_account)
}
state.selectedAccount?.accountId == state.toAccountId -> {
updateValidationError(Res.string.feature_make_transfer_error_same_account)
}

state.amount.toDouble() > state.selectedAccountBalance -> {
updateValidationError(Res.string.feature_make_transfer_error_insufficient_balance)
}
transferAmount > state.selectedAccountBalance -> {
updateValidationError(Res.string.feature_make_transfer_error_insufficient_balance)
}

else -> initiateTransfer()
else -> initiateTransfer()
}
}

private fun initiateTransfer() {
Expand Down Expand Up @@ -341,6 +350,7 @@ internal class TransferConfirmViewModel(
private fun updateValidationError(message: StringResource) {
mutableStateFlow.update {
it.copy(
isProcessing = false,
dialogState = TransferConfirmState.DialogState.Error.ValidationError(message),
)
}
Expand Down Expand Up @@ -371,7 +381,8 @@ internal data class TransferConfirmState(
val userVerificationResult: Boolean? = null,
) {
val amountIsValid: Boolean
get() = amount.isNotEmpty() && amount.toDoubleOrNull() != null && amount.toDouble() <= selectedAccountBalance
get() = amount.isNotEmpty() &&
amount.toDoubleOrNull()?.let { it > 0.0 && it <= selectedAccountBalance } == true

val descriptionIsValid: Boolean
get() = description.trim().isNotEmpty()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2026 Mifos Initiative
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*
* See https://github.com/openMF/mobile-wallet/blob/master/LICENSE.md
*/
package org.mifospay.feature.transfer.intrabank.confirm

import kotlin.test.Test
import kotlin.test.assertFalse
import kotlin.test.assertTrue

class TransferConfirmStateTest {

@Test
fun amountIsValidReturnsFalseForZeroAmount() {
val state = TransferConfirmState(
amount = "0",
selectedAccountBalance = 100.0,
)

assertFalse(state.amountIsValid)
}

@Test
fun amountIsValidReturnsFalseForNegativeAmount() {
val state = TransferConfirmState(
amount = "-1",
selectedAccountBalance = 100.0,
)

assertFalse(state.amountIsValid)
}

@Test
fun amountIsValidReturnsTrueForPositiveAmountWithinBalance() {
val state = TransferConfirmState(
amount = "10.50",
selectedAccountBalance = 100.0,
)

assertTrue(state.amountIsValid)
}

@Test
fun amountIsValidReturnsFalseWhenAmountExceedsBalance() {
val state = TransferConfirmState(
amount = "101",
selectedAccountBalance = 100.0,
)

assertFalse(state.amountIsValid)
}

@Test
fun amountIsValidReturnsFalseForNonNumericAmount() {
val state = TransferConfirmState(
amount = "abc",
selectedAccountBalance = 100.0,
)

assertFalse(state.amountIsValid)
}
}