import { registerLocaleData } from '@angular/common'; import localeDe from '@angular/common/locales/de'; import { format } from 'date-fns'; import { de } from 'date-fns/locale'; import { formatForDatabase, formatFullDate, formatFullDateWithTimeAndDay, formatFullDateWithTimeWithoutSeconds, formatToPrettyDate, formatWithoutYear } from './date.util'; registerLocaleData(localeDe); describe('Date Util', () => { const dateToFormat: Date = new Date('1010-01-01:01:01:01'); describe('formatForDatabase()', () => { it('should return correct date string with Date object', () => { const formattedDate: string = formatForDatabase(dateToFormat); expect(formattedDate).toEqual('1010-01-01'); }) }) it('should return full date with hour and minutes', () => { const formattedDate: string = formatFullDateWithTimeWithoutSeconds(dateToFormat); expect(formattedDate).toEqual('01.01.10 01:01'); }) it('should return name of day, full date, hour, minute and seconds', () => { const formattedDate: string = formatFullDateWithTimeAndDay(dateToFormat); expect(formattedDate).toEqual('Montag, 01.01.1010, 01:01:01'); }) it('should return full date', () => { const formattedDate: string = formatFullDate(dateToFormat); expect(formattedDate).toEqual('01.01.1010'); }) it('should return day and name of month', () => { const formattedDate: string = formatWithoutYear(dateToFormat); expect(formattedDate).toEqual('01. Jan.'); }) describe('formatToPrettyDate()', () => { it('should format date without year', () => { const today: Date = new Date(); const day: string = format(today, 'dd'); const month: string = format(today, 'MMMM', {locale: de}); const result: string = formatToPrettyDate(today); expect(result).toEqual(`${day}. ${month}`); }) it('should format date without year', () => { const result: string = formatToPrettyDate(dateToFormat); expect(result).toEqual('01.01.1010'); }) }) })