|
| 1 | +import {expect} from '@jest/globals'; |
| 2 | +import {parseRangesToHTMLNodes} from '../web/utils/parserUtils'; |
| 3 | +import parseExpensiMark from '../parseExpensiMark'; |
| 4 | + |
| 5 | +/** |
| 6 | + * Focused tests for the single-line input fix |
| 7 | + * These tests validate the specific fix for unwanted space insertion |
| 8 | + */ |
| 9 | + |
| 10 | +describe('Single-line input fix validation', () => { |
| 11 | + describe('parseRangesToHTMLNodes with isMultiline parameter', () => { |
| 12 | + it('should not generate BR elements for single-line text (isMultiline=false)', () => { |
| 13 | + const text = 'simple text'; |
| 14 | + const ranges = parseExpensiMark(text); |
| 15 | + |
| 16 | + const result = parseRangesToHTMLNodes(text, ranges, false, {}, true); |
| 17 | + |
| 18 | + // Should not contain BR elements for single-line input |
| 19 | + expect(result.dom.innerHTML).not.toContain('<span data-type="br">'); |
| 20 | + expect(result.dom.innerHTML).not.toContain('<br>'); |
| 21 | + }); |
| 22 | + |
| 23 | + it('should generate proper structure for multiline text (isMultiline=true)', () => { |
| 24 | + const text = 'line 1\nline 2'; |
| 25 | + const ranges = parseExpensiMark(text); |
| 26 | + |
| 27 | + const result = parseRangesToHTMLNodes(text, ranges, true, {}, true); |
| 28 | + |
| 29 | + // Should contain proper paragraph structure for multiline |
| 30 | + expect(result.dom.innerHTML).toContain('<p data-type="line"'); |
| 31 | + // Note: The library may not always use BR elements depending on the implementation |
| 32 | + // The key is that it handles multiline properly |
| 33 | + expect(result.dom.innerHTML.split('<p data-type="line"').length - 1).toBeGreaterThan(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should not generate BR elements for single-line markdown (isMultiline=false)', () => { |
| 37 | + const text = 'hello *world* test'; |
| 38 | + const ranges = parseExpensiMark(text); |
| 39 | + |
| 40 | + const result = parseRangesToHTMLNodes(text, ranges, false, {}, true); |
| 41 | + |
| 42 | + // Should not contain BR elements but should have markdown formatting |
| 43 | + expect(result.dom.innerHTML).not.toContain('<span data-type="br">'); |
| 44 | + expect(result.dom.innerHTML).not.toContain('<br>'); |
| 45 | + expect(result.dom.innerHTML).toContain('data-type="bold"'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should handle empty single-line input without BR elements (isMultiline=false)', () => { |
| 49 | + const text = ''; |
| 50 | + const ranges = parseExpensiMark(text); |
| 51 | + |
| 52 | + const result = parseRangesToHTMLNodes(text, ranges, false, {}, true); |
| 53 | + |
| 54 | + // For empty input, should either be empty or have minimal structure without BR |
| 55 | + const html = result.dom.innerHTML; |
| 56 | + if (html !== '') { |
| 57 | + // If there's structure, it shouldn't have BR elements for single-line |
| 58 | + expect(html).not.toContain('<span data-type="br">'); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('should preserve existing multiline behavior when isMultiline=true', () => { |
| 63 | + const singleLineText = 'test text'; |
| 64 | + const ranges = parseExpensiMark(singleLineText); |
| 65 | + |
| 66 | + // Compare single-line vs multiline behavior |
| 67 | + const singleLineResult = parseRangesToHTMLNodes(singleLineText, ranges, false, {}, true); |
| 68 | + const multilineResult = parseRangesToHTMLNodes(singleLineText, ranges, true, {}, true); |
| 69 | + |
| 70 | + // Single-line should be more compact than multiline |
| 71 | + expect(singleLineResult.dom.innerHTML.length).toBeLessThanOrEqual(multilineResult.dom.innerHTML.length); |
| 72 | + |
| 73 | + // Single-line should not have BR elements |
| 74 | + expect(singleLineResult.dom.innerHTML).not.toContain('<span data-type="br">'); |
| 75 | + }); |
| 76 | + }); |
| 77 | + |
| 78 | + describe('Integration test for the original bug scenario', () => { |
| 79 | + it('should demonstrate the fix for typing after selecting all text', () => { |
| 80 | + // This test validates the core fix: when user types "t" after selecting all, |
| 81 | + // the generated DOM should not contain elements that would cause space insertion |
| 82 | + |
| 83 | + const userTypedText = 't'; |
| 84 | + const ranges = parseExpensiMark(userTypedText); |
| 85 | + |
| 86 | + // Generate DOM with single-line setting (the fix) |
| 87 | + const result = parseRangesToHTMLNodes(userTypedText, ranges, false, {}, true); |
| 88 | + |
| 89 | + // Critical assertions for the fix: |
| 90 | + // 1. Should not contain BR elements that add newlines |
| 91 | + expect(result.dom.innerHTML).not.toContain('<span data-type="br">'); |
| 92 | + expect(result.dom.innerHTML).not.toContain('<br>'); |
| 93 | + |
| 94 | + // 2. Should contain the text properly structured |
| 95 | + expect(result.dom.innerHTML).toContain('t'); |
| 96 | + |
| 97 | + // 3. The DOM should be as minimal as possible for single character |
| 98 | + expect(result.dom.innerHTML.length).toBeLessThan(200); // Reasonable upper bound |
| 99 | + }); |
| 100 | + |
| 101 | + it('should work correctly with markdown in single-line context', () => { |
| 102 | + // Test that our fix doesn't break markdown functionality |
| 103 | + const markdownText = '*bold* normal `code`'; |
| 104 | + const ranges = parseExpensiMark(markdownText); |
| 105 | + |
| 106 | + const result = parseRangesToHTMLNodes(markdownText, ranges, false, {}, true); |
| 107 | + |
| 108 | + // Should have proper markdown elements |
| 109 | + expect(result.dom.innerHTML).toContain('data-type="bold"'); |
| 110 | + expect(result.dom.innerHTML).toContain('data-type="code"'); |
| 111 | + |
| 112 | + // But no BR elements |
| 113 | + expect(result.dom.innerHTML).not.toContain('<span data-type="br">'); |
| 114 | + expect(result.dom.innerHTML).not.toContain('<br>'); |
| 115 | + }); |
| 116 | + }); |
| 117 | +}); |
0 commit comments