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

Commit

Permalink
refactor: Add test for dayjs getDateFromNow method (#390)
Browse files Browse the repository at this point in the history
* feat: create hook for dayjs

* test: add test for getDateFromNow method

* refactor: update avatar component to use new lib function

* refactor: update to dynamic date string

* chore: cleanup

Co-authored-by: OGBONNA SUNDAY <62995161+SunGoldTech@users.noreply.github.com>
  • Loading branch information
OgDev-01 and OgDev-01 authored Jan 5, 2023
1 parent 6326836 commit e5fd935
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/components/Avatar.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { getAvatarLink, getProfileLink } from "../lib/github";
import * as HoverCardPrimitive from "@radix-ui/react-hover-card";
import cx from "classnames";
import dayjs from "dayjs/esm/index.js";
import relativeTime from "dayjs/esm/plugin/relativeTime";

dayjs.extend(relativeTime);
import getDateFromNow from "../lib/getDatefromNow";

export declare interface AvatarProps {
contributor: string;
Expand Down Expand Up @@ -68,7 +65,7 @@ const Avatar = ({ contributor, lastPr }: AvatarProps): JSX.Element => (
</h3>

<p className="mt-1 text-sm font-normal text-gray-700 dark:text-gray-400">
{`Last contribution was ${dayjs(lastPr).fromNow()}.`}
{`Last contribution was ${getDateFromNow(lastPr ?? "")}.`}
</p>
</div>
</div>
Expand Down
27 changes: 27 additions & 0 deletions src/lib/getDateFromNow.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import getDateFromNow from "./getDatefromNow";
import { describe, expect, test } from "vitest";

describe("Test: getDateFromNow() without suffix", () => {
test("should return 24 years", () => {
const date = (new Date);

date.setFullYear(date.getFullYear() - 24);
const dateString = date.toISOString().substring(0, 10);
const result = getDateFromNow(dateString, true);

expect(result).toBe("24 years");
});
});

describe("Test: getDateFromNow() with suffix", () => {
test("should return 20 years", () => {
const date = (new Date);

date.setFullYear(date.getFullYear() - 20);
const dateString = date.toISOString().substring(0, 10);

const result = getDateFromNow(dateString);

expect(result).toBe("20 years ago");
});
});
12 changes: 12 additions & 0 deletions src/lib/getDatefromNow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import dayjs from "dayjs/esm/index.js";
import relativeTime from "dayjs/esm/plugin/relativeTime";

dayjs.extend(relativeTime);

const getDateFromNow = (dateString: string, suffix?: boolean) => {
const dateSuffix = typeof suffix !== "undefined" ? suffix : false;

return dateSuffix ? dayjs(dateString).fromNow(true) : dayjs(dateString).fromNow();
};

export default getDateFromNow;

0 comments on commit e5fd935

Please sign in to comment.