Skip to content

Commit 3b23df1

Browse files
committed
added:
* tests * .js testfiles with .txt ending * .html testfiles updated var rewriting | options - cb
1 parent b037b53 commit 3b23df1

File tree

6 files changed

+94
-19
lines changed

6 files changed

+94
-19
lines changed

lib/utils/options/fileReplace.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,12 @@ fileReplace.replace = (filepath, options, cb) => {
3434
})
3535
};
3636

37+
// set cb if options are not set
38+
if (typeof cb !== 'function') {
39+
cb = options;
40+
options = {};
41+
}
42+
3743
// file does not exist | bail
3844
if (!fs.existsSync(filepath)) {
3945
cb ({
@@ -42,12 +48,6 @@ fileReplace.replace = (filepath, options, cb) => {
4248
});
4349
}
4450

45-
// set cb if options are not set
46-
if (typeof cb !== 'function') {
47-
cb = options;
48-
options = {};
49-
}
50-
5151
options = _.merge(optionsDefault, options);
5252

5353
fs.readFile(filepath, 'utf8', (err, data) => {
@@ -81,6 +81,12 @@ fileReplace.replaceCss = (filepath, options, cb) => {
8181
// matches from . or # every character until : or . or ) (for pseudo elements or dots or even in :not(.class))
8282
let regex = /(#|\.)[^\s:\.\{)]+/g;
8383

84+
// set cb if options are not set
85+
if (typeof cb !== 'function') {
86+
cb = options;
87+
options = {};
88+
}
89+
8490
// file does not exist | bail
8591
if (!fs.existsSync(filepath)) {
8692
cb ({
@@ -89,12 +95,6 @@ fileReplace.replaceCss = (filepath, options, cb) => {
8995
});
9096
}
9197

92-
// set cb if options are not set
93-
if (typeof cb !== 'function') {
94-
cb = options;
95-
options = {};
96-
}
97-
9898
fs.readFile(filepath, 'utf8', (err, data) => {
9999
rcs.selectorLibrary.set(data.match(regex));
100100

test/files/fixtures/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Test Document</title>
6+
</head>
7+
<body>
8+
<div class="jp-block"></div>
9+
<div class="jp-block__element"></div>
10+
</body>
11+
</html>

test/files/fixtures/main.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// jQuery example
2+
$('.jp-block');
3+
4+
// vanillaJS example
5+
document.getElementsByClassName('jp-block__element');
6+
document.getElementById('jp-block__element--modifier');

test/files/results/index.html

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Test Document</title>
6+
</head>
7+
<body>
8+
<div class="a"></div>
9+
<div class="b"></div>
10+
</body>
11+
</html>

test/files/results/main.txt

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
// jQuery example
2+
$('.a');
3+
4+
// vanillaJS example
5+
document.getElementsByClassName('b');
6+
document.getElementById('c');

test/rcs/fileReplace.spec.js

+48-7
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,60 @@ const fs = require('fs');
55
const expect = require('chai').expect;
66

77
describe('rcs file replace', () => {
8+
beforeEach(() => {
9+
// reset counter and selectors for tests
10+
rcs.selectorLibrary.selectors = {};
11+
rcs.nameGenerator.resetCountForTests();
12+
});
13+
814
describe('replaceCss', () => {
9-
beforeEach(() => {
10-
// reset counter and selectors for tests
11-
rcs.selectorLibrary.selectors = {};
12-
rcs.nameGenerator.resetCountForTests();
15+
it('should return the modified css file', done => {
16+
rcs.fileReplace.replaceCss('test/files/fixtures/style.css', (err, data) => {
17+
expect(data).to.equal(fs.readFileSync('test/files/results/style.css', 'utf8'));
18+
19+
done();
20+
});
1321
});
1422

15-
it('should replace css file and return modified selectors', done => {
16-
rcs.fileReplace.replaceCss('test/files/fixtures/style.css', {}, (err, data) => {
17-
expect(data).to.equal(fs.readFileSync('test/files/results/style.css', 'utf8'));
23+
it('should fail', done => {
24+
rcs.fileReplace.replaceCss('non/exisiting/path.css', err => {
25+
expect(err).to.be.an('object');
26+
expect(err.error).to.equal('ENOENT');
1827

1928
done();
2029
});
2130
});
2231
});
32+
33+
describe('replace any file', () => {
34+
it('should fail', done => {
35+
rcs.fileReplace.replace('non/exisiting/path.css', err => {
36+
expect(err).to.be.an('object');
37+
expect(err.error).to.equal('ENOENT');
38+
39+
done();
40+
});
41+
});
42+
43+
it('should return the modified html file', done => {
44+
rcs.fileReplace.replaceCss('test/files/fixtures/style.css', (err, data) => {
45+
rcs.fileReplace.replace('test/files/fixtures/index.html', (err, data) => {
46+
expect(data).to.equal(fs.readFileSync('test/files/results/index.html', 'utf8'));
47+
48+
done();
49+
});
50+
});
51+
});
52+
53+
it('should return the modified js file', done => {
54+
// `js` file imported as `txt` to avoid having mocha-phantomjs
55+
rcs.fileReplace.replaceCss('test/files/fixtures/style.css', (err, data) => {
56+
rcs.fileReplace.replace('test/files/fixtures/main.txt', (err, data) => {
57+
expect(data).to.equal(fs.readFileSync('test/files/results/main.txt', 'utf8'));
58+
59+
done();
60+
});
61+
});
62+
});
63+
});
2364
});

0 commit comments

Comments
 (0)