From cc1d9329168fad9513e81dd299af58ef5d040cce Mon Sep 17 00:00:00 2001 From: beatrizmaselli Date: Sat, 14 Sep 2024 14:23:00 +0200 Subject: [PATCH 1/4] add test cases and fix shouldShowNumberKeyboard logic --- CHANGELOG.md | 6 +++ react/PostalCodeGetter.js | 10 ++--- react/transforms/shouldShowNumberKeyboard.js | 30 +++++++++++++ .../shouldShowNumberKeyboard.test.js | 43 +++++++++++++++++++ 4 files changed, 82 insertions(+), 7 deletions(-) create mode 100644 react/transforms/shouldShowNumberKeyboard.js create mode 100644 react/transforms/shouldShowNumberKeyboard.test.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e886598..a1221438 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] +### Added +- Tests to validate if a country should use Number Keyboard (shouldShowNumberKeyboard). + +### Fixed +- Logic to validate if a country should use Number Keyboard (shouldShowNumberKeyboard). + ## [4.25.0] - 2024-09-13 ### Fixed diff --git a/react/PostalCodeGetter.js b/react/PostalCodeGetter.js index f1e5d2be..ceae6563 100644 --- a/react/PostalCodeGetter.js +++ b/react/PostalCodeGetter.js @@ -15,7 +15,7 @@ import { injectAddressContext, addressContextPropTypes, } from './addressContainerContext' -import { removeNonWords } from './transforms/utils' +import { shouldShowNumberKeyboard as determineShouldShowNumberKeyboard } from './transforms/shouldShowNumberKeyboard' class PostalCodeGetter extends Component { render() { @@ -89,12 +89,8 @@ class PostalCodeGetter extends Component { default: case POSTAL_CODE: { const field = getField('postalCode', rules) - const numericString = field.mask ? removeNonWords(field.mask) : '' - const isPurelyNumeric = - numericString === '' || /^\d+$/.test(numericString) - const shouldShowNumberKeyboard = isNaN(field.mask) - ? isPurelyNumeric - : false + const mask = field?.mask + const shouldShowNumberKeyboard = determineShouldShowNumberKeyboard(mask) return ( { + test('Should return true for mask rule undefined', () => { + expect(shouldShowNumberKeyboard(undefined)).toBe(true) + }) + + test('Should return true for mask rule null', () => { + expect(shouldShowNumberKeyboard(null)).toBe(true) + }) + + test('Should return true for mask rule empty string', () => { + expect(shouldShowNumberKeyboard('')).toBe(true) + }) + + test('Should return true for mask rule numeric string without separators', () => { + expect(shouldShowNumberKeyboard('9999')).toBe(true) + }) + + test('Should return true for mask rule numeric string with spaces', () => { + expect(shouldShowNumberKeyboard('999 99')).toBe(true) + }) + + test('Should return true for mask rule numeric string with dashes', () => { + expect(shouldShowNumberKeyboard('9999-99')).toBe(true) + }) + + test('Should return false for mask rulee for string with letters', () => { + expect(shouldShowNumberKeyboard('999AA')).toBe(false) + }) + + test('Should return false for mask rulee for string with mixed letters and spaces', () => { + expect(shouldShowNumberKeyboard('999 AA')).toBe(false) + }) + + test('Should return false for mask rulee for mixed numeric and letter string with dashes', () => { + expect(shouldShowNumberKeyboard('9AA9-99')).toBe(false) + }) + + test('Should return false for mask rulee for NaN', () => { + expect(shouldShowNumberKeyboard(NaN)).toBe(false) + }) +}) From 3fb94aa2b8bb6bad19491952f6b6596029f58d2c Mon Sep 17 00:00:00 2001 From: beatrizmaselli Date: Sat, 14 Sep 2024 14:30:24 +0200 Subject: [PATCH 2/4] remove utils folder --- react/transforms/utils.js | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 react/transforms/utils.js diff --git a/react/transforms/utils.js b/react/transforms/utils.js deleted file mode 100644 index 1fb2b0f8..00000000 --- a/react/transforms/utils.js +++ /dev/null @@ -1,3 +0,0 @@ -export function removeNonWords(string) { - return string && string.replace(/\W/g, '') -} From 8b394ec90135b5a052ce54674e64667cd5e9cf24 Mon Sep 17 00:00:00 2001 From: beatrizmaselli Date: Sat, 14 Sep 2024 14:44:04 +0200 Subject: [PATCH 3/4] adjust comment --- react/country/GBR.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/react/country/GBR.js b/react/country/GBR.js index 38c1c731..59f10756 100644 --- a/react/country/GBR.js +++ b/react/country/GBR.js @@ -17,9 +17,10 @@ export default { maxLength: 50, fixedLabel: 'Postcode', required: true, - // UK has different patterns for postal codes alphanumericals, so we need can't use a mask. + // UK has different patterns for postal codes alphanumericals, so we can't use a mask. mask: NaN, - regex: /^([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$/, + regex: + /^([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$/, postalCodeAPI: false, size: 'small', autoComplete: 'nope', From 00876e3514cde50c28ed4deb913a2d2fa42b1e95 Mon Sep 17 00:00:00 2001 From: beatrizmaselli Date: Sat, 14 Sep 2024 14:44:35 +0200 Subject: [PATCH 4/4] adjust comment --- react/country/GBR.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/react/country/GBR.js b/react/country/GBR.js index 59f10756..fa0c0606 100644 --- a/react/country/GBR.js +++ b/react/country/GBR.js @@ -17,7 +17,7 @@ export default { maxLength: 50, fixedLabel: 'Postcode', required: true, - // UK has different patterns for postal codes alphanumericals, so we can't use a mask. + // UK postal codes have various alphanumeric patterns, so a simple mask cannot be used. mask: NaN, regex: /^([A-Za-z][A-Ha-hJ-Yj-y]?[0-9][A-Za-z0-9]? ?[0-9][A-Za-z]{2}|[Gg][Ii][Rr] ?0[Aa]{2})$/,