Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4f65ae3

Browse files
committed
Add unit test for applyDiffLineSelection
1 parentcf809d3 commit4f65ae3

File tree

2 files changed

+87
-1
lines changed

2 files changed

+87
-1
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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+
});

‎web_src/js/features/repo-diff-selection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function parseDiffAnchor(anchor: string): DiffAnchorInfo | null {
3636
return{anchor, fragment, side, line};
3737
}
3838

39-
functionapplyDiffLineSelection(container:HTMLElement,range:DiffSelectionRange):boolean{
39+
exportfunctionapplyDiffLineSelection(container:HTMLElement,range:DiffSelectionRange):boolean{
4040
// Find the start and end anchor elements
4141
conststartId=`${range.fragment}${range.startSide}${range.startLine}`;
4242
constendId=`${range.fragment}${range.endSide}${range.endLine}`;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp