const { htmlToPlainText, formatEmailDate, formatShortDate, formatCurrency, } = require('../../../../services/email/core/emailUtils'); describe('Email Utils', () => { describe('htmlToPlainText', () => { it('should remove HTML tags', () => { const html = '
Hello World
'; const result = htmlToPlainText(html); expect(result).toBe('Hello World'); }); it('should convert br tags to newlines', () => { const html = 'Line 1Paragraph 1
Paragraph 2
'; const result = htmlToPlainText(html); expect(result).toContain('Paragraph 1'); expect(result).toContain('Paragraph 2'); }); it('should convert li tags to bullet points', () => { const html = 'Content
'; const result = htmlToPlainText(html); expect(result).toBe('Content'); expect(result).not.toContain('color'); }); it('should remove script tags and their content', () => { const html = 'Content
'; const result = htmlToPlainText(html); expect(result).toBe('Content'); expect(result).not.toContain('alert'); }); it('should decode HTML entities', () => { const html = '& < > " ' '; const result = htmlToPlainText(html); expect(result).toContain('&'); expect(result).toContain('<'); expect(result).toContain('>'); expect(result).toContain('"'); expect(result).toContain("'"); }); it('should handle empty string', () => { expect(htmlToPlainText('')).toBe(''); }); it('should trim whitespace', () => { const html = 'Content
'; const result = htmlToPlainText(html); expect(result).toBe('Content'); }); it('should collapse multiple newlines', () => { const html = 'Line 1
\n\n\n\nLine 2
'; const result = htmlToPlainText(html); expect(result).not.toMatch(/\n{4,}/); }); }); describe('formatEmailDate', () => { it('should format a Date object', () => { const date = new Date('2024-03-15T14:30:00'); const result = formatEmailDate(date); expect(result).toContain('March'); expect(result).toContain('15'); expect(result).toContain('2024'); }); it('should format a date string', () => { const dateStr = '2024-06-20T10:00:00'; const result = formatEmailDate(dateStr); expect(result).toContain('June'); expect(result).toContain('20'); expect(result).toContain('2024'); }); it('should include day of week', () => { const date = new Date('2024-03-15T14:30:00'); // Friday const result = formatEmailDate(date); expect(result).toContain('Friday'); }); it('should include time', () => { const date = new Date('2024-03-15T14:30:00'); const result = formatEmailDate(date); expect(result).toMatch(/\d{1,2}:\d{2}/); }); }); describe('formatShortDate', () => { it('should format a Date object without time', () => { const date = new Date('2024-03-15T14:30:00'); const result = formatShortDate(date); expect(result).toContain('March'); expect(result).toContain('15'); expect(result).toContain('2024'); expect(result).not.toMatch(/\d{1,2}:\d{2}/); // No time }); it('should format a date string', () => { const dateStr = '2024-12-25T00:00:00'; const result = formatShortDate(dateStr); expect(result).toContain('December'); expect(result).toContain('25'); expect(result).toContain('2024'); }); }); describe('formatCurrency', () => { it('should format amount in cents to USD', () => { const result = formatCurrency(1000); expect(result).toBe('$10.00'); }); it('should handle decimal amounts', () => { const result = formatCurrency(1050); expect(result).toBe('$10.50'); }); it('should handle large amounts', () => { const result = formatCurrency(100000); expect(result).toBe('$1,000.00'); }); it('should handle zero', () => { const result = formatCurrency(0); expect(result).toBe('$0.00'); }); it('should accept currency parameter', () => { const result = formatCurrency(1000, 'EUR'); expect(result).toContain('€'); }); }); });