Skip to content

Commit 9863ccb

Browse files
author
Alejandro Caicedo
committed
feat: add comprehensive tests for currency and date utilities; enhance format functions
1 parent 58cf4bc commit 9863ccb

5 files changed

Lines changed: 510 additions & 13 deletions

File tree

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
import {
2+
formatCurrency,
3+
formatCompactCurrency,
4+
formatNumber,
5+
} from '@modules/core/domain/utils/currency.utils';
6+
7+
describe('currency.utils', () => {
8+
describe('formatCurrency', () => {
9+
it('debe retornar una cadena no vacía para un número válido', () => {
10+
const result = formatCurrency(1000);
11+
expect(typeof result).toBe('string');
12+
expect(result.length).toBeGreaterThan(0);
13+
});
14+
15+
it('debe incluir el número en la cadena formateada', () => {
16+
const result = formatCurrency(1000);
17+
expect(result).toMatch(/1[.,]?000/);
18+
});
19+
20+
it('debe retornar cadena vacía para NaN', () => {
21+
expect(formatCurrency(NaN)).toBe('');
22+
});
23+
24+
it('debe retornar cadena vacía para Infinity', () => {
25+
expect(formatCurrency(Infinity)).toBe('');
26+
});
27+
28+
it('debe retornar cadena vacía para -Infinity', () => {
29+
expect(formatCurrency(-Infinity)).toBe('');
30+
});
31+
32+
it('debe formatear el valor cero', () => {
33+
const result = formatCurrency(0);
34+
expect(result).toContain('0');
35+
});
36+
37+
it('debe formatear valores negativos', () => {
38+
const result = formatCurrency(-500);
39+
expect(result).toContain('500');
40+
});
41+
42+
it('debe aceptar locale y moneda personalizados', () => {
43+
const result = formatCurrency(1000, { locale: 'en-US', currency: 'USD' });
44+
expect(result).toContain('1');
45+
expect(result).toContain('000');
46+
});
47+
48+
it('debe formatear con dos decimales usando locale en-US', () => {
49+
const result = formatCurrency(100, { locale: 'en-US', currency: 'USD' });
50+
expect(result).toMatch(/100\.00/);
51+
});
52+
53+
it('debe formatear valores decimales', () => {
54+
const result = formatCurrency(99.99, { locale: 'en-US', currency: 'USD' });
55+
expect(result).toContain('99.99');
56+
});
57+
});
58+
59+
describe('formatCompactCurrency', () => {
60+
it('debe formatear en notación compacta para millones', () => {
61+
const result = formatCompactCurrency(1000000);
62+
expect(typeof result).toBe('string');
63+
expect(result.length).toBeGreaterThan(0);
64+
});
65+
66+
it('debe producir una cadena más corta que el formato completo para valores grandes', () => {
67+
const full = formatCurrency(1000000);
68+
const compact = formatCompactCurrency(1000000);
69+
expect(compact.length).toBeLessThan(full.length);
70+
});
71+
72+
it('debe retornar cadena vacía para NaN', () => {
73+
expect(formatCompactCurrency(NaN)).toBe('');
74+
});
75+
76+
it('debe retornar cadena vacía para Infinity', () => {
77+
expect(formatCompactCurrency(Infinity)).toBe('');
78+
});
79+
80+
it('debe retornar cadena vacía para -Infinity', () => {
81+
expect(formatCompactCurrency(-Infinity)).toBe('');
82+
});
83+
84+
it('debe formatear valores menores a mil sin notación compacta', () => {
85+
const result = formatCompactCurrency(500);
86+
expect(result).toContain('500');
87+
});
88+
89+
it('debe aceptar opciones de locale y moneda personalizados', () => {
90+
const result = formatCompactCurrency(1000000, {
91+
locale: 'en-US',
92+
currency: 'USD',
93+
});
94+
expect(typeof result).toBe('string');
95+
expect(result.length).toBeGreaterThan(0);
96+
});
97+
});
98+
99+
describe('formatNumber', () => {
100+
it('debe formatear un número entero', () => {
101+
const result = formatNumber(1234);
102+
expect(typeof result).toBe('string');
103+
expect(result).toContain('1');
104+
});
105+
106+
it('debe retornar cadena vacía para NaN', () => {
107+
expect(formatNumber(NaN)).toBe('');
108+
});
109+
110+
it('debe retornar cadena vacía para Infinity', () => {
111+
expect(formatNumber(Infinity)).toBe('');
112+
});
113+
114+
it('debe retornar cadena vacía para -Infinity', () => {
115+
expect(formatNumber(-Infinity)).toBe('');
116+
});
117+
118+
it('debe formatear el valor cero', () => {
119+
expect(formatNumber(0)).toContain('0');
120+
});
121+
122+
it('debe formatear con separador de miles usando locale en-US', () => {
123+
const result = formatNumber(1234, {}, 'en-US');
124+
expect(result).toBe('1,234');
125+
});
126+
127+
it('debe aceptar opciones de estilo porcentaje', () => {
128+
const result = formatNumber(0.75, { style: 'percent' });
129+
expect(result).toContain('75');
130+
});
131+
132+
it('debe respetar las opciones de decimales', () => {
133+
const result = formatNumber(3.14159, {
134+
minimumFractionDigits: 2,
135+
maximumFractionDigits: 2,
136+
}, 'en-US');
137+
expect(result).toBe('3.14');
138+
});
139+
140+
it('debe formatear números negativos', () => {
141+
const result = formatNumber(-42);
142+
expect(result).toContain('42');
143+
});
144+
});
145+
});

‎__tests__/modules/core/domain/date.utils.test.ts‎

Lines changed: 167 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,192 @@
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

313
describe('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(/Ene \d+, 2024/);
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(/Feb \d+, 2024/);
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(/Dic \d+, 2024/);
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(/Jun \d+, 2023/);
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

Comments
 (0)