Skip to content

Commit

Permalink
chore(test) add test for basic form calculation component
Browse files Browse the repository at this point in the history
  • Loading branch information
ansmonjol committed Sep 14, 2023
1 parent 0394d45 commit fe8f59b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 5 deletions.
8 changes: 4 additions & 4 deletions src/components/creditNote/CreditNoteFormCalculation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export const CreditNoteFormCalculation = ({
<Icon name="info-circle" />
</Tooltip>
</InlineLabel>
<Typography color="grey700">
<Typography color="grey700" data-test="prorated-coupon-amount">
-
{intlFormatNumber(proRatedCouponAmount || 0, {
currency,
Expand All @@ -129,7 +129,7 @@ export const CreditNoteFormCalculation = ({
)}
<Line>
<Typography variant="bodyHl">{translate('text_636bedf292786b19d3398f02')}</Typography>
<Typography color="grey700">
<Typography color="grey700" data-test="total-excluded-tax">
{!totalExcludedTax
? '-'
: intlFormatNumber(totalExcludedTax, {
Expand All @@ -148,7 +148,7 @@ export const CreditNoteFormCalculation = ({
.map((tax) => (
<Line key={tax.label}>
<Typography variant="bodyHl">{tax.label}</Typography>
<Typography color="grey700">
<Typography color="grey700" data-test={`tax-${tax.taxRate}-amount`}>
{intlFormatNumber(tax.amount, {
currency,
})}
Expand All @@ -171,7 +171,7 @@ export const CreditNoteFormCalculation = ({
<Typography variant="bodyHl" color="grey700">
{translate('text_636bedf292786b19d3398f0a')}
</Typography>
<Typography color="grey700">
<Typography color="grey700" data-test="total-tax-included">
{!totalTaxIncluded
? '-'
: intlFormatNumber(totalTaxIncluded, {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { act, cleanup, screen } from '@testing-library/react'
import { useFormik } from 'formik'
import { object } from 'yup'

import { chargeSchema } from '~/formValidation/chargeSchema'
import { render } from '~/test-utils'

import { addOnFeeMock, feesMock, invoiceMock } from './fixtures'

import { CreditNoteFormCalculation } from '../CreditNoteFormCalculation'
import { CreditNoteForm } from '../types'

async function prepare() {
const CreditNoteFormCalculationMock = () => {
const formikProps = useFormik<Partial<CreditNoteForm>>({
initialValues: {
description: undefined,
reason: undefined,
fees: feesMock,
addOnFee: addOnFeeMock,
payBack: [{ type: undefined, value: undefined }],
creditAmount: undefined,
refundAmount: undefined,
},
validationSchema: object().shape({
charges: chargeSchema,
}),
enableReinitialize: true,
validateOnMount: true,
onSubmit: () => {},
})

return <CreditNoteFormCalculation invoice={invoiceMock} formikProps={formikProps} />
}

await act(() => {
render(<CreditNoteFormCalculationMock />)
})
}
describe('CreditNoteFormCalculation', () => {
afterEach(cleanup)

it('renders with correct values', async () => {
await prepare()

expect(screen.queryByTestId('prorated-coupon-amount')).toHaveTextContent('-€482.23')
expect(screen.queryByTestId('total-excluded-tax')).toHaveTextContent('€30,017.77')
expect(screen.queryByTestId('tax-10-amount')).toHaveTextContent('€1,033.12')
expect(screen.queryByTestId('tax-20-amount')).toHaveTextContent('€3,837.31')
expect(screen.queryByTestId('total-tax-included')).toHaveTextContent('€34,888.20')
})
})
15 changes: 15 additions & 0 deletions src/components/creditNote/__tests__/fixtures.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { CurrencyEnum, InvoicePaymentStatusTypeEnum } from '~/generated/graphql'

export const feesMock = {
subscriptionId1: {
subscriptionName: 'Subscription 1',
Expand Down Expand Up @@ -149,3 +151,16 @@ export const addOnFeeMock = [
],
},
]

export const invoiceMock = {
id: '1234',
couponsAmountCents: '1010',
paymentStatus: InvoicePaymentStatusTypeEnum.Pending,
creditableAmountCents: '0',
refundableAmountCents: '0',
feesAmountCents: '62833',
currency: CurrencyEnum.Eur,
versionNumber: 3,
fees: addOnFeeMock,
invoiceSubscriptions: feesMock,
}
2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,7 @@
"@babel/helper-validator-identifier" "^7.16.7"
to-fast-properties "^2.0.0"

"@babel/types@^7.0.0", "@babel/types@^7.16.8", "@babel/types@^7.17.0", "@babel/types@^7.18.10", "@babel/types@^7.18.13", "@babel/types@^7.18.6", "@babel/types@^7.18.9", "@babel/types@^7.20.0", "@babel/types@^7.21.3", "@babel/types@^7.21.4", "@babel/types@^7.22.0", "@babel/types@^7.22.10", "@babel/types@^7.22.11", "@babel/types@^7.22.15", "@babel/types@^7.22.3", "@babel/types@^7.22.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3", "@babel/types@^7.4.4":
"@babel/types@^7.0.0", "@babel/types@^7.16.8", "@babel/types@^7.17.0", "@babel/types@^7.18.10", "@babel/types@^7.18.13", "@babel/types@^7.18.6", "@babel/types@^7.18.9", "@babel/types@^7.20.0", "@babel/types@^7.21.3", "@babel/types@^7.21.4", "@babel/types@^7.22.0", "@babel/types@^7.22.11", "@babel/types@^7.22.15", "@babel/types@^7.22.3", "@babel/types@^7.22.5", "@babel/types@^7.3.0", "@babel/types@^7.3.3", "@babel/types@^7.4.4":
version "7.22.17"
resolved "https://registry.yarnpkg.com/@babel/types/-/types-7.22.17.tgz#f753352c4610ffddf9c8bc6823f9ff03e2303eee"
integrity sha512-YSQPHLFtQNE5xN9tHuZnzu8vPr61wVTBZdfv1meex1NBosa4iT05k/Jw06ddJugi4bk7The/oSwQGFcksmEJQg==
Expand Down

0 comments on commit fe8f59b

Please sign in to comment.