|
| 1 | +import {applyDiffLineSelection} from './repo-diff-selection.ts'; |
| 2 | + |
| 3 | +function createDiffRow(tbody: HTMLTableSectionElement, options: {id?: string, lineType?: string} = {}) { |
| 4 | + const tr = document.createElement('tr'); |
| 5 | + if (options.lineType) tr.setAttribute('data-line-type', options.lineType); |
| 6 | + |
| 7 | + const numberCell = document.createElement('td'); |
| 8 | + numberCell.classList.add('lines-num'); |
| 9 | + const span = document.createElement('span'); |
| 10 | + if (options.id) span.id = options.id; |
| 11 | + numberCell.append(span); |
| 12 | + tr.append(numberCell); |
| 13 | + |
| 14 | + tr.append(document.createElement('td')); |
| 15 | + tbody.append(tr); |
| 16 | + return tr; |
| 17 | +} |
| 18 | + |
| 19 | +describe('applyDiffLineSelection', () => { |
| 20 | + beforeEach(() => { |
| 21 | + document.body.innerHTML = ''; |
| 22 | + }); |
| 23 | + |
| 24 | + test('selects contiguous diff rows, skips expansion rows, and clears previous selection', () => { |
| 25 | + const fragment = 'diff-selection'; |
| 26 | + |
| 27 | + const otherBox = document.createElement('div'); |
| 28 | + const otherTable = document.createElement('table'); |
| 29 | + otherTable.classList.add('code-diff'); |
| 30 | + const otherTbody = document.createElement('tbody'); |
| 31 | + const staleActiveRow = document.createElement('tr'); |
| 32 | + staleActiveRow.classList.add('active'); |
| 33 | + otherTbody.append(staleActiveRow); |
| 34 | + otherTable.append(otherTbody); |
| 35 | + otherBox.append(otherTable); |
| 36 | + |
| 37 | + const container = document.createElement('div'); |
| 38 | + container.classList.add('diff-file-box'); |
| 39 | + const table = document.createElement('table'); |
| 40 | + table.classList.add('code-diff'); |
| 41 | + const tbody = document.createElement('tbody'); |
| 42 | + table.append(tbody); |
| 43 | + container.append(table); |
| 44 | + |
| 45 | + const rows = [ |
| 46 | + createDiffRow(tbody, {id: `${fragment}L1`}), |
| 47 | + createDiffRow(tbody), |
| 48 | + createDiffRow(tbody, {lineType: '4'}), |
| 49 | + createDiffRow(tbody), |
| 50 | + createDiffRow(tbody, {id: `${fragment}R5`}), |
| 51 | + createDiffRow(tbody), |
| 52 | + ]; |
| 53 | + |
| 54 | + document.body.append(otherBox, container); |
| 55 | + |
| 56 | + const range = {fragment, startSide: 'L' as const, startLine: 1, endSide: 'R' as const, endLine: 5}; |
| 57 | + const applied = applyDiffLineSelection(container, range); |
| 58 | + |
| 59 | + expect(applied).toBe(true); |
| 60 | + expect(rows[0].classList.contains('active')).toBe(true); |
| 61 | + expect(rows[1].classList.contains('active')).toBe(true); |
| 62 | + expect(rows[2].classList.contains('active')).toBe(false); |
| 63 | + expect(rows[3].classList.contains('active')).toBe(true); |
| 64 | + expect(rows[4].classList.contains('active')).toBe(true); |
| 65 | + expect(rows[5].classList.contains('active')).toBe(false); |
| 66 | + expect(staleActiveRow.classList.contains('active')).toBe(false); |
| 67 | + }); |
| 68 | + |
| 69 | + test('returns false when either anchor is missing', () => { |
| 70 | + const fragment = 'diff-missing'; |
| 71 | + const container = document.createElement('div'); |
| 72 | + container.classList.add('diff-file-box'); |
| 73 | + const table = document.createElement('table'); |
| 74 | + table.classList.add('code-diff'); |
| 75 | + const tbody = document.createElement('tbody'); |
| 76 | + table.append(tbody); |
| 77 | + container.append(table); |
| 78 | + document.body.append(container); |
| 79 | + |
| 80 | + createDiffRow(tbody, {id: `${fragment}L1`}); |
| 81 | + |
| 82 | + const applied = applyDiffLineSelection(container, {fragment, startSide: 'L' as const, startLine: 1, endSide: 'R' as const, endLine: 2}); |
| 83 | + expect(applied).toBe(false); |
| 84 | + expect(container.querySelectorAll('tr.active').length).toBe(0); |
| 85 | + }); |
| 86 | +}); |
0 commit comments