Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Woo POS][Cash & Receipts] Validate empty cash user input as zero #14954

Merged
Merged
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 @@ -36,8 +36,10 @@ final class CollectCashViewHelper {
func validateAmountOnSubmit(orderTotal: String,
textFieldAmountInput: String,
onError: (String) -> Void) -> Bool {
let userInput = textFieldAmountInput.isEmpty ? "0" : textFieldAmountInput
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I'd do this the other way around – it's easier to understand if the common case comes first (by checking .isNotEmpty,) then the fallback.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense, updated: 29b3990


guard let orderDecimal = parseCurrency(orderTotal),
let inputDecimal = parseCurrency(textFieldAmountInput) else {
let inputDecimal = parseCurrency(userInput) else {
onError(Localization.failedToCollectCashPayment)
return false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,59 @@ struct CollectCashViewHelperTests {
#expect(result == false)
#expect(capturedErrorMessage == expectedErrorMessage)
}

@Test func validateAmountOnSubmit_when_input_is_empty_then_returns_true() {
// Given
let orderTotal = "$0.00"
let inputAmount = ""

// When
let result = sut.validateAmountOnSubmit(
orderTotal: orderTotal,
textFieldAmountInput: inputAmount,
onError: { _ in })

// Then
#expect(result == true)
}

@Test func validateAmountOnSubmit_when_orderTotal_is_empty_then_returns_false_with_expected_error_message() {
// Given
let orderTotal = ""
let inputAmount = ""
let expectedErrorMessage = "Error trying to process payment. Try again."
var capturedErrorMessage: String?

// When
let result = sut.validateAmountOnSubmit(
orderTotal: orderTotal,
textFieldAmountInput: inputAmount,
onError: { error in
capturedErrorMessage = error
})

// Then
#expect(result == false)
#expect(capturedErrorMessage == expectedErrorMessage)
}

@Test func validateAmountOnSubmit_when_inputDecimal_is_empty_and_less_than_orderTotal_then_returns_false_with_expected_error_message() {
// Given
let orderTotal = "$1.00"
let inputAmount = ""
let expectedErrorMessage = "Amount must be more or equal to total."
var capturedErrorMessage: String?

// When
let result = sut.validateAmountOnSubmit(
orderTotal: orderTotal,
textFieldAmountInput: inputAmount,
onError: { error in
capturedErrorMessage = error
})

// Then
#expect(result == false)
#expect(capturedErrorMessage == expectedErrorMessage)
}
}
Loading