1- import { toISODateString , twentyFourHourTime } from '../datetime' ;
1+ import { TFunction } from 'react-i18next' ;
2+ import { formatActiveSince , toISODateString , twentyFourHourTime } from '../datetime' ;
3+ import { getLanguage } from '../language' ;
24
35describe ( 'datetime' , ( ) => {
46 it ( 'should toISODateString' , ( ) => {
@@ -13,3 +15,66 @@ describe('datetime', () => {
1315 expect ( twentyFourHourTime ( new Date ( '1955-11-05T06:15:00' ) ) ) . toBe ( '06:15' ) ;
1416 } ) ;
1517} ) ;
18+
19+ describe ( 'formatActiveSince' , ( ) => {
20+ const FIXED_NOW = new Date ( '2025-11-27T15:00:00' ) ;
21+ const tMock : TFunction = ( ( key : string ) => key ) as unknown as TFunction ;
22+
23+ beforeAll ( ( ) => {
24+ jest . useFakeTimers ( ) ;
25+ jest . setSystemTime ( FIXED_NOW ) ;
26+ } ) ;
27+
28+ afterAll ( ( ) => {
29+ jest . useRealTimers ( ) ;
30+ } ) ;
31+
32+ it ( 'should format "today" as only time' , ( ) => {
33+ const ts = '2025-11-27T10:15:00' ;
34+ const date = new Date ( ts ) ;
35+
36+ const expectedTime = twentyFourHourTime ( date , false ) ;
37+
38+ const result = formatActiveSince ( tMock , ts ) ;
39+
40+ expect ( result ) . toBe ( expectedTime ) ;
41+ } ) ;
42+
43+ it ( 'should format "yesterday" as "Yesterday, HH:MM"' , ( ) => {
44+ const ts = '2025-11-26T22:00:00' ;
45+ const date = new Date ( ts ) ;
46+
47+ const expectedTime = twentyFourHourTime ( date , false ) ;
48+
49+ const result = formatActiveSince ( tMock , ts ) ;
50+
51+ expect ( result ) . toBe ( `Yesterday, ${ expectedTime } ` ) ;
52+ } ) ;
53+
54+ it ( 'should format dates within the last 7 days as "Weekday, HH:MM"' , ( ) => {
55+ const ts = '2025-11-25T09:30:00' ;
56+ const date = new Date ( ts ) ;
57+
58+ const weekdayFormatter = new Intl . DateTimeFormat ( getLanguage ( ) , {
59+ weekday : 'short'
60+ } ) ;
61+ const weekday = weekdayFormatter . format ( date ) ;
62+ const time = twentyFourHourTime ( date , false ) ;
63+
64+ const result = formatActiveSince ( tMock , ts ) ;
65+
66+ expect ( result ) . toBe ( `${ weekday } , ${ time } ` ) ;
67+ } ) ;
68+
69+ it ( 'should format older dates with "YYYY-MM-DD HH:MM"' , ( ) => {
70+ const ts = '2025-11-07T14:00:00' ;
71+ const date = new Date ( ts ) ;
72+
73+ const expectedDate = toISODateString ( date ) ;
74+ const expectedTime = twentyFourHourTime ( date , false ) ;
75+
76+ const result = formatActiveSince ( tMock , ts ) ;
77+
78+ expect ( result ) . toBe ( `${ expectedDate } ${ expectedTime } ` ) ;
79+ } ) ;
80+ } ) ;
0 commit comments