Skip to content

Commit cc1cb08

Browse files
committed
Merge pull request #54 from bem/tests
Tests
2 parents 091b114 + ca236d3 commit cc1cb08

5 files changed

Lines changed: 240 additions & 1 deletion

File tree

test/diffHtml.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var fs = require('fs'),
2+
HtmlDiffer = require('../lib/index').HtmlDiffer;
3+
4+
function readFiles(f1, f2) {
5+
var files = {};
6+
7+
files.html1 = fs.readFileSync('test/fixtures/' + f1, 'utf-8');
8+
files.html2 = fs.readFileSync('test/fixtures/' + f2, 'utf-8');
9+
10+
return files;
11+
}
12+
13+
describe('\'diffHtml\'', function () {
14+
15+
it('must not be diffs', function () {
16+
var htmlDiffer = new HtmlDiffer(),
17+
18+
files = readFiles('1.html', '_1.html'),
19+
20+
res = [ {
21+
value: '<!DOCTYPE html>\n<html>\n<head lang="en">\n <meta charset="UTF-8">\n <title></title>\n</head>\n<body>\nText\n</body>\n</html>\n'
22+
} ];
23+
24+
htmlDiffer.diffHtml(files.html1, files.html2).must.be.eql(res);
25+
});
26+
27+
it('must be diffs', function () {
28+
var htmlDiffer = new HtmlDiffer(),
29+
30+
files = readFiles('2.html', '_2.html'),
31+
32+
res = [ {
33+
value: '<!DOCTYPE html><html><head lang="en"><meta charset="UTF-8"><title></title></head><body>\n',
34+
added: undefined,
35+
removed: undefined
36+
}, {
37+
value: '!',
38+
added: true,
39+
removed: undefined
40+
}, {
41+
value: 'Text',
42+
added: undefined,
43+
removed: undefined
44+
}, {
45+
value: '!',
46+
added: true,
47+
removed: undefined
48+
}, {
49+
value: '\n</body></html>',
50+
added: undefined,
51+
removed: undefined
52+
} ];
53+
54+
htmlDiffer.diffHtml(files.html1, files.html2).must.be.eql(res);
55+
});
56+
57+
it('must set options', function () {
58+
var htmlDiffer = new HtmlDiffer({ ignoreHtmlAttrs: ['id', 'for'] }),
59+
60+
files = readFiles('3.html', '_3.html'),
61+
62+
res = [ {
63+
value: '<html><head><title>Test</title></head><body><label for="">label for input</label><input id=""></body></html>',
64+
added: undefined,
65+
removed: undefined
66+
} ];
67+
68+
htmlDiffer.diffHtml(files.html1, files.html2).must.be.eql(res);
69+
});
70+
71+
});

test/fixtures/9.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<label for="random">label for input</label>
9+
<input id="random">
10+
<div onclick="return {&quot;a&quot;:{&quot;b&quot;:&quot;bb&quot;,&quot;a&quot;:&quot;function() { return true; }&quot;}}" ondblclick="return {&quot;a&quot;:{&quot;b&quot;:&quot;bb&quot;,&quot;a&quot;:&quot;function() { return true; }&quot;}}"></div>
11+
<div data-bem="{&quot;a&quot;:{&quot;a&quot;:{&quot;x&quot;:&quot;xx&quot;,&quot;y&quot;:&quot;yy&quot;},&quot;b&quot;:&quot;bb&quot;,&quot;c&quot;:&quot;cc&quot;},&quot;b&quot;:&quot;bb&quot;}">
12+
</body>
13+
</html>

test/fixtures/_9.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head lang="en">
4+
<meta charset="UTF-8">
5+
<title></title>
6+
</head>
7+
<body>
8+
<label for="sfsdfksdf">label for input</label>
9+
<input id="sfsdfksdf">
10+
<div onclick="return {&quot;a&quot;:{&quot;b&quot;:&quot;bb&quot;,&quot;a&quot;:&quot;function() { return true; }&quot;}}" ondblclick="return {&quot;a&quot;:{&quot;b&quot;:&quot;bb&quot;,&quot;a&quot;:&quot;function() { return true; }&quot;}}"></div>
11+
<div data-bem="{&quot;b&quot;:&quot;bb&quot;,&quot;a&quot;:{&quot;c&quot;:&quot;cc&quot;,&quot;b&quot;:&quot;bb&quot;,&quot;a&quot;:{&quot;y&quot;:&quot;yy&quot;,&quot;x&quot;:&quot;xx&quot;}}}">
12+
</body>
13+
</html>

