Skip to content

Commit

Permalink
added more test
Browse files Browse the repository at this point in the history
  • Loading branch information
santanaj committed Jul 3, 2022
1 parent fcf072a commit 8eaabef
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 47 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Show Time Ago Utility

[![GPLv3 License](https://img.shields.io/badge/License-GPL%20v3-yellow.svg)](https://opensource.org/licenses/) <a href="https://www.npmjs.com/package/showtimeago"><img src="https://img.shields.io/npm/v/showtimeago.svg?style=flat-square&colorB=51C838"
alt="NPM Version"></a> <a href="https://github.com/jackie1santana/showTimeAgoUtility/actions/workflows/unit-test-action.yml">[![Test showTimeAgo Utility](https://github.com/jackie1santana/showTimeAgoUtility/actions/workflows/unit-test-action.yml/badge.svg)](https://github.com/jackie1santana/showTimeAgoUtility/actions/workflows/unit-test-action.yml)</a>

alt="NPM Version"></a> <a href="https://github.com/jackie1santana/showTimeAgoUtility/actions/workflows/unit-test-action.yml">[![Test showTimeAgo Utility](https://github.com/jackie1santana/showTimeAgoUtility/actions/workflows/unit-test-action.yml/badge.svg)](https://github.com/jackie1santana/showTimeAgoUtility/actions/workflows/unit-test-action.yml)</a> [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)



Expand Down
97 changes: 78 additions & 19 deletions __test__/showTimeAgo.spec.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,58 @@
const showTimeAgo = require('../index');

const today = new Date();
const currentYear = today.getFullYear();

// const shouldInclude = 'now'
// || 'seconds ago'
// || 'about a minute ago'
// || 'minutes ago'
// || 'hour ago'
// || 'hours ago'
// || 'days ago'
// || 'day ago'
// || 'month ago'
// || 'months ago'
// || 'year ago'
// || 'years ago';
// || 'Yesterday'
// || 'Today';
const getCurrentDay = [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'
]

const getCurrentMonth = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
]


//random number of days
const randomDayNumber = (min, max) => { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}

//check previous day
const prevNumberDay = new Date().getDate() - 1;
const previousDay = new Date(`${getCurrentDay[today.getDay()]} ${getCurrentMonth[today.getMonth()]} ${prevNumberDay} ${currentYear}`)

//check previous days
const previousDays = new Date(`${getCurrentDay[today.getDay()]} ${getCurrentMonth[today.getMonth()]} ${today.getDate() - 2} ${currentYear}`)

//create random years
const randomYearNumber = (min, max) => { // min and max included
return Math.floor(Math.random() * (max - min + 1) + min)
}

//check previous year
const prevYear = today.getFullYear() - 1;
const aYearFromNow = new Date(`Sun July 02 ${prevYear}`)

//check previous years
const prevYears = today.getFullYear() - randomYearNumber(2, 20);
const yearsFromNow = new Date(`Sun July 02 ${prevYears}`)

test('showTimeAgo Utility to return a string', () => {
expect(typeof showTimeAgo(today)).toBe('string');
Expand All @@ -29,14 +66,36 @@ test('showTimeAgo Utility to not be NaN', () => {
expect(typeof showTimeAgo(today)).not.toBeNaN();
});

// test('showTimeAgo Utility to include certain words', () => {
// expect(showTimeAgo(today)).toContain(shouldInclude);
// });

test('showTimeAgo Utility by default should not have an argument', () => {
expect(showTimeAgo()).toBeNull();
});

test('showTimeAgo Utility type to be a function object', () => {
expect(typeof showTimeAgo()).toBe('object');
});
});

test('Test Date today', () => {
expect(showTimeAgo(today)).toContain('now');
});

test('Test Date from Yesterday', () => {
// if test fail, it is because it is the same day or it will should a - negative integer.
expect(showTimeAgo(previousDay)).toContain('Yesterday at');
});


test('Test Date from days ago', () => {
// if test fail, it is because it is the same day or 2nd day of the month or it will should a - negative integer.
expect(showTimeAgo(previousDays)).toContain('days ago');
});


test('Test Date from 1 year ago', () => {
expect(showTimeAgo(aYearFromNow)).toBe('1 year ago');
});

test('Test Date from years ago', () => {
expect(showTimeAgo(yearsFromNow)).toContain('years ago');
});


35 changes: 10 additions & 25 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,8 @@ try {
'December',
];

function getOrdinalNum() {
return (
n +
(n > 0 ? ['th', 'st', 'nd', 'rd'][
(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10
] :
'')
);
function getOrdinalNum(n) {
return (n +(n > 0 ? ['th', 'st', 'nd', 'rd'][(n > 3 && n < 21) || n % 10 > 3 ? 0 : n % 10] :''));
}

function getFormattedDate(
Expand All @@ -42,25 +36,21 @@ try {

switch (true) {
case (hours > 12):
hours -= 12;
hours = 12;
case (hours === 0):
hours = 12;
case (minutes < 10):
minutes = `${minutes}`;
minutes = `0${minutes}`;
case (preformattedDate):
// Today at 10:20am
// Yesterday at 10:20am
return `${ preformattedDate } at ${ hours }:${ minutes } ${ampm}`;
case (hideYear):
// January 10th at 10:20pm
return `${month} ${getOrdinalNum(
day
)}, at ${hours}:${minutes} ${ampm}`;
return `${month} ${getOrdinalNum(day)}, at ${hours}:${minutes} ${ampm}`;
default:
// January 10th 2022 at 10:20pm
return `${month} ${getOrdinalNum(
day
)}, ${year} at ${hours}:${minutes} ${ampm}`;
return `${month} ${getOrdinalNum(day)}, ${year} at ${hours}:${minutes} ${ampm}`;
}

}
Expand All @@ -71,8 +61,7 @@ try {
return null;
}

const date =
typeof dateParam === 'object' ? dateParam : new Date(dateParam);
const date = typeof dateParam === 'object' ? dateParam : new Date(dateParam);
const DAY_IN_MS = 86400000; // 24 * 60 * 60 * 1000
const today = new Date();

Expand All @@ -86,7 +75,6 @@ try {
const year = Math.floor(seconds / 31536000);
const isToday = today.toDateString() === date.toDateString();
const isYesterday = yesterday.toDateString() === date.toDateString();
const isThisYear = today.getFullYear() === date.getFullYear();

switch (true) {
case (seconds < 5):
Expand All @@ -105,13 +93,11 @@ try {
return getFormattedDate(date, 'Today'); // Today at 10:20am
case (isYesterday):
return getFormattedDate(date, 'Yesterday'); // Yesterday at 10:20am
case (day > 1 && day <= 30):
case (day <= 30):
return `${day} days ago`; // 2 days ago
case (isThisYear):
return getFormattedDate(date, false, true); // January 10th at 10:20pm
case (day > 30 && month <= 1):
return `${hour} month ago`; // 1 month ago
case (month > 1 && month <= 12):
return `${month} month ago`; // 1 month ago
case (month > 1 && month < 12):
return `${month} months ago`; // 2 months ago
case (year === 1):
return `${year} year ago`; // 1 year ago
Expand All @@ -124,7 +110,6 @@ try {

return timeAgo(date);
}

} catch (error) {
core.setFailed(error.message);
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "showtimeago",
"version": "3.3.6",
"version": "3.3.8",
"description": "Show Time Ago is a utility that allows you to see how long ago a date was.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 8eaabef

Please sign in to comment.