Skip to content

Commit

Permalink
Edits to the export to ensure the actual amounts are shown without ro…
Browse files Browse the repository at this point in the history
…unding or the currency sign
  • Loading branch information
dr-bizz committed Oct 16, 2024
1 parent 4a4c745 commit a8d4ac0
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ const defaultRouter = {
query: { accountListId },
isReady: true,
};
const entriesRouter = {
query: {
accountListId,
financialAccount: [financialAccountId, 'entries'],
},
isReady: true,
};

const Components = ({ router = defaultRouter }: { router?: object }) => (
<I18nextProvider i18n={i18n}>
Expand Down Expand Up @@ -85,17 +92,7 @@ describe('Financial Accounts Page', () => {

it('should show the transactions page for a financial account', async () => {
const { findByText, findByRole, getByText, queryByText, queryByRole } =
render(
<Components
router={{
query: {
accountListId,
financialAccount: [financialAccountId, 'entries'],
},
isReady: true,
}}
/>,
);
render(<Components router={entriesRouter} />);

expect(await findByText('Account 1')).toBeInTheDocument();

Expand All @@ -112,17 +109,7 @@ describe('Financial Accounts Page', () => {
});

it('should open filters on load and set initial date Range filter', async () => {
const { findByRole } = render(
<Components
router={{
query: {
accountListId,
financialAccount: [financialAccountId, 'entries'],
},
isReady: true,
}}
/>,
);
const { findByRole } = render(<Components router={entriesRouter} />);

expect(
await findByRole('heading', { name: 'Filter (1)' }),
Expand All @@ -131,15 +118,7 @@ describe('Financial Accounts Page', () => {

it('should open and close filters and menu', async () => {
const { findByRole, getByRole, queryByRole } = render(
<Components
router={{
query: {
accountListId,
financialAccount: [financialAccountId, 'entries'],
},
isReady: true,
}}
/>,
<Components router={entriesRouter} />,
);

// Filters
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ThemeProvider } from '@mui/material/styles';
import { LocalizationProvider } from '@mui/x-date-pickers';
import { AdapterLuxon } from '@mui/x-date-pickers/AdapterLuxon';
import { fireEvent, render, waitFor } from '@testing-library/react';
import { Settings } from 'luxon';
import { SnackbarProvider } from 'notistack';
import { I18nextProvider } from 'react-i18next';
import TestRouter from '__tests__/util/TestRouter';
Expand All @@ -20,11 +21,7 @@ import {
FinancialAccountContext,
FinancialAccountType,
} from '../Context/FinancialAccountsContext';
import {
AccountTransactions,
defaultEndDate,
defaultStartDate,
} from './AccountTransactions';
import { AccountTransactions } from './AccountTransactions';
import { financialAccountEntriesMock } from './AccountTransactionsMocks';
import { FinancialAccountEntriesQuery } from './financialAccountTransactions.generated';

Expand Down Expand Up @@ -96,6 +93,9 @@ const Components = ({
);

describe('Financial Account Transactions', () => {
beforeEach(() => {
Settings.now = () => new Date(2024, 7, 31).valueOf();
});
describe('Resetting filters', () => {
it('should reset the activeFilters with the filters from the url filters', () => {
render(
Expand All @@ -113,8 +113,8 @@ describe('Financial Account Transactions', () => {

expect(setActiveFilters).not.toHaveBeenCalledWith({
dateRange: {
min: defaultStartDate,
max: defaultEndDate,
min: '2024-08-01',
max: '2024-08-31',
},
});

Expand All @@ -129,8 +129,8 @@ describe('Financial Account Transactions', () => {

expect(setActiveFilters).toHaveBeenCalledWith({
dateRange: {
min: defaultStartDate,
max: defaultEndDate,
min: '2024-08-01',
max: '2024-08-31',
},
});
});
Expand All @@ -143,6 +143,7 @@ describe('Financial Account Transactions', () => {
expect(getAllByText('category1Code')).toHaveLength(2);

// Header
expect(getByText('8/31/2024')).toBeInTheDocument();
expect(getByText('Closing Balance')).toBeInTheDocument();
expect(getByText('UAH 280,414')).toBeInTheDocument();

Expand All @@ -165,6 +166,7 @@ describe('Financial Account Transactions', () => {
expect(getByText('UAH 37')).toBeInTheDocument();

// Footer
expect(getByText('8/1/2024')).toBeInTheDocument();
expect(getByText('Opening Balance')).toBeInTheDocument();
expect(getByText('UAH 202,240')).toBeInTheDocument();

Expand Down Expand Up @@ -312,9 +314,9 @@ describe('Financial Account Transactions', () => {

const csvContentArray = [
['Date', 'Payee', 'Memo', 'Outflow', 'Inflow'],
['8/9/2024', 'description1', 'category1Name', '', 'UAH 7,048'],
['8/8/2024', 'description2', 'category1Name', 'UAH 15,008', ''],
['8/7/2024', 'description3', 'category2Name', '', 'UAH 37'],
['8/9/2024', 'description1', 'category1Name', '', '7047.28'],
['8/8/2024', 'description2', 'category1Name', '15008', ''],
['8/7/2024', 'description3', 'category2Name', '', '36.2'],
];

const csvContent =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import { headerHeight } from 'src/components/Shared/Header/ListHeader';
import { useDebouncedValue } from 'src/hooks/useDebounce';
import useGetAppSettings from 'src/hooks/useGetAppSettings';
import { useLocale } from 'src/hooks/useLocale';
import { currencyFormat, dateFormatShort } from 'src/lib/intlFormat';
import { formatNumber } from '../AccountSummary/AccountSummaryHelper';
import { dateFormatShort } from 'src/lib/intlFormat';
import {
FinancialAccountContext,
FinancialAccountType,
Expand All @@ -27,19 +26,31 @@ const Container = styled(Box)(() => ({
overflowY: 'auto',
}));

const formatDateRange = (startDate?: DateTime, endDate?: DateTime) => {
if (!startDate) {
startDate = DateTime.local().minus({ months: 1 }).plus({ days: 1 });
/**
* Converts the "amount" string to a number to remove ".0"
* If the value is 0 or isExpense is true, it returns the value as is.
* Otherwise, it removes the '-' character if present, or prepends it if absent.
*/
const formatAmountForExport = (
amount?: string | null,
isExpense?: boolean,
): number => {
if (!amount) {
return 0;

Check warning on line 39 in src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.tsx

View check run for this annotation

Codecov / codecov/patch

src/components/Reports/FinancialAccountsReport/AccountTransactions/AccountTransactions.tsx#L39

Added line #L39 was not covered by tests
}
if (!endDate) {
endDate = DateTime.local();

if (amount === '0' || isExpense) {
return Number(amount);
}
return `${startDate.toISODate()}..${endDate.toISODate()}`;
return -Number(amount);
};

const defaultDateRange = formatDateRange();
export const defaultStartDate = defaultDateRange.split('..')[0];
export const defaultEndDate = defaultDateRange.split('..')[1];
const formatDateRange = (startDate?: DateTime, endDate?: DateTime) => {
const minDate =
startDate ?? DateTime.local().minus({ months: 1 }).plus({ days: 1 });
const maxDate = endDate ?? DateTime.local();
return `${minDate.toISODate()}..${maxDate.toISODate()}`;
};

export const AccountTransactions: React.FC = () => {
const { query } = useRouter();
Expand Down Expand Up @@ -69,6 +80,10 @@ export const AccountTransactions: React.FC = () => {
};
}, [query?.filters]);

const defaultDateRange = useMemo(() => formatDateRange(), []);
const defaultStartDate = defaultDateRange.split('..')[0];
const defaultEndDate = defaultDateRange.split('..')[1];

useEffect(() => {
if (!hasActiveFilters && !query?.filters) {
setActiveFilters({
Expand Down Expand Up @@ -129,18 +144,10 @@ export const AccountTransactions: React.FC = () => {
entry.description ?? '',
entry.category?.name ?? entry.category?.code ?? '',
entry.type === FinancialAccountEntryTypeEnum.Debit
? currencyFormat(
formatNumber(entry.amount),
entry.currency,
locale,
).replace(/[\xA0\u2000-\u200B\uFEFF]/g, ' ')
? `${formatAmountForExport(entry.amount, true)}`
: '',
entry.type === FinancialAccountEntryTypeEnum.Credit
? currencyFormat(
formatNumber(entry.amount),
entry.currency,
locale,
).replace(/[\xA0\u2000-\u200B\uFEFF]/g, ' ')
? `${formatAmountForExport(entry.amount)}`
: '',
],
];
Expand Down

0 comments on commit a8d4ac0

Please sign in to comment.