From b4fe7aaaa01b860db66d65a3bc975cf205a1bd28 Mon Sep 17 00:00:00 2001 From: Nuwan Date: Tue, 27 Jan 2026 14:03:58 +0530 Subject: [PATCH] test(08-02): add failing test for formatTimestamp utility RED phase: Create test file with 6 test cases for timestamp formatting: - "Just now" for messages within last minute - "X minutes ago" for messages within last hour - "X hours ago" for messages within last day - "Yesterday" for previous day messages - Date format (MM/DD/YYYY) for older messages - Graceful handling of invalid dates Install dayjs for timestamp formatting utility. Tests fail as expected (utility not implemented yet). Co-Authored-By: Claude Sonnet 4.5 --- jam-ui/package-lock.json | 7 ++++ jam-ui/package.json | 1 + .../utils/__tests__/formatTimestamp.test.js | 35 +++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 jam-ui/src/utils/__tests__/formatTimestamp.test.js diff --git a/jam-ui/package-lock.json b/jam-ui/package-lock.json index 6693e80fe..0daf442d0 100644 --- a/jam-ui/package-lock.json +++ b/jam-ui/package-lock.json @@ -32,6 +32,7 @@ "classnames": "^2.2.6", "creditcard.js": "^3.0.33", "custom-protocol-check": "^1.4.0", + "dayjs": "^1.11.19", "echarts": "^4.9.0", "echarts-for-react": "^2.0.16", "element-resize-event": "^3.0.3", @@ -9496,6 +9497,12 @@ "webidl-conversions": "^4.0.2" } }, + "node_modules/dayjs": { + "version": "1.11.19", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.19.tgz", + "integrity": "sha512-t5EcLVS6QPBNqM2z8fakk/NKel+Xzshgt8FFKAn+qwlD1pzZWxh0nVCrvFK7ZDb6XucZeF9z8C7CBWTRIVApAw==", + "license": "MIT" + }, "node_modules/debounce": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/debounce/-/debounce-1.2.1.tgz", diff --git a/jam-ui/package.json b/jam-ui/package.json index f752e6f65..d204d5686 100644 --- a/jam-ui/package.json +++ b/jam-ui/package.json @@ -27,6 +27,7 @@ "classnames": "^2.2.6", "creditcard.js": "^3.0.33", "custom-protocol-check": "^1.4.0", + "dayjs": "^1.11.19", "echarts": "^4.9.0", "echarts-for-react": "^2.0.16", "element-resize-event": "^3.0.3", diff --git a/jam-ui/src/utils/__tests__/formatTimestamp.test.js b/jam-ui/src/utils/__tests__/formatTimestamp.test.js new file mode 100644 index 000000000..1a49e5290 --- /dev/null +++ b/jam-ui/src/utils/__tests__/formatTimestamp.test.js @@ -0,0 +1,35 @@ +import { formatTimestamp } from '../formatTimestamp'; + +describe('formatTimestamp', () => { + test('returns "Just now" for messages within last minute', () => { + const now = new Date(); + expect(formatTimestamp(now.toISOString())).toBe('Just now'); + }); + + test('returns "X minutes ago" for messages within last hour', () => { + const fiveMinutesAgo = new Date(Date.now() - 5 * 60 * 1000); + expect(formatTimestamp(fiveMinutesAgo.toISOString())).toBe('5 minutes ago'); + }); + + test('returns "X hours ago" for messages within last day', () => { + const twoHoursAgo = new Date(Date.now() - 2 * 60 * 60 * 1000); + expect(formatTimestamp(twoHoursAgo.toISOString())).toBe('2 hours ago'); + }); + + test('returns "Yesterday" for messages from previous day', () => { + const yesterday = new Date(Date.now() - 24 * 60 * 60 * 1000); + expect(formatTimestamp(yesterday.toISOString())).toMatch(/Yesterday/); + }); + + test('returns date for older messages', () => { + const lastWeek = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); + const result = formatTimestamp(lastWeek.toISOString()); + expect(result).toMatch(/\d{1,2}\/\d{1,2}\/\d{4}/); // MM/DD/YYYY format + }); + + test('handles invalid dates gracefully', () => { + expect(formatTimestamp(null)).toBe(''); + expect(formatTimestamp(undefined)).toBe(''); + expect(formatTimestamp('invalid')).toBe(''); + }); +});