Skip to content

Commit a05280a

Browse files
committed
Adding tests
1 parent 734f949 commit a05280a

File tree

4 files changed

+257
-31
lines changed

4 files changed

+257
-31
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
"publish:examples": "NODE_ENV=production yarn build:examples && gh-pages -d examples/dist -r $GITHUB_REPO_URL",
2525
"publish:examples:local": "NODE_ENV=production yarn build:examples && gh-pages -d examples/dist",
2626
"start:examples": "webpack-dev-server --open --hot --inline",
27-
"test": "mocha --require ts-node/register --require enzyme.ts ./test/index.tsx",
28-
"test:watch": "mocha --require ts-node/register --require enzyme.ts --watch-extensions ts,tsx --watch ./test/index.tsx"
27+
"test": "mocha --require ts-node/register --require enzyme.ts ./test/**",
28+
"test:watch": "mocha --require ts-node/register --require enzyme.ts --watch-extensions ts,tsx --watch ./test/**"
2929
},
3030
"dependencies": {
3131
"classnames": "^2.2.6",

test/compute-lines-test.ts

Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
import * as expect from 'expect';
2+
import computeLines from '../src/compute-lines';
3+
4+
describe('Testing compute lines utils', (): void => {
5+
it('Should it avoid trailing spaces', (): void => {
6+
const oldCode = `test
7+
8+
9+
`;
10+
const newCode = `test
11+
12+
`;
13+
14+
expect(computeLines(oldCode, newCode))
15+
.toMatchObject({
16+
lineInformation: [
17+
{
18+
left: {
19+
lineNumber: 1,
20+
type: 0,
21+
value: 'test',
22+
},
23+
right: {
24+
lineNumber: 1,
25+
type: 0,
26+
value: 'test',
27+
},
28+
},
29+
],
30+
diffLines: [],
31+
});
32+
});
33+
34+
it('Should identify line addition', (): void => {
35+
const oldCode = 'test';
36+
const newCode = `test
37+
newLine`;
38+
39+
expect(computeLines(oldCode, newCode))
40+
.toMatchObject({
41+
lineInformation: [
42+
{
43+
right: {
44+
lineNumber: 1,
45+
type: 0,
46+
value: 'test',
47+
},
48+
left: {
49+
lineNumber: 1,
50+
type: 0,
51+
value: 'test',
52+
},
53+
},
54+
{
55+
right: {
56+
lineNumber: 2,
57+
type: 1,
58+
value: ' newLine',
59+
},
60+
left: {},
61+
},
62+
],
63+
diffLines: [1],
64+
});
65+
});
66+
67+
it('Should identify line deletion', (): void => {
68+
const oldCode = `test
69+
oldLine`;
70+
const newCode = 'test';
71+
72+
expect(computeLines(oldCode, newCode))
73+
.toMatchObject({
74+
lineInformation: [
75+
{
76+
right: {
77+
lineNumber: 1,
78+
type: 0,
79+
value: 'test',
80+
},
81+
left: {
82+
lineNumber: 1,
83+
type: 0,
84+
value: 'test',
85+
},
86+
},
87+
{
88+
right: {},
89+
left: {
90+
lineNumber: 2,
91+
type: 2,
92+
value: ' oldLine',
93+
},
94+
},
95+
],
96+
diffLines: [1],
97+
});
98+
});
99+
100+
it('Should identify line modification', (): void => {
101+
const oldCode = `test
102+
oldLine`;
103+
const newCode = `test
104+
newLine`;
105+
106+
expect(computeLines(oldCode, newCode, true))
107+
.toMatchObject({
108+
lineInformation: [
109+
{
110+
right: {
111+
lineNumber: 1,
112+
type: 0,
113+
value: 'test',
114+
},
115+
left: {
116+
lineNumber: 1,
117+
type: 0,
118+
value: 'test',
119+
},
120+
},
121+
{
122+
right: {
123+
lineNumber: 2,
124+
type: 1,
125+
value: ' newLine',
126+
},
127+
left: {
128+
lineNumber: 2,
129+
type: 2,
130+
value: ' oldLine',
131+
},
132+
},
133+
],
134+
diffLines: [1],
135+
});
136+
});
137+
138+
it('Should identify word diff', (): void => {
139+
const oldCode = `test
140+
oldLine`;
141+
const newCode = `test
142+
newLine`;
143+
144+
expect(computeLines(oldCode, newCode))
145+
.toMatchObject({
146+
lineInformation: [
147+
{
148+
right: {
149+
lineNumber: 1,
150+
type: 0,
151+
value: 'test',
152+
},
153+
left: {
154+
lineNumber: 1,
155+
type: 0,
156+
value: 'test',
157+
},
158+
},
159+
{
160+
right: {
161+
lineNumber: 2,
162+
type: 1,
163+
value: [
164+
{
165+
type: 0,
166+
value: ' ',
167+
},
168+
{
169+
type: 1,
170+
value: 'new',
171+
},
172+
{
173+
type: 0,
174+
value: 'Line',
175+
},
176+
],
177+
},
178+
left: {
179+
lineNumber: 2,
180+
type: 2,
181+
value: [
182+
{
183+
type: 0,
184+
value: ' ',
185+
},
186+
{
187+
type: 2,
188+
value: 'old',
189+
},
190+
{
191+
type: 0,
192+
value: 'Line',
193+
},
194+
],
195+
},
196+
},
197+
],
198+
diffLines: [1],
199+
});
200+
});
201+
});

test/index.tsx

Lines changed: 0 additions & 29 deletions
This file was deleted.

test/react-diff-viewer-test.tsx

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { shallow } from 'enzyme';
2+
import * as React from 'react';
3+
import * as expect from 'expect';
4+
5+
import DiffViewer from '../lib/index';
6+
7+
const oldCode = `
8+
const a = 123
9+
const b = 456
10+
const c = 4556
11+
const d = 4566
12+
const e = () => {
13+
console.log('c')
14+
}
15+
`;
16+
17+
const newCode = `
18+
const a = 123
19+
const b = 456
20+
const c = 4556
21+
const d = 4566
22+
const aa = 123
23+
const bb = 456
24+
`;
25+
26+
describe('Testing react diff viewer', (): void => {
27+
it('It should render a table', (): void => {
28+
const node = shallow(<DiffViewer
29+
oldValue={oldCode}
30+
newValue={newCode}
31+
/>);
32+
33+
expect(node.find('table').length).toEqual(1);
34+
});
35+
36+
it('It should render diff lines in diff view', (): void => {
37+
const node = shallow(<DiffViewer
38+
oldValue={oldCode}
39+
newValue={newCode}
40+
/>);
41+
42+
expect(node.find('table > tbody tr').length).toEqual(6);
43+
});
44+
45+
it('It should render diff lines in inline view', (): void => {
46+
const node = shallow(<DiffViewer
47+
oldValue={oldCode}
48+
newValue={newCode}
49+
splitView={false}
50+
/>);
51+
52+
expect(node.find('table > tbody tr').length).toEqual(9);
53+
});
54+
});

0 commit comments

Comments
 (0)