import MessageFormatter from '../MessageFormatter'; describe('#MessageFormatter', () => { describe('content with links', () => { it('should format correctly', () => { const message = 'Chatwoot is an opensource tool. [Chatwoot](https://www.chatwoot.com)'; expect(new MessageFormatter(message).formattedMessage).toMatch( '

Chatwoot is an opensource tool. Chatwoot

' ); }); it('should format correctly', () => { const message = 'Chatwoot is an opensource tool. https://www.chatwoot.com'; expect(new MessageFormatter(message).formattedMessage).toMatch( '

Chatwoot is an opensource tool. https://www.chatwoot.com

' ); }); it('should not convert template variables to links when linkify is disabled', () => { const message = 'Hey {{customer.name}}, check https://chatwoot.com'; const formatter = new MessageFormatter(message, false, false, false); expect(formatter.formattedMessage).toMatch( '

Hey {{customer.name}}, check https://chatwoot.com

' ); }); }); describe('parses heading to strong', () => { it('should format correctly', () => { const message = '### opensource \n ## tool'; expect(new MessageFormatter(message).formattedMessage).toMatch( `

opensource

tool

` ); }); it('should not render a setext heading when text is followed by "--"', () => { const message = 'hy\n\n\\\n\\-\\-\n\nHello there'; const result = new MessageFormatter(message).formattedMessage; expect(result).not.toMatch('

'); expect(result).not.toMatch('

'); }); }); describe('content with image and has "cw_image_height" query at the end of URL', () => { it('should set image height correctly', () => { const message = 'Chatwoot is an opensource tool. ![](http://chatwoot.com/chatwoot.png?cw_image_height=24px)'; expect(new MessageFormatter(message).formattedMessage).toMatch( '

Chatwoot is an opensource tool.

' ); }); it('should set image height correctly if its original size', () => { const message = 'Chatwoot is an opensource tool. ![](http://chatwoot.com/chatwoot.png?cw_image_height=auto)'; expect(new MessageFormatter(message).formattedMessage).toMatch( '

Chatwoot is an opensource tool.

' ); }); it('should not set height', () => { const message = 'Chatwoot is an opensource tool. ![](http://chatwoot.com/chatwoot.png)'; expect(new MessageFormatter(message).formattedMessage).toMatch( '

Chatwoot is an opensource tool.

' ); }); }); describe('#disableImageRendering', () => { it('omits nested and reference images with relative URLs', () => { const message = `Before ![nested [alt]](/relative.png) ![reference][logo] [logo]: /logo.png After`; const formatter = new MessageFormatter(message); formatter.disableImageRendering(); expect(formatter.formattedMessage).not.toContain(' { it('should return the same string if not tags or @mentions', () => { const message = 'Chatwoot is an opensource tool'; expect(new MessageFormatter(message).formattedMessage).toMatch(message); }); it('should add links to @mentions', () => { const message = '@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername'; expect( new MessageFormatter(message, true, false).formattedMessage ).toMatch( '

@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername

' ); }); it('should add links to #tags', () => { const message = '#chatwootapp is an opensource tool'; expect( new MessageFormatter(message, true, false).formattedMessage ).toMatch( '

#chatwootapp is an opensource tool

' ); }); }); describe('private notes', () => { it('should return the same string if not tags or @mentions', () => { const message = 'Chatwoot is an opensource tool'; expect(new MessageFormatter(message).formattedMessage).toMatch(message); }); it('should add links to @mentions', () => { const message = '@chatwootapp is an opensource tool thanks @longnonexistenttwitterusername'; expect( new MessageFormatter(message, false, true).formattedMessage ).toMatch(message); }); it('should add links to #tags', () => { const message = '#chatwootapp is an opensource tool'; expect( new MessageFormatter(message, false, true).formattedMessage ).toMatch(message); }); }); describe('plain text content', () => { it('returns the plain text without HTML', () => { const message = 'Chatwoot is an opensource tool. https://www.chatwoot.com'; expect(new MessageFormatter(message).plainText).toMatch( 'Chatwoot is an opensource tool. https://www.chatwoot.com' ); }); }); describe('help center table colwidth marker', () => { it('strips the internal colwidths marker from rendered output', () => { const message = '\n| A | B |\n| --- | --- |\n| 1 | 2 |'; const formatter = new MessageFormatter(message); expect(formatter.formattedMessage).not.toContain('cw-colwidths'); expect(formatter.plainText).not.toContain('cw-colwidths'); }); it('strips a blockquote-prefixed marker so the quoted table still renders', () => { const message = '> \n> | A | B |\n> | --- | --- |\n> | 1 | 2 |'; const { formattedMessage } = new MessageFormatter(message); expect(formattedMessage).not.toContain('cw-colwidths'); expect(formattedMessage).toContain('
'); expect(formattedMessage).toContain(''); }); }); describe('#sanitize', () => { it('sanitizes markup and removes all unnecessary elements', () => { const message = '[xssLink](javascript:alert(document.cookie))\n[normalLink](https://google.com)**I am a bold text paragraph**'; expect(new MessageFormatter(message).formattedMessage).toMatch( `

[xssLink](javascript:alert(document.cookie))
normalLinkI am a bold text paragraph

` ); }); }); });