|
| 1 | +import{applyDiffLineSelection}from'./repo-diff-selection.ts'; |
| 2 | + |
| 3 | +functioncreateDiffRow(tbody:HTMLTableSectionElement,options:{id?:string,lineType?:string}={}){ |
| 4 | +consttr=document.createElement('tr'); |
| 5 | +if(options.lineType)tr.setAttribute('data-line-type',options.lineType); |
| 6 | + |
| 7 | +constnumberCell=document.createElement('td'); |
| 8 | +numberCell.classList.add('lines-num'); |
| 9 | +constspan=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 | +returntr; |
| 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 | +constfragment='diff-selection'; |
| 26 | + |
| 27 | +constotherBox=document.createElement('div'); |
| 28 | +constotherTable=document.createElement('table'); |
| 29 | +otherTable.classList.add('code-diff'); |
| 30 | +constotherTbody=document.createElement('tbody'); |
| 31 | +conststaleActiveRow=document.createElement('tr'); |
| 32 | +staleActiveRow.classList.add('active'); |
| 33 | +otherTbody.append(staleActiveRow); |
| 34 | +otherTable.append(otherTbody); |
| 35 | +otherBox.append(otherTable); |
| 36 | + |
| 37 | +constcontainer=document.createElement('div'); |
| 38 | +container.classList.add('diff-file-box'); |
| 39 | +consttable=document.createElement('table'); |
| 40 | +table.classList.add('code-diff'); |
| 41 | +consttbody=document.createElement('tbody'); |
| 42 | +table.append(tbody); |
| 43 | +container.append(table); |
| 44 | + |
| 45 | +constrows=[ |
| 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 | +constrange={fragment,startSide:'L'asconst,startLine:1,endSide:'R'asconst,endLine:5}; |
| 57 | +constapplied=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 | +constfragment='diff-missing'; |
| 71 | +constcontainer=document.createElement('div'); |
| 72 | +container.classList.add('diff-file-box'); |
| 73 | +consttable=document.createElement('table'); |
| 74 | +table.classList.add('code-diff'); |
| 75 | +consttbody=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 | +constapplied=applyDiffLineSelection(container,{fragment,startSide:'L'asconst,startLine:1,endSide:'R'asconst,endLine:2}); |
| 83 | +expect(applied).toBe(false); |
| 84 | +expect(container.querySelectorAll('tr.active').length).toBe(0); |
| 85 | +}); |
| 86 | +}); |