From b98009652a9fe7d6e0124103331896422ba936de Mon Sep 17 00:00:00 2001 From: Adolfo Builes Date: Mon, 23 Jul 2018 11:21:50 -0500 Subject: [PATCH] Add new payment form component. --- app/components/PaymentForm.tsx | 88 +++++ app/components/__tests__/PaymentForm.test.tsx | 18 + .../__snapshots__/PaymentForm.test.tsx.snap | 313 ++++++++++++++++++ storybook/stories/PaymentForm/index.tsx | 30 ++ storybook/stories/index.js | 4 + 5 files changed, 453 insertions(+) create mode 100644 app/components/PaymentForm.tsx create mode 100644 app/components/__tests__/PaymentForm.test.tsx create mode 100644 app/components/__tests__/__snapshots__/PaymentForm.test.tsx.snap create mode 100644 storybook/stories/PaymentForm/index.tsx diff --git a/app/components/PaymentForm.tsx b/app/components/PaymentForm.tsx new file mode 100644 index 0000000..2918856 --- /dev/null +++ b/app/components/PaymentForm.tsx @@ -0,0 +1,88 @@ +import * as React from 'react'; +import { Alert } from 'react-native' +import { Container, Content, Form, Item, Input, Label, Button, Text, Spinner } from 'native-base'; +import { styles as s } from "react-native-style-tachyons"; + +import { Transaction } from '../Types' + +interface Props { + send: (username: string, amount: string) => Promise + didSend: () => void +} + +interface State { + amount: string + username: string + sending: boolean +} + +class PaymentForm extends React.Component { + constructor(props: Props) { + super(props) + + this.state = { + amount: '', + username: '', + sending: false + } + } + + async send(): Promise { + const { username, amount } = this.state + + this.setState({ + sending: true + }) + + const transaction = await this.props.send(username, amount) + + this.setState({ + sending: false + }) + + this.props.didSend() + + return transaction + } + + render() { + const { amount, username, sending } = this.state + + return ( + + +
+ + + this.setState({ username })} + value={username} + autoFocus={true} + autoCorrect={false} + autoCapitalize={"none"} + /> + + + + this.setState({ amount })} + value={amount} + keyboardType={'decimal-pad'} + autoCorrect={false} + autoCapitalize={"none"} + /> + + {!sending && ( + + )} + {sending && } + +
+
+ ); + } +} + +export default PaymentForm diff --git a/app/components/__tests__/PaymentForm.test.tsx b/app/components/__tests__/PaymentForm.test.tsx new file mode 100644 index 0000000..4903181 --- /dev/null +++ b/app/components/__tests__/PaymentForm.test.tsx @@ -0,0 +1,18 @@ +import * as React from 'react' +import PaymentForm from '../PaymentForm' + +import * as renderer from 'react-test-renderer' + +const send = (amount: string) => { + return new Promise.resolve({ + id: '1234' + }) +} + +jest.useFakeTimers() + +it('renders correctly', () => { + const rendered = renderer.create().toJSON() + + expect(rendered).toMatchSnapshot() +}); diff --git a/app/components/__tests__/__snapshots__/PaymentForm.test.tsx.snap b/app/components/__tests__/__snapshots__/PaymentForm.test.tsx.snap new file mode 100644 index 0000000..577806b --- /dev/null +++ b/app/components/__tests__/__snapshots__/PaymentForm.test.tsx.snap @@ -0,0 +1,313 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`renders correctly 1`] = ` + + + + + + + + + Recipient username + + + + + + + + + + Amount + + + + + + + + Send + + + + + + +`; diff --git a/storybook/stories/PaymentForm/index.tsx b/storybook/stories/PaymentForm/index.tsx new file mode 100644 index 0000000..78e9910 --- /dev/null +++ b/storybook/stories/PaymentForm/index.tsx @@ -0,0 +1,30 @@ +import * as React from 'react'; +import { Alert } from 'react-native'; +import PaymentForm from '../../../app/components/PaymentForm'; + +class PaymentFormStory extends React.Component { + render() { + const transaction = { + id: '1234' + } + + const send = (username: string, amount: string) => { + return new Promise((resolve) => { + Alert.alert( + `Sending ${amount} to ${username}!`, + 'This should call the mutation payment', + [ + {text: 'OK', onPress: () => resolve(transaction)}, + ], + { cancelable: false } + ) + }) + } + + return ( + {}} /> + ) + } +} + +export default PaymentFormStory diff --git a/storybook/stories/index.js b/storybook/stories/index.js index 765d69a..cfca2a4 100644 --- a/storybook/stories/index.js +++ b/storybook/stories/index.js @@ -11,6 +11,7 @@ import Login from './Login' import Balance from '../../app/containers/Balance' import Payment from './Payment' import Transfer from './Transfer' +import PaymentForm from './PaymentForm' storiesOf('Welcome', module).add('to Storybook', () => ) @@ -47,3 +48,6 @@ storiesOf('Payment', module) storiesOf('TransferForm', module). add('default', () => ) + +storiesOf('PaymentForm', module). + add('default', () => )