Skip to content

Open-source live-chat, email support, omni-channel desk. An alternative to Intercom, Zendesk, Salesforce Service Cloud etc. 🔥💬

2.5 KB
Raw Blame
import {
  buildForwardedEmailHtml,
  textToHtml,
} from 'dashboard/helper/forwardEmailHelper';

const labels = {
  title: '---------- Forwarded message ---------',
  from: 'From',
  date: 'Date',
  subject: 'Subject',
  to: 'To',
};

describe('forwardEmailHelper', () => {
  describe('textToHtml', () => {
    it('escapes html and converts line breaks', () => {
      expect(textToHtml('Hi <b>there</b>\nBye & thanks')).toBe(
        'Hi &lt;b&gt;there&lt;/b&gt;<br>Bye &amp; thanks'
      );
    });
  });

  describe('buildForwardedEmailHtml', () => {
    const build = overrides =>
      buildForwardedEmailHtml({
        labels,
        sender: { name: 'Jane Doe', email: '[email protected]' },
        date: 'Thu, Sep 17, 2026 at 9:39 AM',
        subject: 'Order #42',
        recipients: ['[email protected]'],
        bodyHtml: '<p>Original body</p>',
        ...overrides,
      });

    it('renders the gmail style header followed by the body', () => {
      expect(build()).toBe(
        [
          '<div class="chatwoot_forward_header">',
          '---------- Forwarded message ---------<br>',
          'From: <b>Jane Doe</b> &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;<br>',
          'Date: Thu, Sep 17, 2026 at 9:39 AM<br>',
          'Subject: Order #42<br>',
          'To: &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;',
          '</div><br><p>Original body</p>',
        ].join('')
      );
    });

    it('omits the bold name when the sender has none', () => {
      expect(build({ sender: { email: '[email protected]' } })).toContain(
        'From: &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;<br>'
      );
    });

    it('lists every recipient', () => {
      expect(
        build({ recipients: ['[email protected]', '[email protected]'] })
      ).toContain(
        'To: &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;, &lt;<a href="mailto:[email protected]">[email protected]</a>&gt;'
      );
    });

    it('escapes header values but keeps the body html intact', () => {
      const html = build({
        sender: { name: '<script>x</script>', email: 'a"[email protected]' },
        subject: 'Re: <img src=x onerror=alert(1)>',
        bodyHtml: '<p>Keep <b>this</b></p>',
      });

      expect(html).toContain('<b>&lt;script&gt;x&lt;/script&gt;</b>');
      expect(html).toContain('mailto:a&quot;[email protected]');
      expect(html).toContain('Subject: Re: &lt;img src=x onerror=alert(1)&gt;');
      expect(html).toContain('<p>Keep <b>this</b></p>');
      expect(html).not.toContain('<script>');
    });
  });
});