Files
rentall-app/backend/tests/unit/services/email/emailUtils.test.js
jackiettran 3f319bfdd0 unit tests
2025-12-12 16:27:56 -05:00

153 lines
4.8 KiB
JavaScript

const {
htmlToPlainText,
formatEmailDate,
formatShortDate,
formatCurrency,
} = require('../../../../services/email/core/emailUtils');
describe('Email Utils', () => {
describe('htmlToPlainText', () => {
it('should remove HTML tags', () => {
const html = '<p>Hello <strong>World</strong></p>';
const result = htmlToPlainText(html);
expect(result).toBe('Hello World');
});
it('should convert br tags to newlines', () => {
const html = 'Line 1<br>Line 2<br/>Line 3';
const result = htmlToPlainText(html);
expect(result).toBe('Line 1\nLine 2\nLine 3');
});
it('should convert p tags to double newlines', () => {
const html = '<p>Paragraph 1</p><p>Paragraph 2</p>';
const result = htmlToPlainText(html);
expect(result).toContain('Paragraph 1');
expect(result).toContain('Paragraph 2');
});
it('should convert li tags to bullet points', () => {
const html = '<ul><li>Item 1</li><li>Item 2</li></ul>';
const result = htmlToPlainText(html);
expect(result).toContain('• Item 1');
expect(result).toContain('• Item 2');
});
it('should remove style tags and their content', () => {
const html = '<style>.class { color: red; }</style><p>Content</p>';
const result = htmlToPlainText(html);
expect(result).toBe('Content');
expect(result).not.toContain('color');
});
it('should remove script tags and their content', () => {
const html = '<script>alert("test")</script><p>Content</p>';
const result = htmlToPlainText(html);
expect(result).toBe('Content');
expect(result).not.toContain('alert');
});
it('should decode HTML entities', () => {
const html = '&amp; &lt; &gt; &quot; &#39; &nbsp;';
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 = ' <p>Content</p> ';
const result = htmlToPlainText(html);
expect(result).toBe('Content');
});
it('should collapse multiple newlines', () => {
const html = '<p>Line 1</p>\n\n\n\n<p>Line 2</p>';
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('€');
});
});
});