|
| 1 | +import { expect } from '@vaadin/chai-plugins'; |
| 2 | +import { fixtureSync, nextRender, nextUpdate } from '@vaadin/testing-helpers'; |
| 3 | +import sinon from 'sinon'; |
| 4 | +import { Tooltip } from '../src/vaadin-tooltip.js'; |
| 5 | + |
| 6 | +describe('markdown', () => { |
| 7 | + let tooltip, overlay, contentNode; |
| 8 | + |
| 9 | + before(async () => { |
| 10 | + // Preload markdown helpers to avoid dynamic import delays |
| 11 | + await Tooltip.__importMarkdownHelpers(); |
| 12 | + }); |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + tooltip = fixtureSync('<vaadin-tooltip></vaadin-tooltip>'); |
| 16 | + await nextRender(); |
| 17 | + overlay = tooltip.shadowRoot.querySelector('vaadin-tooltip-overlay'); |
| 18 | + contentNode = tooltip.querySelector('[slot="overlay"]'); |
| 19 | + }); |
| 20 | + |
| 21 | + it('should have markdown property with default value false', () => { |
| 22 | + expect(tooltip.markdown).to.equal(false); |
| 23 | + }); |
| 24 | + |
| 25 | + it('should reflect markdown property to attribute', async () => { |
| 26 | + expect(tooltip.hasAttribute('markdown')).to.be.false; |
| 27 | + |
| 28 | + tooltip.markdown = true; |
| 29 | + await nextUpdate(tooltip); |
| 30 | + expect(tooltip.hasAttribute('markdown')).to.be.true; |
| 31 | + }); |
| 32 | + |
| 33 | + describe('markdown disabled', () => { |
| 34 | + it('should not parse markdown syntax by default', async () => { |
| 35 | + tooltip.text = '**Bold text** and *italic text*'; |
| 36 | + await nextUpdate(tooltip); |
| 37 | + expect(contentNode.innerHTML).to.equal('**Bold text** and *italic text*'); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('markdown enabled', () => { |
| 42 | + beforeEach(async () => { |
| 43 | + tooltip.markdown = true; |
| 44 | + await nextUpdate(tooltip); |
| 45 | + }); |
| 46 | + |
| 47 | + it('should parse markdown syntax', async () => { |
| 48 | + tooltip.text = '**Bold text** and *italic text*'; |
| 49 | + await nextUpdate(tooltip); |
| 50 | + expect(contentNode.innerHTML.trim()).to.equal('<p><strong>Bold text</strong> and <em>italic text</em></p>'); |
| 51 | + }); |
| 52 | + |
| 53 | + it('should update content when markdown text changes', async () => { |
| 54 | + tooltip.text = '# Heading 1'; |
| 55 | + await nextUpdate(tooltip); |
| 56 | + expect(contentNode.innerHTML.trim()).to.equal('<h1>Heading 1</h1>'); |
| 57 | + |
| 58 | + tooltip.text = '## Heading 2'; |
| 59 | + await nextUpdate(tooltip); |
| 60 | + expect(contentNode.innerHTML.trim()).to.equal('<h2>Heading 2</h2>'); |
| 61 | + }); |
| 62 | + |
| 63 | + it('should handle empty/null/undefined content', async () => { |
| 64 | + tooltip.text = ''; |
| 65 | + await nextUpdate(tooltip); |
| 66 | + expect(contentNode.innerHTML).to.equal(''); |
| 67 | + |
| 68 | + tooltip.text = null; |
| 69 | + await nextUpdate(tooltip); |
| 70 | + expect(contentNode.innerHTML).to.equal(''); |
| 71 | + |
| 72 | + tooltip.text = undefined; |
| 73 | + await nextUpdate(tooltip); |
| 74 | + expect(contentNode.innerHTML).to.equal(''); |
| 75 | + }); |
| 76 | + |
| 77 | + it('should sanitize markdown', async () => { |
| 78 | + tooltip.text = '<script>alert("xss")</script>\n\n**Safe content**'; |
| 79 | + await nextUpdate(tooltip); |
| 80 | + |
| 81 | + expect(contentNode.innerHTML.trim()).to.equal('<p><strong>Safe content</strong></p>'); |
| 82 | + }); |
| 83 | + |
| 84 | + it('should hide overlay when markdown content is empty', async () => { |
| 85 | + tooltip.text = ''; |
| 86 | + await nextUpdate(tooltip); |
| 87 | + |
| 88 | + expect(overlay.hasAttribute('hidden')).to.be.true; |
| 89 | + }); |
| 90 | + |
| 91 | + it('should show overlay when markdown content is not empty', async () => { |
| 92 | + tooltip.text = '**Content**'; |
| 93 | + await nextUpdate(tooltip); |
| 94 | + |
| 95 | + expect(overlay.hasAttribute('hidden')).to.be.false; |
| 96 | + }); |
| 97 | + |
| 98 | + it('should fire content-changed event when markdown content is set', async () => { |
| 99 | + const spy = sinon.spy(); |
| 100 | + tooltip.addEventListener('content-changed', spy); |
| 101 | + |
| 102 | + tooltip.text = '**Bold text**'; |
| 103 | + await nextUpdate(tooltip); |
| 104 | + |
| 105 | + expect(spy.callCount).to.equal(1); |
| 106 | + expect(spy.firstCall.args[0].detail).to.deep.equal({ content: 'Bold text\n' }); |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + describe('switching between text and markdown', () => { |
| 111 | + it('should switch from text to markdown', async () => { |
| 112 | + tooltip.text = '**Bold text**'; |
| 113 | + await nextUpdate(tooltip); |
| 114 | + expect(contentNode.innerHTML).to.equal('**Bold text**'); |
| 115 | + |
| 116 | + tooltip.markdown = true; |
| 117 | + await nextUpdate(tooltip); |
| 118 | + expect(contentNode.innerHTML.trim()).to.equal('<p><strong>Bold text</strong></p>'); |
| 119 | + }); |
| 120 | + |
| 121 | + it('should switch from markdown to text', async () => { |
| 122 | + tooltip.markdown = true; |
| 123 | + tooltip.text = '**Bold text**'; |
| 124 | + await nextUpdate(tooltip); |
| 125 | + expect(contentNode.innerHTML.trim()).to.equal('<p><strong>Bold text</strong></p>'); |
| 126 | + |
| 127 | + tooltip.markdown = false; |
| 128 | + await nextUpdate(tooltip); |
| 129 | + expect(contentNode.innerHTML).to.equal('**Bold text**'); |
| 130 | + }); |
| 131 | + |
| 132 | + it('should fire content-changed event when switching content types', async () => { |
| 133 | + const spy = sinon.spy(); |
| 134 | + tooltip.addEventListener('content-changed', spy); |
| 135 | + |
| 136 | + tooltip.text = '**Bold text**'; |
| 137 | + await nextUpdate(tooltip); |
| 138 | + |
| 139 | + expect(spy.callCount).to.equal(1); |
| 140 | + expect(spy.firstCall.args[0].detail).to.deep.equal({ content: '**Bold text**' }); |
| 141 | + |
| 142 | + tooltip.markdown = true; |
| 143 | + await nextUpdate(tooltip); |
| 144 | + |
| 145 | + expect(spy.callCount).to.equal(2); |
| 146 | + expect(spy.secondCall.args[0].detail).to.deep.equal({ content: 'Bold text\n' }); |
| 147 | + }); |
| 148 | + }); |
| 149 | +}); |
0 commit comments