Skip to content

Commit ec3af40

Browse files
add tests to activeSince function
1 parent 318df17 commit ec3af40

File tree

5 files changed

+72
-6
lines changed

5 files changed

+72
-6
lines changed

web/locales/en/plugin__netobserv-plugin.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -584,5 +584,6 @@
584584
"The top DNS response code extracted from DNS response headers compared to total over the selected interval": "The top DNS response code extracted from DNS response headers compared to total over the selected interval",
585585
"rates": "rates",
586586
"with total": "with total",
587-
"Invalid custom panel id": "Invalid custom panel id"
587+
"Invalid custom panel id": "Invalid custom panel id",
588+
"Yesterday": "Yesterday"
588589
}

web/src/components/health/alert-details.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export const AlertDetails: React.FC<AlertDetailsProps> = ({ resourceName, alert
2727
<AlertDetailsValue title={t('State')}>{alert.state}</AlertDetailsValue>
2828
<AlertDetailsValue title={t('Severity')}>{alert.labels.severity}</AlertDetailsValue>
2929
{alert.activeAt && (
30-
<AlertDetailsValue title={t('Active since')}>{formatActiveSince(alert.activeAt)}</AlertDetailsValue>
30+
<AlertDetailsValue title={t('Active since')}>{formatActiveSince(t, alert.activeAt)}</AlertDetailsValue>
3131
)}
3232
<AlertDetailsValue title={t('Labels')}>
3333
{labels.length === 0

web/src/components/health/alert-row.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export const AlertRow: React.FC<AlertRowProps> = ({ kind, resourceName, alert, w
4040
)}
4141
<Td noPadding={!wide}>{alert.state}</Td>
4242
<Td>{alert.labels.severity}</Td>
43-
{alert.activeAt && <Td>{formatActiveSince(alert.activeAt)}</Td>}
43+
{alert.activeAt && <Td>{formatActiveSince(t, alert.activeAt)}</Td>}
4444
<Td>
4545
{labels.length === 0
4646
? t('None')

web/src/utils/__tests__/datetime.spec.ts

Lines changed: 66 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
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

35
describe('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+
});

web/src/utils/datetime.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export const computeStepInterval = (range: TimeRange | number) => {
131131
* - Last 7 days: Shows day of week and time (Tue, 14:23)
132132
* - Older: Shows full date and time (2025-11-24 14:23)
133133
*/
134-
export const formatActiveSince = (timestamp: string): string => {
134+
export const formatActiveSince = (t: TFunction, timestamp: string): string => {
135135
const activeDate = new Date(timestamp);
136136
const now = new Date();
137137

@@ -155,7 +155,7 @@ export const formatActiveSince = (timestamp: string): string => {
155155
activeDate.getMonth() === yesterday.getMonth() &&
156156
activeDate.getFullYear() === yesterday.getFullYear()
157157
) {
158-
return `Yesterday, ${timeStr}`;
158+
return `${t('Yesterday')}, ${timeStr}`;
159159
}
160160

161161
// Last 7 days: show day of week and time

0 commit comments

Comments
 (0)