test/getDiffText.js

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
var diffLoger = require('../lib/diff-logger');
2+
3+
require('colors');
4+
5+
describe('\'diffHtml\'', function () {
6+
7+
it('must return an empty string', function () {
8+
9+
var inp = [ {
10+
value: 'text',
11+
added: undefined,
12+
removed: undefined
13+
} ];
14+
15+
diffLoger.getDiffText(inp).must.be.equal('');
16+
});
17+
18+
it('must return a diff string', function () {
19+
20+
var inp = [ {
21+
value: 'texttexttexttexttexttexttexttexttexttexttext',
22+
added: undefined,
23+
removed: undefined
24+
}, {
25+
value: '!',
26+
added: true,
27+
removed: undefined
28+
}, {
29+
value: 'Text',
30+
added: undefined,
31+
removed: undefined
32+
}, {
33+
value: '!',
34+
added: true,
35+
removed: undefined
36+
}, {
37+
value: 'texttexttexttext',
38+
added: undefined,
39+
removed: undefined
40+
} ],
41+
42+
out = '\n...\n' + 'texttexttexttexttext'.grey + '!'.green + 'Text'.grey + '!'.green + 'texttexttexttext'.grey;
43+
44+
diffLoger.getDiffText(inp).must.be.eql(out);
45+
});
46+
47+
it('must consider negative value of \'charsAroundDiff\' option', function () {
48+
var inp = [ {
49+
value: 'text',
50+
added: undefined,
51+
removed: undefined
52+
} ];
53+
54+
diffLoger.getDiffText(inp, { charsAroundDiff: -5 } ).must.be.equal('');
55+
});
56+
57+
58+
59+
it('must return a diff when there is nothing else in the input', function () {
60+
61+
var inp = [ {
62+
value: 'texttexttexttexttexttexttexttexttexttexttext',
63+
added: true,
64+
removed: undefined
65+
}, {
66+
value: 'ololoololoololoololoololoololoololoololoolol',
67+
added: false,
68+
removed: true
69+
} ],
70+
71+
out = '\n' + 'texttexttexttexttexttexttexttexttexttexttext'.green + 'ololoololoololoololoololoololoololoololoolol'.red;
72+
73+
diffLoger.getDiffText(inp).must.be.eql(out);
74+
});
75+
76+
it('must return a diff on the beginning of the input', function () {
77+
78+
var inp = [ {
79+
value: 'texttexttext',
80+
added: undefined,
81+
removed: undefined
82+
}, {
83+
value: 'text',
84+
added: undefined,
85+
removed: true
86+
}, {
87+
value: 'texttexttexttext',
88+
added: undefined,
89+
removed: undefined
90+
} ],
91+
92+
out = '\n' + 'texttexttext'.grey + 'text'.red + 'texttexttexttext'.grey;
93+
94+
diffLoger.getDiffText(inp).must.be.eql(out);
95+
});
96+
97+
it('must return several diffs', function () {
98+
99+
var inp = [ {
100+
value: 'texttexttexttexttexttexttexttexttexttexttext',
101+
added: undefined,
102+
removed: undefined
103+
}, {
104+
value: 'text',
105+
added: undefined,
106+
removed: true
107+
}, {
108+
value: 'texttexttexttexttexttexttexttexttexttexttext',
109+
added: undefined,
110+
removed: undefined
111+
}, {
112+
value: '!',
113+
added: true,
114+
removed: false
115+
}, {
116+
value: 'text',
117+
added: undefined,
118+
removed: undefined
119+
}, {
120+
value: '!',
121+
added: true,
122+
removed: false
123+
}, {
124+
value: 'texttexttexttexttext',
125+
added: undefined,
126+
removed: undefined
127+
} ],
128+
129+
out = '\n...\n' + 'texttexttexttexttext'.grey + 'text'.red + 'texttexttexttexttext'.grey + '\n...\n' + 'texttexttexttexttext'.grey + '!'.green + 'text'.grey + '!'.green + 'texttexttexttexttext'.grey;
130+
131+
diffLoger.getDiffText(inp).must.be.eql(out);
132+
});
133+
134+
});

test/isEqual.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('\'isEqual\'', function () {
6161
});
6262

6363
it('must ignore space characters', function () {
64-
var htmlDiffer = new HtmlDiffer(),
64+
var htmlDiffer = new HtmlDiffer({ ignoreWhitespaces: true }),
6565

6666
files = readFiles('7.html', '_7.html');
6767

@@ -76,4 +76,12 @@ describe('\'isEqual\'', function () {
7676
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
7777
});
7878

79+
it('must work \'bem\' option', function() {
80+
var htmlDiffer = new HtmlDiffer({ bem: true }),
81+
82+
files = readFiles('9.html', '_9.html');
83+
84+
htmlDiffer.isEqual(files.html1, files.html2).must.be.true();
85+
});
86+
7987
});

0 commit comments

Comments
 (0)