Skip to content

Commit

Permalink
JessicaH- merged dayUtil into dateUtil and added tests for calculateDays
Browse files Browse the repository at this point in the history
  • Loading branch information
Jessica Ho committed Jun 3, 2023
1 parent 92eff30 commit abfc085
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
6 changes: 3 additions & 3 deletions frontend/src/main/components/Commons/CommonsOverview.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { Row, Card, Col, Button } from "react-bootstrap";
import { useNavigate } from "react-router-dom";
import { hasRole } from "main/utils/currentUser";
import { calculateDays } from "main/utils/dayUtils";
import { calculateDays } from "main/utils/dateUtils";

export default function CommonsOverview({ commons, currentUser }) {

Expand All @@ -11,8 +11,8 @@ export default function CommonsOverview({ commons, currentUser }) {
const leaderboardButtonClick = () => { navigate("/leaderboard/" + commons.id) };
const showLeaderboard = (hasRole(currentUser, "ROLE_ADMIN") || commons.showLeaderboard );

//const startingDate = commons.startingDate;
commons.day = calculateDays(commons.startingDate);
const today = new Date();
commons.day = calculateDays(commons.startingDate,today);

return (
<Card data-testid="CommonsOverview">
Expand Down
11 changes: 10 additions & 1 deletion frontend/src/main/utils/dateUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,13 @@ const timestampToDate = (timestamp) => {
return (date.getFullYear() + "-" + (padWithZero(date.getMonth()+1)) + "-" + padWithZero(date.getDate()));
}

export {timestampToDate, padWithZero};

const calculateDays = (startingDate,currentDate) => {
const start = new Date(startingDate);
const today = new Date(currentDate);
const timeDifference = today.getTime() - start.getTime();
const days = Math.floor(timeDifference / (1000 * 3600 * 24));
return days;
}

export {timestampToDate, padWithZero, calculateDays};
7 changes: 0 additions & 7 deletions frontend/src/main/utils/dayUtils.js

This file was deleted.

11 changes: 10 additions & 1 deletion frontend/src/tests/utils/dateUtils.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { padWithZero, timestampToDate } from "main/utils/dateUtils";
import { padWithZero, timestampToDate, calculateDays} from "main/utils/dateUtils";


describe("dateUtils tests", () => {
Expand All @@ -25,4 +25,13 @@ describe("dateUtils tests", () => {
});
});

describe("calculateDays tests", () => {
test("with mock current date 2023-06-01T00:00:00", () => {
const startingDate = "2023-05-01T00:00:00";
const currentDate = "2023-06-01T00:00:00";
expect(calculateDays(startingDate,currentDate)).toBe(31);

});
});

});

0 comments on commit abfc085

Please sign in to comment.