1- import { formatJoinDate } from '../../../../src/modules/core/domain/utils/date.utils' ;
1+ import {
2+ formatDate ,
3+ formatDateTime ,
4+ formatRelativeDate ,
5+ formatDateParts ,
6+ getDateRangeLabel ,
7+ formatJoinDate ,
8+ } from '../../../../src/modules/core/domain/utils/date.utils' ;
9+
10+ // Fecha fija para hacer deterministas los tests de tiempo relativo
11+ const FIXED_NOW = new Date ( '2024-03-15T12:00:00Z' ) ;
212
313describe ( 'date.utils' , ( ) => {
14+ describe ( 'formatDate' , ( ) => {
15+ it ( 'debe formatear una fecha válida y contener el año' , ( ) => {
16+ const result = formatDate ( new Date ( '2024-06-15T12:00:00Z' ) ) ;
17+ expect ( result ) . toContain ( '2024' ) ;
18+ } ) ;
19+
20+ it ( 'debe aceptar un string ISO como fecha' , ( ) => {
21+ const result = formatDate ( '2024-06-20T12:00:00Z' ) ;
22+ expect ( result ) . toContain ( '2024' ) ;
23+ } ) ;
24+
25+ it ( 'debe aceptar un timestamp numérico' , ( ) => {
26+ const result = formatDate ( Date . UTC ( 2024 , 5 , 15 , 12 ) ) ;
27+ expect ( result ) . toContain ( '2024' ) ;
28+ } ) ;
29+
30+ it ( 'debe retornar cadena vacía para una fecha inválida' , ( ) => {
31+ expect ( formatDate ( 'fecha-invalida' ) ) . toBe ( '' ) ;
32+ } ) ;
33+
34+ it ( 'debe aceptar opciones de formato personalizadas' , ( ) => {
35+ const result = formatDate ( new Date ( '2024-01-15T12:00:00Z' ) , {
36+ year : 'numeric' ,
37+ month : 'long' ,
38+ day : '2-digit' ,
39+ } ) ;
40+ expect ( result ) . toContain ( '2024' ) ;
41+ } ) ;
42+ } ) ;
43+
44+ describe ( 'formatDateTime' , ( ) => {
45+ it ( 'debe incluir el año en el resultado' , ( ) => {
46+ const result = formatDateTime ( new Date ( '2024-03-10T14:30:00Z' ) ) ;
47+ expect ( result ) . toContain ( '2024' ) ;
48+ } ) ;
49+
50+ it ( 'debe retornar cadena vacía para una fecha inválida' , ( ) => {
51+ expect ( formatDateTime ( 'no-es-fecha' ) ) . toBe ( '' ) ;
52+ } ) ;
53+
54+ it ( 'debe aceptar un timestamp numérico' , ( ) => {
55+ const result = formatDateTime ( Date . UTC ( 2024 , 2 , 10 , 14 , 30 ) ) ;
56+ expect ( result ) . toContain ( '2024' ) ;
57+ } ) ;
58+ } ) ;
59+
60+ describe ( 'formatRelativeDate' , ( ) => {
61+ beforeEach ( ( ) => {
62+ jest . useFakeTimers ( ) ;
63+ jest . setSystemTime ( FIXED_NOW ) ;
64+ } ) ;
65+
66+ afterEach ( ( ) => {
67+ jest . useRealTimers ( ) ;
68+ } ) ;
69+
70+ it ( 'debe retornar tiempo en minutos para diferencias menores a 1 hora' , ( ) => {
71+ const future = new Date ( FIXED_NOW . getTime ( ) + 45 * 60 * 1000 ) ;
72+ expect ( formatRelativeDate ( future ) ) . toBe ( 'dentro de 45 minutos' ) ;
73+ } ) ;
74+
75+ it ( 'debe retornar tiempo en horas para diferencias entre 1 y 24 horas' , ( ) => {
76+ const future = new Date ( FIXED_NOW . getTime ( ) + 2 * 60 * 60 * 1000 ) ;
77+ expect ( formatRelativeDate ( future ) ) . toBe ( 'dentro de 2 horas' ) ;
78+ } ) ;
79+
80+ it ( 'debe retornar tiempo en días para diferencias mayores a 24 horas' , ( ) => {
81+ const past = new Date ( FIXED_NOW . getTime ( ) - 3 * 24 * 60 * 60 * 1000 ) ;
82+ expect ( formatRelativeDate ( past ) ) . toBe ( 'hace 3 días' ) ;
83+ } ) ;
84+
85+ it ( 'debe retornar tiempo pasado en minutos' , ( ) => {
86+ const past = new Date ( FIXED_NOW . getTime ( ) - 45 * 60 * 1000 ) ;
87+ expect ( formatRelativeDate ( past ) ) . toBe ( 'hace 45 minutos' ) ;
88+ } ) ;
89+
90+ it ( 'debe retornar cadena vacía para fecha inválida' , ( ) => {
91+ expect ( formatRelativeDate ( 'invalido' ) ) . toBe ( '' ) ;
92+ } ) ;
93+
94+ it ( 'debe aceptar locale personalizado' , ( ) => {
95+ const future = new Date ( FIXED_NOW . getTime ( ) + 45 * 60 * 1000 ) ;
96+ const result = formatRelativeDate ( future , 'en-US' ) ;
97+ expect ( result ) . toContain ( '45' ) ;
98+ } ) ;
99+ } ) ;
100+
101+ describe ( 'formatDateParts' , ( ) => {
102+ it ( 'debe retornar un arreglo de partes de fecha para una fecha válida' , ( ) => {
103+ const parts = formatDateParts ( new Date ( '2024-01-15T12:00:00Z' ) ) ;
104+ expect ( Array . isArray ( parts ) ) . toBe ( true ) ;
105+ expect ( parts . length ) . toBeGreaterThan ( 0 ) ;
106+ } ) ;
107+
108+ it ( 'debe incluir una parte de tipo año con el valor correcto' , ( ) => {
109+ const parts = formatDateParts ( new Date ( '2024-01-15T12:00:00Z' ) ) ;
110+ const yearPart = parts . find ( p => p . type === 'year' ) ;
111+ expect ( yearPart ) . toBeDefined ( ) ;
112+ expect ( yearPart ?. value ) . toBe ( '2024' ) ;
113+ } ) ;
114+
115+ it ( 'debe retornar arreglo vacío para fecha inválida' , ( ) => {
116+ expect ( formatDateParts ( 'fecha-invalida' ) ) . toEqual ( [ ] ) ;
117+ } ) ;
118+
119+ it ( 'debe incluir una parte de tipo mes cuando se solicita' , ( ) => {
120+ const parts = formatDateParts ( new Date ( '2024-01-15T12:00:00Z' ) , {
121+ month : 'long' ,
122+ } ) ;
123+ expect ( parts . some ( p => p . type === 'month' ) ) . toBe ( true ) ;
124+ } ) ;
125+ } ) ;
126+
127+ describe ( 'getDateRangeLabel' , ( ) => {
128+ it ( 'debe retornar un rango con separador " - " entre fechas' , ( ) => {
129+ const result = getDateRangeLabel (
130+ new Date ( '2024-01-15T12:00:00Z' ) ,
131+ new Date ( '2024-03-20T12:00:00Z' ) ,
132+ ) ;
133+ expect ( result ) . toContain ( ' - ' ) ;
134+ } ) ;
135+
136+ it ( 'debe incluir el año de inicio y fin en el rango' , ( ) => {
137+ const result = getDateRangeLabel (
138+ new Date ( '2023-01-01T12:00:00Z' ) ,
139+ new Date ( '2024-12-31T12:00:00Z' ) ,
140+ ) ;
141+ expect ( result ) . toContain ( '2023' ) ;
142+ expect ( result ) . toContain ( '2024' ) ;
143+ } ) ;
144+
145+ it ( 'debe retornar cadena vacía si la fecha de inicio es inválida' , ( ) => {
146+ expect ( getDateRangeLabel ( 'invalido' , new Date ( ) ) ) . toBe ( '' ) ;
147+ } ) ;
148+
149+ it ( 'debe retornar cadena vacía si la fecha de fin es inválida' , ( ) => {
150+ expect ( getDateRangeLabel ( new Date ( ) , 'invalido' ) ) . toBe ( '' ) ;
151+ } ) ;
152+
153+ it ( 'debe aceptar timestamps numéricos' , ( ) => {
154+ const result = getDateRangeLabel (
155+ Date . UTC ( 2024 , 0 , 1 , 12 ) ,
156+ Date . UTC ( 2024 , 11 , 31 , 12 ) ,
157+ ) ;
158+ expect ( result ) . toContain ( '2024' ) ;
159+ } ) ;
160+ } ) ;
161+
4162 describe ( 'formatJoinDate' , ( ) => {
5- it ( 'should format date in Spanish month format ' , ( ) => {
6- // Use noon time to avoid timezone issues
163+ it ( 'debe formatear una fecha en formato de mes abreviado en español ' , ( ) => {
164+ // Se usa hora al mediodía UTC para evitar problemas de zona horaria
7165 const result = formatJoinDate ( '2024-01-15T12:00:00Z' ) ;
8166 expect ( result ) . toMatch ( / E n e \d + , 2 0 2 4 / ) ;
9167 } ) ;
10168
11- it ( 'should format February correctly ' , ( ) => {
169+ it ( 'debe formatear febrero correctamente ' , ( ) => {
12170 const result = formatJoinDate ( '2024-02-20T12:00:00Z' ) ;
13171 expect ( result ) . toMatch ( / F e b \d + , 2 0 2 4 / ) ;
14172 } ) ;
15173
16- it ( 'should format December correctly ' , ( ) => {
174+ it ( 'debe formatear diciembre correctamente ' , ( ) => {
17175 const result = formatJoinDate ( '2024-12-25T12:00:00Z' ) ;
18176 expect ( result ) . toMatch ( / D i c \d + , 2 0 2 4 / ) ;
19177 } ) ;
20178
21- it ( 'should handle different years ' , ( ) => {
179+ it ( 'debe manejar diferentes años ' , ( ) => {
22180 const result = formatJoinDate ( '2023-06-15T12:00:00Z' ) ;
23181 expect ( result ) . toMatch ( / J u n \d + , 2 0 2 3 / ) ;
24182 } ) ;
25183
26- it ( 'should return month abbreviations in Spanish' , ( ) => {
27- // Just verify the function returns a properly formatted string
184+ it ( 'debe retornar cadena en formato mes_abreviado día, año' , ( ) => {
28185 const result = formatJoinDate ( '2024-05-15T12:00:00Z' ) ;
29186 expect ( result ) . toMatch ( / \w { 3 } \d + , \d { 4 } / ) ;
30187 } ) ;
31188
32- it ( 'should handle all month abbreviations ' , ( ) => {
189+ it ( 'debe manejar todas las abreviaciones de meses en español ' , ( ) => {
33190 const monthMap : Record < number , string > = {
34191 0 : 'Ene' ,
35192 1 : 'Feb' ,
@@ -45,12 +202,9 @@ describe('date.utils', () => {
45202 11 : 'Dic' ,
46203 } ;
47204
48- // Test each month using local date to avoid timezone issues
49205 for ( let month = 0 ; month < 12 ; month ++ ) {
50206 const date = new Date ( 2024 , month , 15 ) ;
51- const isoString = date . toISOString ( ) ;
52- const result = formatJoinDate ( isoString ) ;
53- // The function uses getMonth() which returns local month
207+ const result = formatJoinDate ( date . toISOString ( ) ) ;
54208 expect ( result ) . toContain ( monthMap [ date . getMonth ( ) ] ) ;
55209 }
56210 } ) ;
0 commit comments