2020-08-10 18:29:17 +00:00
|
|
|
/* eslint-disable jest/no-disabled-tests */
|
|
|
|
|
|
|
|
// License: LGPL-3.0-or-later
|
|
|
|
|
|
|
|
import React, { useEffect } from 'react';
|
|
|
|
|
|
|
|
import { render, fireEvent, waitFor, act } from '@testing-library/react';
|
|
|
|
import '@testing-library/jest-dom/extend-expect';
|
|
|
|
import {MoneyTextField} from './index';
|
|
|
|
import { Field, Formik, useFormikContext } from 'formik';
|
|
|
|
import { Money } from '../../common/money';
|
2020-10-24 19:46:36 +00:00
|
|
|
import { IntlProvider } from '../intl';
|
2020-08-10 18:29:17 +00:00
|
|
|
|
|
|
|
|
|
|
|
function FormikInner(props: { onChange:(args:{value:Money})=> void}) {
|
|
|
|
const context = useFormikContext<{value:Money}>();
|
|
|
|
const {value} = context.values;
|
|
|
|
const {onChange} = props;
|
|
|
|
useEffect(() => {
|
|
|
|
onChange({value});
|
|
|
|
}, [value, onChange]);
|
|
|
|
|
2021-02-26 20:23:29 +00:00
|
|
|
return <><div><span aria-label="amount">{value.cents}</span><span aria-label="currency">{value.currency}</span></div>
|
2020-08-10 18:29:17 +00:00
|
|
|
<Field component={MoneyTextField} name="value" aria-label="field"/></>;
|
|
|
|
}
|
|
|
|
|
2020-10-22 19:27:07 +00:00
|
|
|
function FormikHandler(props: { onChange:(args:{value:Money})=> void, value: Money}) {
|
2020-08-10 18:29:17 +00:00
|
|
|
|
|
|
|
const {value, ...innerFormikProps} = props;
|
2020-10-24 19:46:36 +00:00
|
|
|
return <IntlProvider locale="en">
|
2020-08-10 18:29:17 +00:00
|
|
|
<Formik initialValues={{ value }} onSubmit={() => { console.log("submitted");}} enableReinitialize={true}>
|
|
|
|
<FormikInner {...innerFormikProps} />
|
|
|
|
</Formik>
|
2020-10-24 19:46:36 +00:00
|
|
|
</IntlProvider>;
|
2020-08-10 18:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FormikHandler.defaultProps = {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-empty-function
|
2020-09-30 17:05:22 +00:00
|
|
|
onChange: () => {},
|
2020-08-10 18:29:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
describe('MoneyTextField', () => {
|
|
|
|
it('displays the $8.00 when Money of {800, usd} is passed in', async () => {
|
|
|
|
expect.hasAssertions();
|
2021-02-26 20:23:29 +00:00
|
|
|
const result = render(<FormikHandler value={Money.fromCents({ cents: 800, currency: 'usd' })} />);
|
2020-08-10 18:29:17 +00:00
|
|
|
const field = result.container.querySelector("input[name=value]");
|
|
|
|
expect(field).toHaveValue("$8.00");
|
|
|
|
const amount = await result.findByLabelText('amount');
|
|
|
|
const currency = await result.findByLabelText('currency');
|
|
|
|
|
|
|
|
expect(amount).toHaveTextContent("800");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the 8.00 € when Money of {800, eur} is passed in', async () => {
|
|
|
|
expect.hasAssertions();
|
2021-02-26 20:23:29 +00:00
|
|
|
const result = render(<FormikHandler value={Money.fromCents({ cents: 800, currency: 'eur' })} />);
|
2020-08-10 18:29:17 +00:00
|
|
|
const field = result.container.querySelector("input[name=value]");
|
|
|
|
expect(field).toHaveValue("€8.00");
|
|
|
|
const amount = await result.findByLabelText('amount');
|
|
|
|
const currency = await result.findByLabelText('currency');
|
|
|
|
|
|
|
|
expect(amount).toHaveTextContent("800");
|
|
|
|
expect(currency).toHaveTextContent("eur");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the ¥800 when Money of {800, jpy} is passed in', async () => {
|
|
|
|
expect.hasAssertions();
|
2021-02-26 20:23:29 +00:00
|
|
|
const result = render(<FormikHandler value={Money.fromCents({ cents: 800, currency: 'jpy' })} />);
|
2020-08-10 18:29:17 +00:00
|
|
|
const field = result.container.querySelector("input[name=value]");
|
|
|
|
expect(field).toHaveValue("¥800");
|
|
|
|
const amount = await result.findByLabelText('amount');
|
|
|
|
const currency = await result.findByLabelText('currency');
|
|
|
|
|
|
|
|
expect(amount).toHaveTextContent("800");
|
|
|
|
expect(currency).toHaveTextContent("jpy");
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
it('displays the $8.00 when Money of {100, usd} is passed in and then the amount changes to 8.00', async () => {
|
|
|
|
expect.hasAssertions();
|
2021-02-26 20:23:29 +00:00
|
|
|
const result = render(<FormikHandler value={Money.fromCents({ cents: 100, currency: 'usd' })} />);
|
2020-08-10 18:29:17 +00:00
|
|
|
const field = result.container.querySelector("input[name=value]");
|
|
|
|
expect(field).toHaveValue("$1.00");
|
|
|
|
|
|
|
|
const amount = await result.findByLabelText('amount');
|
|
|
|
const currency = await result.findByLabelText('currency');
|
|
|
|
|
|
|
|
expect(amount).toHaveTextContent("100");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
|
2020-10-21 17:34:07 +00:00
|
|
|
await act(async () => {
|
2020-08-10 18:29:17 +00:00
|
|
|
fireEvent.change(field, {target:{value: "$8.00"}});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(field).toHaveValue("$8.00");
|
|
|
|
expect(amount).toHaveTextContent("800");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the $80.00 when Money of {800, usd} is passed in and then the amount changes to 8.000', async () => {
|
|
|
|
expect.hasAssertions();
|
2021-02-26 20:23:29 +00:00
|
|
|
const result = render(<FormikHandler value={Money.fromCents({ cents: 800, currency: 'usd' })} />);
|
2020-08-10 18:29:17 +00:00
|
|
|
const field = result.container.querySelector("input[name=value]");
|
|
|
|
expect(field).toHaveValue("$8.00");
|
|
|
|
|
|
|
|
const amount = await result.findByLabelText('amount');
|
|
|
|
const currency = await result.findByLabelText('currency');
|
|
|
|
|
|
|
|
expect(amount).toHaveTextContent("800");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
|
2020-10-21 17:34:07 +00:00
|
|
|
await act(async () => {
|
2020-08-10 18:29:17 +00:00
|
|
|
fireEvent.change(field, {target:{value: "$8.000"}});
|
|
|
|
});
|
|
|
|
|
|
|
|
expect(field).toHaveValue("$80.00");
|
|
|
|
expect(amount).toHaveTextContent("8000");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
});
|
|
|
|
|
|
|
|
it('displays the $80.00 when Money of {800, usd} is passed in and then {8000, usd} is passed in', async () => {
|
|
|
|
expect.hasAssertions();
|
2021-02-26 20:23:29 +00:00
|
|
|
const {container, findByLabelText, rerender} = render(<FormikHandler value={Money.fromCents({ cents:800, currency: 'usd' })} />, );
|
2020-08-10 18:29:17 +00:00
|
|
|
const field = container.querySelector("input[name=value]");
|
|
|
|
expect(field).toHaveValue("$8.00");
|
|
|
|
|
|
|
|
const amount = await findByLabelText('amount');
|
|
|
|
const currency = await findByLabelText('currency');
|
|
|
|
|
|
|
|
expect(amount).toHaveTextContent("800");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
|
|
|
|
|
2021-02-26 20:23:29 +00:00
|
|
|
rerender(<FormikHandler value={Money.fromCents({ cents:8000, currency: 'usd' })} />);
|
2020-08-10 18:29:17 +00:00
|
|
|
expect(amount).toHaveTextContent("8000");
|
|
|
|
expect(currency).toHaveTextContent("usd");
|
|
|
|
|
|
|
|
waitFor(() => expect(field).toHaveValue("$80.00"));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
|