-
Notifications
You must be signed in to change notification settings - Fork 659
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
[stripe-ui-core] Fix phone number length #6771
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,6 +28,7 @@ import androidx.compose.ui.focus.focusRequester | |
import androidx.compose.ui.focus.onFocusChanged | ||
import androidx.compose.ui.focus.onFocusEvent | ||
import androidx.compose.ui.platform.LocalFocusManager | ||
import androidx.compose.ui.platform.testTag | ||
import androidx.compose.ui.res.stringResource | ||
import androidx.compose.ui.text.input.ImeAction | ||
import androidx.compose.ui.text.input.KeyboardType | ||
|
@@ -39,6 +40,9 @@ import kotlinx.coroutines.job | |
import kotlinx.coroutines.launch | ||
import com.stripe.android.core.R as CoreR | ||
|
||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
const val PHONE_NUMBER_TEXT_FIELD_TAG = "PhoneNumberTextField" | ||
|
||
@Preview | ||
@Composable | ||
private fun PhoneNumberCollectionPreview() { | ||
|
@@ -114,7 +118,8 @@ fun PhoneNumberElementUI( | |
controller.onFocusChange(it.isFocused) | ||
} | ||
hasFocus = it.isFocused | ||
}, | ||
} | ||
.testTag(PHONE_NUMBER_TEXT_FIELD_TAG), | ||
enabled = enabled, | ||
label = { | ||
FormLabel( | ||
|
@@ -163,3 +168,6 @@ fun PhoneNumberElementUI( | |
} | ||
} | ||
} | ||
|
||
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP) | ||
const val PHONE_NUMBER_FIELD_TAG = "phone_number" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this used anywhere? |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,7 +8,7 @@ internal class PhoneNumberFormatterTest { | |
|
||
@Test | ||
fun `Phone number is correctly formatted for US locale`() { | ||
val formatter = PhoneNumberFormatter.forCountry("US") | ||
val formatter = PhoneNumberFormatter.forCountry("US") // "(###) ###-####" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we test every phone number that we support? If not, can we add tests for them? Do you think it is valuable to add tests for each country? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm what we changed is just limiting the length of converted phone number, which is covered by the test, not sure adding a single test for each region adds too much value here, instead I added another test for a customized pattern |
||
|
||
assertThat(formatter.format("123")).isEqualTo("(123") | ||
assertThat(formatter.format("1234")).isEqualTo("(123) 4") | ||
|
@@ -17,21 +17,21 @@ internal class PhoneNumberFormatterTest { | |
assertThat(formatter.format("1234567890")).isEqualTo("(123) 456-7890") | ||
// prefix has 1 digit so full number must be at most 14 digits | ||
assertThat(formatter.format("12345asdfg678901234567890")) | ||
.isEqualTo("(123) 456-7890 1234") | ||
.isEqualTo("(123) 456-7890") | ||
} | ||
|
||
@Test | ||
fun `Phone number is correctly formatted for FI locale`() { | ||
val formatter = PhoneNumberFormatter.forCountry("FI") | ||
val formatter = PhoneNumberFormatter.forCountry("FI") // "## ### ## ##" | ||
|
||
assertThat(formatter.format("123")).isEqualTo("12 3") | ||
assertThat(formatter.format("1234")).isEqualTo("12 34") | ||
assertThat(formatter.format("123456")).isEqualTo("12 345 6") | ||
assertThat(formatter.format("1234567")).isEqualTo("12 345 67") | ||
assertThat(formatter.format("1234567890")).isEqualTo("12 345 67 89 0") | ||
assertThat(formatter.format("1234567890")).isEqualTo("12 345 67 89") | ||
// prefix has 3 digits so full number must be at most 12 digits | ||
assertThat(formatter.format("12345asdfg678901234567890")) | ||
.isEqualTo("12 345 67 89 012") | ||
.isEqualTo("12 345 67 89") | ||
} | ||
|
||
@Test | ||
|
@@ -44,6 +44,23 @@ internal class PhoneNumberFormatterTest { | |
.isEqualTo("+123456789012345") | ||
} | ||
|
||
@Test | ||
fun `WithRegion correctly formats with pattern`() { | ||
val pattern = "(###)-###+#####!#" | ||
val formatter = PhoneNumberFormatter.WithRegion( | ||
PhoneNumberFormatter.Metadata( | ||
"prefix", | ||
"regionCode", | ||
pattern | ||
) | ||
) | ||
|
||
assertThat(formatter.format("123")).isEqualTo("(123") | ||
assertThat(formatter.format("1234567")).isEqualTo("(123)-456+7") | ||
assertThat(formatter.format("123456789012")).isEqualTo("(123)-456+78901!2") | ||
assertThat(formatter.format("123456789012456")).isEqualTo("(123)-456+78901!2") | ||
} | ||
|
||
private fun PhoneNumberFormatter.format(input: String) = | ||
visualTransformation.filter(AnnotatedString(userInputFilter(input))).text.text | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is this being used? If it is not being used externally, can we keep this internal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It allows us to write test to programatically input a valid/invalid phone number and assert other states, such as this