| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- import { describe, test, it, expect } from 'vitest'
- import DateUtils, { supportedLocales } from '~/services/utils/dateUtils'
- describe('format', () => {
- test('simple formatting', () => {
- const input = new Date(2020, 4, 12)
- expect(DateUtils.format(input, 'y-MM-dd')).toEqual('2020-05-12')
- })
- })
- describe('formatDatesAndConcat', () => {
- test('simple array and default sep', () => {
- const input = [
- new Date(2023, 0, 10),
- new Date(2023, 0, 11),
- new Date(2023, 0, 12),
- ]
- const result = DateUtils.formatAndConcat(input, 'dd/MM/y')
- expect(result).toEqual('10/01/2023 - 11/01/2023 - 12/01/2023')
- })
- test('single date and default sep', () => {
- const input = new Date(2023, 0, 10)
- const result = DateUtils.formatAndConcat(input, 'dd/MM/y')
- expect(result).toEqual('10/01/2023')
- })
- test('simple array with other format and custom sep', () => {
- const input = [
- new Date(2023, 0, 10),
- new Date(2023, 0, 11),
- new Date(2023, 0, 12),
- ]
- const result = DateUtils.formatAndConcat(input, 'yMMdd', '|')
- expect(result).toEqual('20230110|20230111|20230112')
- })
- test('empty array', () => {
- expect(DateUtils.formatAndConcat([], 'dd-MM-y')).toEqual('')
- })
- })
- describe('sortDate', () => {
- test('simple array', () => {
- const date1 = new Date(2023, 0, 10)
- const date2 = new Date(2023, 0, 14)
- const date3 = new Date(2023, 0, 12)
- const input = [date1, date2, date3]
- const result = DateUtils.sort(input)
- expect(result).toEqual([date1, date3, date2])
- })
- test('simple array reverse', () => {
- const date1 = new Date(2023, 0, 10)
- const date2 = new Date(2023, 0, 14)
- const date3 = new Date(2023, 0, 12)
- const input = [date1, date2, date3]
- const result = DateUtils.sort(input, true)
- expect(result).toEqual([date2, date3, date1])
- })
- })
- describe('getFnsLocale', () => {
- test('standard call', () => {
- expect(DateUtils.getFnsLocale(supportedLocales.FR).code).toEqual('fr')
- expect(DateUtils.getFnsLocale(supportedLocales.EN).code).toEqual('en-US')
- })
- test('unsupported locale', () => {
- // @ts-ignore
- expect(DateUtils.getFnsLocale('xx').code).toEqual('fr')
- })
- })
- describe('getShortFormatPattern', () => {
- test('standard call', () => {
- expect(DateUtils.getShortFormatPattern(supportedLocales.FR)).toEqual(
- 'dd/MM/yyyy',
- )
- expect(DateUtils.getShortFormatPattern(supportedLocales.EN)).toEqual(
- 'MM/dd/yyyy',
- )
- })
- test('unsupported locale', () => {
- // @ts-ignore
- expect(DateUtils.getShortFormatPattern('xx')).toEqual('dd/MM/yyyy')
- })
- })
- describe('getFormatPattern', () => {
- test('standard call', () => {
- expect(DateUtils.getFormatPattern(supportedLocales.FR)).toEqual(
- 'dd/MM/yyyy HH:mm',
- )
- expect(DateUtils.getFormatPattern(supportedLocales.EN)).toEqual(
- 'MM/dd/yyyy HH:mm',
- )
- })
- test('unsupported locale', () => {
- // @ts-ignore
- expect(DateUtils.getFormatPattern('xx')).toEqual('dd/MM/yyyy HH:mm')
- })
- })
- describe('formatIsoShortDate', () => {
- test('standard call', () => {
- expect(DateUtils.formatIsoShortDate(new Date(2023, 0, 10))).toEqual(
- '2023-01-10',
- )
- expect(DateUtils.formatIsoShortDate(new Date(2023, 11, 20))).toEqual(
- '2023-12-20',
- )
- })
- })
|