Skip to content
This repository has been archived by the owner on Sep 18, 2024. It is now read-only.

Commit

Permalink
update date util and add test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
rylew1 committed May 29, 2024
1 parent 3eb5fb5 commit 706dd8d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
7 changes: 6 additions & 1 deletion frontend/src/utils/dateUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ export function formatDate(dateStr: string | null) {
if (dateStr === "" || dateStr === null) {
return "";
}
const date = new Date(dateStr);

const [year, month, day] = dateStr.split("-").map(Number);

// Create a new Date object using the local time
const date = new Date(year, month - 1, day);

const options: Intl.DateTimeFormatOptions = {
year: "numeric",
month: "long",
Expand Down
30 changes: 30 additions & 0 deletions frontend/tests/components/search/SearchResultsListItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,34 @@ describe("SearchResultsListItem", () => {
expect(screen.getByText("Test Opportunity")).toBeInTheDocument();
expect(screen.getByText("OPP-12345")).toBeInTheDocument();
});

getDateTestCases().forEach(({ api_date, ui_date }) => {
it(`renders formatted date ${ui_date} for API date ${api_date}`, () => {
const opportunityWithDate = {
...mockOpportunity,
summary: {
...mockOpportunity.summary,
post_date: api_date,
},
};

render(<SearchResultsListItem opportunity={opportunityWithDate} />);
expect(screen.getByText(ui_date)).toBeInTheDocument();
});
});
});

function getDateTestCases() {
return [
{ api_date: "2024-05-01", ui_date: "May 1, 2024" },
{ api_date: "2023-07-21", ui_date: "July 21, 2023" },
{ api_date: "2022-11-30", ui_date: "November 30, 2022" },
{ api_date: "2021-01-15", ui_date: "January 15, 2021" },
{ api_date: "2020-06-17", ui_date: "June 17, 2020" },
{ api_date: "2019-08-25", ui_date: "August 25, 2019" },
{ api_date: "2018-12-05", ui_date: "December 5, 2018" },
{ api_date: "2017-09-13", ui_date: "September 13, 2017" },
{ api_date: "2016-04-07", ui_date: "April 7, 2016" },
{ api_date: "2015-03-23", ui_date: "March 23, 2015" },
];
}

0 comments on commit 706dd8d

Please sign in to comment.