Skip to content

Commit

Permalink
Add test case.
Browse files Browse the repository at this point in the history
  • Loading branch information
brnunes-stripe committed Mar 3, 2021
1 parent 0aea1a2 commit 79b3ca7
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,6 @@ class ExpiryDateEditText @JvmOverloads constructor(
val month = expirationDate.month
val year = expirationDate.year
var shouldShowError = month.length == 2 && !expirationDate.isMonthValid
if (shouldShowError) {
setErrorMessage(resources.getString(R.string.invalid_expiry_month))
}

// Note that we have to check the parts array because afterTextChanged has odd
// behavior when it comes to pasting, where a paste of "1212" triggers this
Expand All @@ -206,14 +203,18 @@ class ExpiryDateEditText @JvmOverloads constructor(
// Here, we have a complete date, so if we've made an invalid one, we want
// to show an error.
shouldShowError = !isDateValid
setErrorMessage(resources.getString(R.string.invalid_expiry_year))
if (!wasComplete && isDateValid) {
completionCallback()
}
} else {
isDateValid = false
}

setErrorMessage(
resources.getString(
if (!expirationDate.isMonthValid) R.string.invalid_expiry_month else R.string.invalid_expiry_year
)
)
this@ExpiryDateEditText.shouldShowError = shouldShowError

formattedDate = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,21 @@ class ExpiryDateEditTextTest {
.isEqualTo(0)
}

@Test
fun inputCompleteDate_whenMonthInvalidAndYearValid_showsInvalidMonth() {
// This test will be invalid if run after the year 2050. Please update the code.
assertThat(Calendar.getInstance().get(Calendar.YEAR) < 2050)
.isTrue()
// Date translates to December, 2012 UNTIL the 2080, at which point it translates to
// December, 2112. Also, this simulates a PASTE action.
expiryDateEditText.append("1550")

assertThat(expiryDateEditText.shouldShowError)
.isTrue()
assertThat(expiryDateEditText.errorMessage)
.isEqualTo(context.getString(R.string.invalid_expiry_month))
}

@Test
fun validatedDate_whenDataIsValid_returnsExpectedValues() {
// This test will be invalid if run after the year 2050. Please update the code.
Expand All @@ -288,7 +303,7 @@ class ExpiryDateEditTextTest {

@Test
fun validatedDate_whenDateIsValidFormatButExpired_returnsNull() {
// This test will be invalid if run after the year 2050. Please update the code.
// This test will be invalid if run after the year 2080. Please update the code.
assertThat(Calendar.getInstance().get(Calendar.YEAR) < 2080)
.isTrue()

Expand Down

0 comments on commit 79b3ca7

Please sign in to comment.