Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: FLUI-146 fix date test and typo #565

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/ui/Release.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### 10.15.3 2025-01-07
- fix: FLUI-146 fix test + typo yesterday

### 10.15.2 2025-01-02
- fix: SKFP-1379 fis JS errors in graph

Expand Down
2 changes: 1 addition & 1 deletion packages/ui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ferlab/ui",
"version": "10.15.2",
"version": "10.15.3",
"description": "Core components for scientific research data portals",
"publishConfig": {
"access": "public"
Expand Down
2 changes: 1 addition & 1 deletion packages/ui/src/components/Flag/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export interface IFlagDictionary {
export interface IFlagDate {
now?: string;
today?: string;
yesteday?: string;
yesterday?: string;
thisYear?: string;
pastYear?: string;
}
Expand Down
72 changes: 38 additions & 34 deletions packages/ui/src/utils/dateUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('Relative date', () => {
pastYear: 'Past Years',
thisYear: 'This Years',
today: 'today',
yesteday: 'yesteday',
yesterday: 'yesterday',
};
test('should return now', () => {
const date = new Date();
Expand All @@ -17,19 +17,23 @@ describe('Relative date', () => {
const date = new Date(Date.now() - 1000 * 60);
expect(getRelativeDate(date, format)).toEqual('today');
});
test('should return yesteday', () => {
test('should return yesterday', () => {
const today = new Date();
const yesterday = new Date(today);
yesterday.setDate(today.getDate() - 1);
expect(getRelativeDate(yesterday, format)).toEqual('yesteday');
expect(getRelativeDate(yesterday, format)).toEqual('yesterday');
});
test('should return this years date', () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should return this year date non ?

const today = new Date();
const startYear = new Date(today.getFullYear(), 0, 1);
const endPeriodTime = new Date(today.getFullYear(), today.getMonth(), today.getDate() - 2);

const randomDate = new Date(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pourquoi avoir une date random et non fixe ?
Il me semble qu'il est préférable d'avoir des tests prédictibles pour les tests.

startYear.getTime() + Math.random() * (endPeriodTime.getTime() - startYear.getTime()),
);

expect(getRelativeDate(randomDate, format)).toEqual('This Years');
});
// FLUI-146 failed the seven first days of a year
// test('should return this years date', () => {
// const today = new Date();
// const pastDate = new Date(today);
// pastDate.setDate(today.getDate() - 7);
// expect(getRelativeDate(pastDate, format)).toEqual('This Years');
// });
test('should return past years date', () => {
const today = new Date();
const pastDate = new Date(today);
Expand All @@ -56,56 +60,56 @@ describe('Relative date', () => {
});
test('should return correct time am en', () => {
const formatTime = {
yesteday: 'Yesteday at {hour}{ampm}',
yesterday: 'Yesterday at {hour}{ampm}',
};
setLocale('en');

const yesteday = new Date();
yesteday.setDate(yesteday.getDate() - 1);
yesteday.setHours(11);
yesteday.setMinutes(52);
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(11);
yesterday.setMinutes(52);

expect(getRelativeDate(yesteday, formatTime)).toEqual('Yesteday at 11:52am');
expect(getRelativeDate(yesterday, formatTime)).toEqual('Yesterday at 11:52am');
});
test('should return correct time am fr', () => {
const formatTime = {
yesteday: 'Hier à {hour}{ampm}',
yesterday: 'Hier à {hour}{ampm}',
};
setLocale('fr');

const yesteday = new Date();
yesteday.setDate(yesteday.getDate() - 1);
yesteday.setHours(11);
yesteday.setMinutes(52);
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(11);
yesterday.setMinutes(52);

expect(getRelativeDate(yesteday, formatTime)).toEqual('Hier à 11h52');
expect(getRelativeDate(yesterday, formatTime)).toEqual('Hier à 11h52');
});

test('should return correct time pm en', () => {
const formatTime = {
yesteday: 'Yesteday at {hour}{ampm}',
yesterday: 'Yesterday at {hour}{ampm}',
};
setLocale('en');

const yesteday = new Date();
yesteday.setDate(yesteday.getDate() - 1);
yesteday.setHours(16);
yesteday.setMinutes(52);
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(16);
yesterday.setMinutes(52);

expect(getRelativeDate(yesteday, formatTime)).toEqual('Yesteday at 4:52pm');
expect(getRelativeDate(yesterday, formatTime)).toEqual('Yesterday at 4:52pm');
});

test('should return correct time pm fr', () => {
const formatTime = {
yesteday: 'Hier à {hour}{ampm}',
yesterday: 'Hier à {hour}{ampm}',
};
setLocale('fr');

const yesteday = new Date();
yesteday.setDate(yesteday.getDate() - 1);
yesteday.setHours(16);
yesteday.setMinutes(52);
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
yesterday.setHours(16);
yesterday.setMinutes(52);

expect(getRelativeDate(yesteday, formatTime)).toEqual('Hier à 16h52');
expect(getRelativeDate(yesterday, formatTime)).toEqual('Hier à 16h52');
});
});
12 changes: 6 additions & 6 deletions packages/ui/src/utils/dateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ export default (
pastYear: '{month} {day}th {year}',
thisYear: '{month} {day}th at {time}{ampm}',
today: 'Today at {hour}{ampm}',
yesteday: 'Yesterday at {hour}{ampm}',
yesterday: 'Yesterday at {hour}{ampm}',
},
): string | undefined => {
const diff = Math.round((new Date().getTime() - new Date(date).getTime()) / 1000);
const now = new Date();
const hours = date.getHours();
const yesteday = new Date();
yesteday.setDate(yesteday.getDate() - 1);
const yesterday = new Date();
yesterday.setDate(yesterday.getDate() - 1);
const locale = localStorage.getItem('locale') === 'fr' ? 'fr-CA' : 'en-US';
const ampm = hours >= 12 ? 'pm' : 'am';

Expand All @@ -49,9 +49,9 @@ export default (
return format?.today
?.replace('{hour}', formatTimeToLocal(date, locale))
.replace('{ampm}', locale === 'en-US' ? ampm : '');
} else if (date.toDateString() === yesteday.toDateString()) {
//Yesteday
return format?.yesteday
} else if (date.toDateString() === yesterday.toDateString()) {
//Yesterday
return format?.yesterday
?.replace('{hour}', formatTimeToLocal(date, locale))
.replace('{ampm}', locale === 'en-US' ? ampm : '');
} else if (date.getFullYear() === now.getFullYear()) {
Expand Down
Loading