Skip to content

Commit

Permalink
fixup! Adding tests to transactions pages
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-bizz committed Oct 16, 2024
1 parent 6249be4 commit 30b7a6e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 48 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 @@ -42,7 +42,7 @@ const formatAmountForExport = (
if (amount === '0' || isExpense) {
return Number(amount);
}
return amount?.[0] === '-' ? Number(amount.substring(1)) : -Number(amount);
return -Number(amount);
};

const formatDateRange = (startDate?: DateTime, endDate?: DateTime) => {
Expand All @@ -55,10 +55,6 @@ const formatDateRange = (startDate?: DateTime, endDate?: DateTime) => {
return `${startDate.toISODate()}..${endDate.toISODate()}`;
};

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

export const AccountTransactions: React.FC = () => {
const { query } = useRouter();
const { t } = useTranslation();
Expand Down Expand Up @@ -87,6 +83,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

0 comments on commit 30b7a6e

Please sign in to comment.