Skip to content

Commit c29768c

Browse files
committed
Correct the date-time component.
1 parent 6bee0ef commit c29768c

File tree

2 files changed

+55
-15
lines changed

2 files changed

+55
-15
lines changed
Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
1-
import useLocalStorage from "../../hooks/local-storage.hook";
1+
import { useLocalStorage } from "primereact/hooks";
2+
import { SupportedLocales } from "../../config/prime-locale";
23

34
const _ = ({ date }: { date?: string }) => {
4-
const [language] = useLocalStorage('language', 'en');
5+
const [language] = useLocalStorage<SupportedLocales>('en', 'language');
56

6-
if (date) {
7-
const formatted = new Intl.DateTimeFormat(language as string, {
8-
year: 'numeric',
9-
month: 'numeric',
10-
day: 'numeric',
11-
hour: 'numeric',
12-
minute: 'numeric'
13-
}).format(new Date(date))
14-
return (
15-
<span className='FormattedDate'>{ formatted }</span>
16-
)
17-
}
7+
if (date) {
8+
const formatted = new Intl.DateTimeFormat(language as string, {
9+
year: 'numeric',
10+
month: 'numeric',
11+
day: 'numeric',
12+
hour: 'numeric',
13+
minute: 'numeric'
14+
}).format(new Date(date))
15+
return (
16+
<span className='FormattedDate'>{ formatted }</span>
17+
)
18+
}
1819

19-
return <></>
20+
return <></>
2021
}
2122

2223
export default _
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// date-time.spec.tsx
2+
import { render, screen } from '@testing-library/react';
3+
import { describe, expect, it, vi } from 'vitest';
4+
import _ from './date-time.component';
5+
6+
describe('date-time.component', () => {
7+
it('renders formatted date when date and language are provided', () => {
8+
const date = '2025-09-15T12:34:56Z';
9+
10+
render(<_ date={ date }/>);
11+
12+
const formattedDate = new Intl.DateTimeFormat('en', {
13+
year: 'numeric',
14+
month: 'numeric',
15+
day: 'numeric',
16+
hour: 'numeric',
17+
minute: 'numeric'
18+
}).format(new Date(date));
19+
20+
expect(screen.getByText(formattedDate)).toBeInTheDocument();
21+
});
22+
23+
it('formats date based on different language setting', () => {
24+
localStorage.setItem("language", '"nl"')
25+
const date = '2025-09-15T12:34:56Z';
26+
27+
render(<_ date={ date }/>);
28+
29+
const formattedDate = new Intl.DateTimeFormat('nl', {
30+
year: 'numeric',
31+
month: 'numeric',
32+
day: 'numeric',
33+
hour: 'numeric',
34+
minute: 'numeric'
35+
}).format(new Date(date));
36+
37+
expect(screen.getByText(formattedDate)).toBeInTheDocument();
38+
});
39+
});

0 commit comments

Comments
 (0)