Created
February 11, 2026 17:03
-
-
Save claudiainbytes/3f488e156fcd300f96befd777a865084 to your computer and use it in GitHub Desktop.
3. Unit Testing. Utils functions format currency and truncate text
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // utils/formatters.ts | |
| export const formatCurrency = (amount: number, locale = 'en-US') => { | |
| return new Intl.NumberFormat(locale, { | |
| style: 'currency', | |
| currency: 'USD', | |
| }).format(amount) | |
| } | |
| export const truncateText = (text: string, maxLength: number) => { | |
| if (text.length <= maxLength) return text | |
| return text.slice(0, maxLength) + '...' | |
| } | |
| // utils/__tests__/formatters.test.ts | |
| describe('formatCurrency', () => { | |
| it('formatea números como moneda USD', () => { | |
| expect(formatCurrency(1234.56)).toBe('$1,234.56') | |
| }) | |
| it('maneja números negativos', () => { | |
| expect(formatCurrency(-50)).toBe('-$50.00') | |
| }) | |
| it('redondea a 2 decimales', () => { | |
| expect(formatCurrency(10.999)).toBe('$11.00') | |
| }) | |
| }) | |
| describe('truncateText', () => { | |
| it('no trunca texto menor al límite', () => { | |
| expect(truncateText('Hello', 10)).toBe('Hello') | |
| }) | |
| it('trunca texto y añade puntos suspensivos', () => { | |
| expect(truncateText('Hello World', 5)).toBe('Hello...') | |
| }) | |
| it('maneja string vacío', () => { | |
| expect(truncateText('', 5)).toBe('') | |
| }) | |
| }) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment