Config.ts import { Module, registerModule } from '@txp-core/runtime'; import {…
- User
Config.ts import { Module, registerModule } from '@txp-core/runtime'; import { totalAmountSideEffectPattern } from './totalAmountSideEffectPatterns'; import { totalAmountHandler } from './totalAmountSaga'; export const totalAmountModule: Module = registerModule({ id: 'totalAmount', sideEffects: [ { id: 'totalAmount', description: 'update total amount when line item gets updated/added', pattern: totalAmountSideEffectPattern, handler: totalAmountHandler } ], withModuleDependencies: ['coreTransactionState', 'coreUIState'] });
Totlamount.tsx import React from 'react'; import { useFormatMessage } from '@txp-core/intl'; import { useTXPStateRead } from '@txp-core/runtime'; import { getMoney } from '@txp-core/basic-utils'; import { Demi } from '@ids-ts/typography'; import styles from 'src/js/modules/TotalAmount/TotalAmount.module.css'; import { TotalsSelectors } from '@txp-core/transactions-calculations'; const TotalAmount = () => { const total = useTXPStateRead(TotalsSelectors.getSubTotal); const formatMessage = useFormatMessage(); const localizedTotalAmount = getMoney(total).toCurrencyString(); const totalLabel = formatMessage({ id: 'total', defaultMessage: 'Total' }); return ( <> {totalLabel} {localizedTotalAmount} </> ); };export default TotalAmount;
Total amountsaga.ts import { ResultType, StepResponse } from '@txp-core/runtime'; import { Effect, select, put } from 'redux-saga/effects'; import { TotalsSelectors } from '@txp-core/transactions-calculations'; import { genericTxnUpdate } from '@txp-core/transactions-core'; export function* totalAmountHandler(): Generator<Effect, StepResponse, string> { const totalAmount = yield select(TotalsSelectors.getSubTotal); yield put(genericTxnUpdate({ amount: totalAmount }, true)); return { result: ResultType.SUCCESS }; }
Totalamountsideeffectpatterns.ts import { genericLineUpdated, itemLineAdded, ITEM_LINE_ADDED, lineTouched, LINE_TOUCHED, LINE_UPDATED, AllActions, ACCOUNT_LINES_ADDED } from '@txp-core/transactions-core'; type TotalAmountSideEffectTypes = | ReturnType | ReturnType | ReturnType; export const totalAmountSideEffectPattern = (action: AllActions | TotalAmountSideEffectTypes) => { switch (action.type) { case ITEM_LINE_ADDED: case ACCOUNT_LINES_ADDED: case LINE_UPDATED: case LINE_TOUCHED: return true; default: return false; } };
The above codes are totalamouunt component implementation done by side effect and more..
Test cases for above codes import * as React from 'react'; import { fireEvent, screen, waitFor } from '@testing-library/react'; import { TransactionTypes } from '@txp-core/transactions-utils'; import { runtimeTestUtils } from '@txp-core/runtime'; import { testUtils as TransactionsCoreTestUtils } from '@txp-core/transactions-core'; import CoreLineAmountField from '@txp-core/line-amount-field'; import TotalAmount from 'src/js/modules/TotalAmount/TotalAmount'; import TransactionHandler from '@txp-core/transaction-handler'; import { totalAmountModule } from '../config'; import { TotalsSelectors } from '@txp-core/transactions-calculations'; describe('test TotalAmount', () => { beforeEach(() => { jest.spyOn(TotalsSelectors, 'getSubTotal').mockImplementation(() => '22.00'); }); test('Total add up to 0.00 when no lines total', () => { jest.spyOn(TotalsSelectors, 'getSubTotal').mockImplementation(() => '0.00'); runtimeTestUtils.renderTXPComponent(, { variability: { environment: { txnType: TransactionTypes.PURCHASE_BILL } } }); expect(screen.getByTestId('total-amount-text').textContent).toEqual('0.00'); }); test('Total add up to correct amount when lines exist', () => { runtimeTestUtils.renderTXPComponent(, { variability: { environment: { txnType: TransactionTypes.PURCHASE_BILL } } }); expect(screen.getByTestId('total-amount-text').textContent).toEqual('22.00'); }); describe('integration test to check genTxnUpdate on sideEffects', () => { test('should dispatch generic txn action when line items added/updated', async () => { const variability = { environment: { txnType: TransactionTypes.PURCHASE_BILL } }; const v4Txn = { lines: { accountLines: [ { description: 'line 1', id: 'EXISTING_LINE_1' }, { description: 'line 2', id: 'EXISTING_LINE_2' } ] } }; const txpInstance = TransactionsCoreTestUtils.createMinimumTransactionTXPInstance( [totalAmountModule], variability ); const { mockedTXPHooks } = await runtimeTestUtils.renderTXPLayout( , txpInstance ); const lineAmount = screen.getByTestId('amount line 1'); // because of MoneyTextField's implementation using local state, we have to fire change + blur // in order for the text field value to update fireEvent.change(lineAmount, { target: { value: '$100.00' } }); fireEvent.blur(lineAmount); await waitFor(() => { expect(mockedTXPHooks.writeSpy).toBeCalled(); }); }); }); });
- Assistant