This repository has been archived by the owner on Sep 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 148
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add test for dayjs
getDateFromNow
method (#390)
* 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
Showing
3 changed files
with
41 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |