write test cases for below code import React from 'react'; import { ChargesTable,…
- User
write test cases for below code import React from 'react'; import { ChargesTable, paymentTableSelectors, PaymentChargesTableAction } from '@txp-core/payment-transactions-table'; import { Table } from '@ids-ts/table'; import { useTXPStateRead, useTXPStateWrite } from '@txp-core/runtime'; import { useFormatMessage } from '@txp-core/intl'; import { TransactionSelectors } from '@txp-core/transactions-core'; import { enabled } from '@txp-core/enabled'; import { TransactionTypes, getTxnUrl } from '@txp-core/transactions-utils'; import { toLocalizedMoneyString, toLocalDateFormat } from '@txp-core/basic-utils'; import TransactionsTableHeader from './TransactionTableHeaders'; import styles from 'src/js/staticLayout/Grid.module.css'; import Checkbox, { CheckboxOnChangeEventType } from '@ids-ts/checkbox'; import style from './Tables.module.css'; import { useTrowserHooks } from '@txp-core/trowser'; import LinkButton from '@txp-core/link-button'; import PaymentField from 'src/js/billpayment/modules/TransactionTables/PaymentField';
export interface txnType { txnTypeId: number; txnTypeLongName: string; }
export interface OutstandingTableDataTypes { txnId: number; txnType: txnType; date: string; amount: string; referenceNumber: number; dueDate: string; openBalance: string; linkedPaymentAmount: string; isLinkedPaymentAmount?: boolean; }
export const OutstandingTransactionsTable = () => { const formatMessage = useFormatMessage(); const customerId = useTXPStateRead(TransactionSelectors.getContactId); const outstandingTableLines = useTXPStateRead(paymentTableSelectors.getCharges) || []; const onPaymentFieldUpdated = useTXPStateWrite( PaymentChargesTableAction.chargesTableDataUpdate ); const { closeTrowser } = useTrowserHooks();
const Items = (props: { item: OutstandingTableDataTypes; idx: number }) => { const { item, idx } = props; const txnUrl = getTxnUrl(`${item.txnType.txnTypeId}`, item.txnId); const referenceNumber = item?.referenceNumber ? `# ${item?.referenceNumber}` : ''; const handleCheckboxChange = (event: CheckboxOnChangeEventType, txnId: number) => { if (event.target.checked) { const value = outstandingTableLines.filter((x) => x.txnId === txnId)[0].openBalance; onPaymentFieldUpdated(txnId, { linkedPaymentAmount: value.toString(), isLinkedPaymentAmount: true }); } else { onPaymentFieldUpdated(txnId, { linkedPaymentAmount: '' }); } }; const handlePaymentFieldChange = (inputValue: string) => { onPaymentFieldUpdated(item.txnId, { linkedPaymentAmount: inputValue, isLinkedPaymentAmount: true }); }; const handleNavigate = () => closeTrowser?.({ navigateTo: txnUrl, skipTrowserCloseDelay: true }); return ( <> <Table.Row key={idx} className='tableRow'> <Table.Cell className={style.checkbox}> <Checkbox aria-label={formatMessage({ id: 'charges_credits_table_select_rows', defaultMessage: 'Select rows' })} style={{ cursor: 'pointer' }} checked={!!item.linkedPaymentAmount} onChange={(event) => handleCheckboxChange(event, item.txnId)} title='Toggle Row Selected' /> </Table.Cell> <Table.Cell className={styles.leftAlign}> <LinkButton onClick={handleNavigate} text={`${item.txnType.txnTypeLongName} ${referenceNumber}`} />{' '} ({toLocalDateFormat(item.date)}) </Table.Cell> <Table.Cell className={styles.leftAlign}> {toLocalDateFormat(item.dueDate)} </Table.Cell> <Table.Cell className={styles.rightAlign}> {toLocalizedMoneyString(item.amount)} </Table.Cell> <Table.Cell className={styles.rightAlign}> {toLocalizedMoneyString(item.openBalance)} </Table.Cell> <Table.Cell> <PaymentField paymentAmount={item.linkedPaymentAmount} txnId={item.txnId} rowId={idx.toString()} handleOnPaymentChange={handlePaymentFieldChange} /> </Table.Cell> </Table.Row> </> ); }; return ( <div className={styles.gridWrapper}> {customerId && ( <ChargesTable header={<TransactionsTableHeader dueDateFlag />}> {(item: OutstandingTableDataTypes, idx: number) => ( <Items item={item} idx={idx} /> )} </ChargesTable> )} </div> );};
export default enabled( { 'environment.txnType': [TransactionTypes.PURCHASE_BILL_PAYMENT] }, OutstandingTransactionsTable );
- Assistant
Here are some test cases for the given code:
-
Test case: Ensure the "OutstandingTransactionsTable" component renders correctly. Input: Render the "OutstandingTransactionsTable" component. Expected output: The component should render correctly with styles using the classNames.
-
Test case: Test the "Items" component renders correctly and with proper data. Input: Pass an "item" and "idx" to the "Items" component inside the "OutstandingTransactionsTable". Expected output: The "Items" component should render correctly with the given data and index.
-
Test case: Test the checkbox functionality in the "Items" component. Input: Click the checkbox in the "Items" component with a specific txnId. Expected output: The linkedPaymentAmount value for the corresponding transaction should change accordingly in the state.
-
Test case: Test the handlePaymentFieldChange function in the "Items" component. Input: Change the input value in the "PaymentField" component inside the "Items" component. Expected output: The linkedPaymentAmount and isLinkedPaymentAmount values for the corresponding transaction should be updated in the state.
-
Test case: Test the handleNavigate function in the "Items" component. Input: Click the "LinkButton" element within the "Items" component. Expected output: The trowser hook "closeTrowser" should be called with appropriate navigation options.
-
Test case: Ensure the "customerID" condition in the "OutstandingTransactionsTable" component is checked. Input: Render the "OutstandingTransactionsTable" component with a given "customerID" in the state. Expected output: The "ChargesTable" component should be rendered inside the "OutstandingTransactionsTable" component.
-
Test case: Test the enabled function wraps the component based on environment variables. Input: Verify the enabled function wrapping the "OutstandingTransactionsTable" component. Expected output: The component should be wrapped by the enabled function and render based on the environment variables.
-