Skip to content

Commit

Permalink
Fix requirement for phone number not being respected for `AddressElem…
Browse files Browse the repository at this point in the history
…ent` (#9577)

* Fix requirement for phone number not being respected for `AddressElement`

* Add test to `AddressElement`
  • Loading branch information
samer-stripe authored Nov 11, 2024
1 parent af7b2e6 commit a86c9fe
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,33 @@ class AddressElementTest {
assertThat(country()).isEqualTo("US")
}

@Test
fun `when phone number is required, should not be complete until fully entered`() = runTest {
val addressElement = AddressElement(
IdentifierSpec.Generic("address"),
mapOf(
IdentifierSpec.Country to "CA"
),
countryDropdownFieldController = countryDropdownFieldController,
addressType = AddressType.Normal(
phoneNumberState = PhoneNumberState.REQUIRED
),
sameAsShippingElement = null,
shippingValuesMap = null,
)

val phoneNumberController = addressElement.phoneNumberElement.controller

phoneNumberController.onRawValueChange("123")
assertThat(phoneNumberController.isComplete.value).isFalse()

phoneNumberController.onRawValueChange("123456")
assertThat(phoneNumberController.isComplete.value).isFalse()

phoneNumberController.onRawValueChange("1234567890")
assertThat(phoneNumberController.isComplete.value).isTrue()
}

private fun createAddressElement(initialValues: Map<IdentifierSpec, String>): AddressElement {
return AddressElement(
IdentifierSpec.Generic("address"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ open class AddressElement(
PhoneNumberController.createPhoneNumberController(
initialValue = rawValuesMap[IdentifierSpec.Phone] ?: "",
showOptionalLabel = addressType.phoneNumberState == PhoneNumberState.OPTIONAL,
acceptAnyInput = true,
acceptAnyInput = addressType.phoneNumberState != PhoneNumberState.REQUIRED,
)
)

Expand Down

0 comments on commit a86c9fe

Please sign in to comment.