Skip to content

Commit

Permalink
payment history page back button to previous page
Browse files Browse the repository at this point in the history
  • Loading branch information
janechodance committed Nov 27, 2024
1 parent e3aa54d commit 92a9abd
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import React, { useEffect, useState, useRef } from 'react';
import React, { useEffect, useState } from 'react';

Check warning on line 1 in src/applications/disability-benefits/view-payments/components/view-payments-lists/payments/Payments.jsx

View workflow job for this annotation

GitHub Actions / App Isolation Annotations

Staged Continuous Deployment App Isolation Conflict

*WARNING* This PR contains changes related to an application that is currently not isolated. As of Feb 3, 2025 deployment may no longer be possible for apps that are not isolated. Please isolate this app from other directories in 'src/applications' to prevent future deployment issues. More information on your app's status can be seen here: https://department-of-veterans-affairs.github.io/veteran-facing-services-tools/frontend-support-dashboard/cross-app-import-report Please reach out to Frontend Platform Support with any questions.
import { chunk } from 'lodash';
import PropTypes from 'prop-types';
import { VaPagination } from '@department-of-veterans-affairs/component-library/dist/react-bindings';
import { useLocation, useNavigate } from 'react-router-dom-v5-compat';

import { clientServerErrorContent } from '../helpers';

Expand All @@ -26,22 +27,24 @@ const Payments = ({
textContent,
alertMessage,
}) => {
const location = useLocation();
const navigate = useNavigate();
const [currentData, setCurrentData] = useState([]);
const [currentPage, setCurrentPage] = useState(1);
// Using `useRef` here to avoid triggering a rerender whenever these are
// updated
const totalPages = useRef(0);
const paginatedData = useRef([]);
const currentPage = new URLSearchParams(location.search).get('page') || 1;
const [totalPages, setTotalPages] = useState(0);

useEffect(() => {
paginatedData.current = paginateData(data);
setCurrentData(paginatedData.current[currentPage - 1]);
totalPages.current = paginatedData.current.length;
}, []);
useEffect(
() => {
const paginatedData = paginateData(data);
setCurrentData(paginatedData[currentPage - 1]);
setTotalPages(paginatedData.length);
},
[currentPage, data],
);

const onPageChange = page => {
setCurrentData(paginatedData.current[page - 1]);
setCurrentPage(page);
const newURL = `${location.pathname}?page=${page}`;
navigate(newURL);
};

const [from, to] = getFromToNums(currentPage, data.length);
Expand Down Expand Up @@ -75,7 +78,7 @@ const Payments = ({
<VaPagination
onPageSelect={e => onPageChange(e.detail.page)}
page={currentPage}
pages={totalPages.current}
pages={totalPages}
maxPageListLength={MAX_PAGE_LIST_LENGTH}
showLastPage
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react';

Check warning on line 1 in src/applications/disability-benefits/view-payments/tests/components/view-payments-lists/Payments/Payments.unit.spec.jsx

View workflow job for this annotation

GitHub Actions / App Isolation Annotations

Staged Continuous Deployment App Isolation Conflict

*WARNING* This PR contains changes related to an application that is currently not isolated. As of Feb 3, 2025 deployment may no longer be possible for apps that are not isolated. Please isolate this app from other directories in 'src/applications' to prevent future deployment issues. More information on your app's status can be seen here: https://department-of-veterans-affairs.github.io/veteran-facing-services-tools/frontend-support-dashboard/cross-app-import-report Please reach out to Frontend Platform Support with any questions.
import { render } from '@testing-library/react';
import { expect } from 'chai';
import { MemoryRouter } from 'react-router-dom-v5-compat';
import { payments } from '../../../helpers';
import {
paymentsReceivedFields,
Expand All @@ -17,6 +18,7 @@ describe('<Payments />', () => {
tableVersion="received"
textContent={paymentsReceivedContent}
/>,
{ wrapper: MemoryRouter },
);

expect(await screen.findByText(/Payments you received/)).to.exist;
Expand All @@ -34,8 +36,55 @@ describe('<Payments />', () => {
tableVersion="received"
textContent={paymentsReceivedContent}
/>,
{ wrapper: MemoryRouter },
);

expect(await screen.findByText(/No received payments/)).to.exist;
});

const getPageFromURL = location => {
return new URLSearchParams(location.search).get('page') || 1;
};
it('should correctly parse the page number from the URL', () => {
const location = {
pathname: '/',
search: '?page=3',
};

render(
<MemoryRouter initialEntries={[location]}>
<Payments
fields={paymentsReceivedFields}
data={payments.payments}
tableVersion="received"
textContent={paymentsReceivedContent}
location={location} // Pass location to Payments component
/>
</MemoryRouter>,
);

const page = getPageFromURL(location);
expect(page).to.equal('3');
});

it('should default to page 1 when no page param is provided', () => {
const location = {
pathname: '/',
search: '',
};

render(
<MemoryRouter initialEntries={[location]}>
<Payments
fields={paymentsReceivedFields}
data={payments.payments}
tableVersion="received"
textContent={paymentsReceivedContent}
location={location} // Pass location to Payments component
/>
</MemoryRouter>,
);
const page = getPageFromURL(location);
expect(page).to.equal(1);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { setupServer } from 'msw/node';
import { renderInReduxProvider } from 'platform/testing/unit/react-testing-library-helpers';
import environment from 'platform/utilities/environment';

import { MemoryRouter } from 'react-router-dom-v5-compat';
import allPayments from '../../../reducers/index';
import ViewPaymentsLists from '../../../components/view-payments-lists/ViewPaymentsLists';
import {
Expand Down Expand Up @@ -70,10 +71,15 @@ describe('View Payments Lists', () => {
},
};

const screen = renderInReduxProvider(<ViewPaymentsLists />, {
initialState,
reducers: allPayments,
});
const screen = renderInReduxProvider(
<MemoryRouter>
<ViewPaymentsLists />
</MemoryRouter>,
{
initialState,
reducers: allPayments,
},
);
expect(await screen.findByText(/Payments you received/)).to.exist;
expect(screen.getByText(/Payments returned/)).to.exist;
});
Expand All @@ -93,10 +99,15 @@ describe('View Payments Lists', () => {
},
};

const screen = renderInReduxProvider(<ViewPaymentsLists />, {
initialState,
reducers: allPayments,
});
const screen = renderInReduxProvider(
<MemoryRouter>
<ViewPaymentsLists />
</MemoryRouter>,
{
initialState,
reducers: allPayments,
},
);

expect(await screen.findByText(/Payments you received/)).to.exist;
expect(
Expand All @@ -119,10 +130,15 @@ describe('View Payments Lists', () => {
},
};

const screen = renderInReduxProvider(<ViewPaymentsLists />, {
initialState,
reducers: allPayments,
});
const screen = renderInReduxProvider(
<MemoryRouter>
<ViewPaymentsLists />
</MemoryRouter>,
{
initialState,
reducers: allPayments,
},
);

expect(
await screen.findByText(
Expand Down

0 comments on commit 92a9abd

Please sign in to comment.