diff --git a/packages/components/package-lock.json b/packages/components/package-lock.json index 457cca5..a9bfd2e 100644 --- a/packages/components/package-lock.json +++ b/packages/components/package-lock.json @@ -1,12 +1,12 @@ { "name": "@jmstechnologiesinc/react-native-components", - "version": "0.7.0", + "version": "0.7.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@jmstechnologiesinc/react-native-components", - "version": "0.7.0", + "version": "0.7.1", "license": "ISC", "devDependencies": { "@jmstechnologiesinc/partner": "0.2.54", diff --git a/packages/components/package.json b/packages/components/package.json index 07f4169..f27a14f 100644 --- a/packages/components/package.json +++ b/packages/components/package.json @@ -1,6 +1,6 @@ { "name": "@jmstechnologiesinc/react-native-components", - "version": "0.7.0", + "version": "0.7.1", "description": "Biblioteca de componentes de interfaz compartida por las aplicaciones de Daxir.", "author": "yerloveraaa ", "homepage": "https://github.com/jmstechnologiesinc/react-native-components#readme", diff --git a/packages/components/src/Form/FormPhoneNumber.js b/packages/components/src/Form/FormPhoneNumber.js index fefdc13..13a6444 100644 --- a/packages/components/src/Form/FormPhoneNumber.js +++ b/packages/components/src/Form/FormPhoneNumber.js @@ -19,6 +19,9 @@ const FormPhoneNumber = ({ showSubmitButton = true, showPhoneNumberValidationError = true, showPhoneVerificationCodeAgreement = true, + // The number is shown, not asked: a code sent to the number the account already has (the app's step-up). The + // field takes no typing and its flag opens no country list. + readOnly = false, isLoading, vereficationCodeError, onPhoneNumberLoginPress, @@ -75,8 +78,9 @@ const FormPhoneNumber = ({ error={showPhoneNumberValidationError && !value} initialCountry="us" initialValue={value} + disabled={readOnly} onChangePhoneNumber={onChangeText} - onPressFlag={onFlagPress} + onPressFlag={readOnly ? undefined : onFlagPress} /> {showPhoneNumberValidationError && !value ? ( diff --git a/packages/components/src/Form/__tests__/FormPhoneNumber.test.js b/packages/components/src/Form/__tests__/FormPhoneNumber.test.js new file mode 100644 index 0000000..7fc4392 --- /dev/null +++ b/packages/components/src/Form/__tests__/FormPhoneNumber.test.js @@ -0,0 +1,48 @@ +// Form.PhoneNumber asks for a number by default; `readOnly` shows the one it is given (the app's step-up sends its +// code to the account's own phone): no typing, and the flag opens no country list. +jest.mock('react-native-localize', () => ({ + findBestLanguageTag: jest.fn(() => ({ languageTag: 'en', isRTL: false })), +})); +jest.mock('@jmstechnologiesinc/react-native-phone-input', () => ({ PhoneInput: 'PhoneInput' })); +jest.mock('../FormVerificationCode', () => 'FormVerificationCode'); +jest.mock('../CountryPicker', () => 'CountryPicker'); +// The section frame alone: the package's ScreenWrapper entry also loads the camera's native modules. +jest.mock('../../ScreenWrapper', () => ({ __esModule: true, default: { Section: 'ScreenWrapper.Section' } })); + +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; + +import { MD3LightTheme, Provider } from '@jmstechnologiesinc/react-native-paper'; + +import FormPhoneNumber from '../FormPhoneNumber'; + +const phoneInputOf = (props) => { + let tree; + act(() => { + tree = renderer.create( + + {}} {...props} /> + + ); + }); + const { props: phoneInput } = tree.root.findByType('PhoneInput'); + act(() => tree.unmount()); + return phoneInput; +}; + +describe('Form.PhoneNumber', () => { + it('asks for the number by default: it can be typed and its country chosen', () => { + const phoneInput = phoneInputOf(); + + expect(phoneInput.disabled).toBe(false); + expect(typeof phoneInput.onPressFlag).toBe('function'); + }); + + it('only shows the number when read-only', () => { + const phoneInput = phoneInputOf({ readOnly: true }); + + expect(phoneInput.disabled).toBe(true); + expect(phoneInput.onPressFlag).toBeUndefined(); + expect(phoneInput.initialValue).toBe('+15555550100'); + }); +}); diff --git a/packages/components/src/StripeForm/StripeFormAccountBank.js b/packages/components/src/StripeForm/StripeFormAccountBank.js index cec59f1..bbe5c1a 100644 --- a/packages/components/src/StripeForm/StripeFormAccountBank.js +++ b/packages/components/src/StripeForm/StripeFormAccountBank.js @@ -3,6 +3,7 @@ import React from 'react'; import { TextInput } from '@jmstechnologiesinc/react-native-paper'; import ScreenWrapper from '../ScreenWrapper'; import { localized } from '../Localization/Localization'; +import { FieldErrorText, errorsOf } from '../Form/FormField'; const StripeFormAccountBank = ({ accountHolder, @@ -10,7 +11,12 @@ const StripeFormAccountBank = ({ reenterAccountNumber, routingNumber, inputActionHandler, + // The host's errors (`{ field, code, message }`, as the package's forms take them), each under its input. The + // form validates nothing by itself. + errors, }) => { + const hasError = (field) => errorsOf(errors, field).length > 0; + return ( <> @@ -18,42 +24,50 @@ const StripeFormAccountBank = ({ mode="outlined" label={localized('accountHolder')} value={accountHolder} + error={hasError('accountHolder')} autoCapitalize="words" onChangeText={(text) => inputActionHandler('accountHolder', text)} /> + inputActionHandler('routingNumber', text)} /> + inputActionHandler('bankAccountNumber', text)} /> + inputActionHandler('reenterAccountNumber', text)} /> + ); diff --git a/packages/components/src/StripeForm/__tests__/StripeFormAccountBank.test.js b/packages/components/src/StripeForm/__tests__/StripeFormAccountBank.test.js new file mode 100644 index 0000000..4736293 --- /dev/null +++ b/packages/components/src/StripeForm/__tests__/StripeFormAccountBank.test.js @@ -0,0 +1,67 @@ +// StripeForm.AccountBank shows the host's errors under their inputs, as the package's forms do, and validates +// nothing by itself: without `errors` it is the form it always was. +jest.mock('react-native-localize', () => ({ + findBestLanguageTag: jest.fn(() => ({ languageTag: 'en', isRTL: false })), +})); +// The section frame alone: the package's ScreenWrapper entry also loads the camera's native modules. +jest.mock('../../ScreenWrapper', () => ({ __esModule: true, default: { Section: 'ScreenWrapper.Section' } })); + +import React from 'react'; +import renderer, { act } from 'react-test-renderer'; + +import { MD3LightTheme, Provider, TextInput } from '@jmstechnologiesinc/react-native-paper'; + +import { setI18nConfig } from '../../Localization/Localization'; +import AccountBank from '../StripeFormAccountBank'; + +jest.useFakeTimers(); + +const mounted = []; +const render = (props) => { + let tree; + act(() => { + tree = renderer.create( + + {}} {...props} /> + + ); + }); + mounted.push(tree); + return tree; +}; +afterEach(() => { + act(() => { + mounted.splice(0).forEach((tree) => tree.unmount()); + jest.runOnlyPendingTimers(); + }); +}); + +beforeAll(() => setI18nConfig()); + +const inputs = (tree) => tree.root.findAllByType(TextInput); +const errorLines = (tree) => tree.root.findAll((node) => typeof node.type === 'string' && /^error\./.test(node.props.testID)); + +describe('StripeForm.AccountBank', () => { + it('marks no input and shows no error line without errors', () => { + const tree = render({}); + + expect(inputs(tree).map((input) => input.props.error)).toEqual([false, false, false, false]); + expect(errorLines(tree)).toHaveLength(0); + }); + + it('shows each of the host\'s errors under its own input', () => { + const tree = render({ + errors: [ + { field: 'routingNumber', code: 'invalid', message: 'Nine digits.' }, + { field: 'reenterAccountNumber', code: 'mismatch', message: 'They do not match.' }, + ], + }); + + expect(inputs(tree).map((input) => input.props.error)).toEqual([false, true, false, true]); + expect(errorLines(tree).map((line) => line.props.testID)).toEqual([ + 'error.routingNumber', + 'error.reenterAccountNumber', + ]); + expect(JSON.stringify(tree.toJSON())).toContain('Nine digits.'); + }); +});