Skip to content

Commit 2abc925

Browse files
committed
add tests
1 parent 6e8ed9a commit 2abc925

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-4
lines changed

src/compute-lines.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ const computeLineInformation = (
140140
newString: string,
141141
disableWordDiff: boolean = false,
142142
compareMethod: string = DiffMethod.CHARS,
143-
linesOffset: number
143+
linesOffset: number = 0,
144144
): ComputedLineInformation => {
145145
const diffArray = diff.diffLines(
146146
oldString.trimRight(),

src/index.tsx

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
8585
extraLinesSurroundingDiff: 3,
8686
showDiffOnly: true,
8787
useDarkTheme: false,
88-
linesOffset: 0
88+
linesOffset: 0,
8989
};
9090

9191
public static propTypes = {
@@ -109,7 +109,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
109109
PropTypes.string,
110110
PropTypes.element,
111111
]),
112-
linesOffset: PropTypes.number
112+
linesOffset: PropTypes.number,
113113
};
114114

115115
public constructor(props: ReactDiffViewerProps) {
@@ -451,7 +451,7 @@ class DiffViewer extends React.Component<ReactDiffViewerProps, ReactDiffViewerSt
451451
newValue,
452452
disableWordDiff,
453453
compareMethod,
454-
linesOffset
454+
linesOffset,
455455
);
456456
const extraLines = this.props.extraLinesSurroundingDiff < 0
457457
? 0

test/compute-lines-test.ts

+36
Original file line numberDiff line numberDiff line change
@@ -374,4 +374,40 @@ Also this info`;
374374
],
375375
});
376376
});
377+
378+
it('Should start line counting from offset', (): void => {
379+
const oldCode = 'Hello World';
380+
const newCode = `My Updated Name
381+
Also this info`;
382+
383+
expect(computeLineInformation(oldCode, newCode, true, DiffMethod.WORDS, 5))
384+
.toMatchObject({
385+
lineInformation: [
386+
{
387+
right: {
388+
lineNumber: 6,
389+
type: 1,
390+
value: 'My Updated Name',
391+
},
392+
left: {
393+
lineNumber: 6,
394+
type: 2,
395+
value: 'Hello World',
396+
},
397+
},
398+
{
399+
right: {
400+
lineNumber: 7,
401+
type: 1,
402+
value: 'Also this info',
403+
},
404+
left: {},
405+
},
406+
],
407+
diffLines: [
408+
0,
409+
2,
410+
],
411+
});
412+
});
377413
});

0 commit comments

Comments
 (0)