diff --git a/package.json b/package.json index 115f5921b..74185c588 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@equinor/amplify-component-lib", - "version": "9.9.1", + "version": "9.9.2", "description": "Frontend Typescript components for the Amplify team", "main": "dist/index.js", "types": "dist/index.d.ts", diff --git a/src/atoms/utils/date.test.ts b/src/atoms/utils/date.test.ts index cd75d7d00..16c603240 100644 --- a/src/atoms/utils/date.test.ts +++ b/src/atoms/utils/date.test.ts @@ -10,7 +10,7 @@ import { test('formatDate works as expected when not sending in a date', () => { const formattedDate = formatDate(null); - expect(formattedDate).toBe(''); + expect(formattedDate).toBe(undefined); }); test('formatDate works as expected with default format', () => { @@ -177,6 +177,11 @@ test('formatDate works as expected with format = "DD. month" and month = "short" expect(formattedDate).toBe(testFormatted); }); +test('formatDate with null should be undefined', () => { + const formatted = formatDate(null); + expect(formatted).toBe(undefined); +}); + test('formatDateTime works as expected', () => { const today = new Date(); const fakeDate = faker.date.past({ @@ -196,7 +201,7 @@ test('formatDateTime works as expected', () => { test('formatDateTime works as expected when not sending in a date', () => { const formatted = formatDateTime(null); - expect(formatted).toBe(''); + expect(formatted).toBe(undefined); }); test('formatDateTime works as expected with options', () => { @@ -234,9 +239,9 @@ test('formatDateTime works as expected when isGMT is set', () => { expect(formatted).toBe(expectedResult); }); -test('formatRelativeDateTime with null should be empty string', () => { +test('formatRelativeDateTime with null should be undefined', () => { const formatted = formatRelativeDateTime(null); - expect(formatted).toContain(''); + expect(formatted).toBe(undefined); }); test('formatRelativeDateTime with isGMT set should convert to local time', () => { @@ -421,6 +426,11 @@ test('formatRelativeDateTime with date not in current year displays as full date expect(formattedDateInNextYear).toBe(expectedNextYearResult); }); +test('formatRelativeDateTime with null should be undefined ', () => { + const formatted = formatDate(null); + expect(formatted).toBe(undefined); +}); + test('isBetweenDates works as expected with null', () => { const yesterday = new Date(new Date().setDate(new Date().getDate() - 1)); const tomorrow = new Date(new Date().setDate(new Date().getDate() + 1)); diff --git a/src/atoms/utils/date.ts b/src/atoms/utils/date.ts index 2e2028b32..0ece1e5c6 100644 --- a/src/atoms/utils/date.ts +++ b/src/atoms/utils/date.ts @@ -20,7 +20,7 @@ export const formatDate = ( format: 'DD. month YYYY', month: 'long', } -): string => { +): string | undefined => { if (date) { const dateObj = new Date(date); if (dateObj.getTime()) { @@ -68,7 +68,7 @@ export const formatDate = ( .padStart(2, '0')}.${year.toString().padStart(4, '0')}`; } } - return ''; + return undefined; }; // formatDateTime(new Date()) => 19. January 2022, 01:32 @@ -80,7 +80,7 @@ export const formatDateTime = ( hideYear: false, isGMT: false, } -): string => { +): string | undefined => { if (date) { const dateObj = new Date(date); if (dateObj.getTime()) { @@ -98,7 +98,7 @@ export const formatDateTime = ( })}`; } } - return ''; + return undefined; }; // formatRelativeDateTime(new Date()) => Today at 7:17 @@ -106,7 +106,7 @@ export const formatDateTime = ( export const formatRelativeDateTime = ( date: Date | string | null | undefined, isGMT = false -): string => { +): string | undefined => { if (date) { const dateObj = new Date(date); const today = new Date(); @@ -150,7 +150,7 @@ export const formatRelativeDateTime = ( } } } - return ''; + return undefined; }; export const isBetweenDates = (