Skip to content

Commit eebd472

Browse files
committed
Adding tests
1 parent 1a9047f commit eebd472

File tree

25 files changed

+580
-9
lines changed

25 files changed

+580
-9
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules/
2-
.vscode/
2+
.vscode/
3+
test/*/output/

jasmine.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"spec_dir": "test",
3+
"spec_files": [
4+
"**/*.spec.js"
5+
]
6+
}

package-lock.json

Lines changed: 39 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
{
22
"name": "genrss",
3-
"version": "1.0.0",
3+
"version": "1.0.1",
44
"description": "Generate RSS feeds for static sites",
55
"main": "src/index.js",
66
"bin": "src/cli.js",
77
"scripts": {
8-
"test": ""
8+
"test": "jasmine --config=jasmine.json"
99
},
1010
"keywords": [
1111
"RSS",
@@ -22,5 +22,9 @@
2222
"glob": "^7.1.6",
2323
"jsdom": "^16.4.0",
2424
"xml": "^1.0.1"
25+
},
26+
"devDependencies": {
27+
"jasmine": "^3.6.3",
28+
"xml2js": "^0.4.23"
2529
}
2630
}

src/feed/createFeed.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ module.exports = async function createFeed(options, items) {
4848
const rss = xml(feedXmlObj, { declaration: true });
4949

5050
const output = path.join(options.outputDir, options.outputFile);
51-
log(`Writing feed xml to ${output}`)
51+
log(`Writing feed xml to ${output}`);
52+
await fs.promises.mkdir(path.dirname(output), { recursive: true });
5253
await fs.promises.writeFile(output, rss, 'utf8');
5354
};
5455

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ const findFiles = require('./feed/findFiles.js');
55
const processFiles = require('./feed/processFiles.js');
66
const createFeed = require('./feed/createFeed.js');
77

8-
module.exports = async function scan(userOptions) {
8+
module.exports = async function scan(runtimeOptions) {
99
log('Beginning scan')
10-
const options = await buildOptions();
10+
const options = await buildOptions(runtimeOptions);
1111
const files = await findFiles(options);
1212
const items = await processFiles(files, options);
1313
await createFeed(options, items);

src/options/buildOptions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const defaultOptions = require('./defaultOptions.json');
33
const fileUtils = require('../utils/file.js');
44
const log = require('../utils/log.js');
55

6-
module.exports = async function buildOptions() {
6+
module.exports = async function buildOptions(runtimeOptions) {
77
const configFile = path.join(process.cwd(), 'genrss.json');
88
log(`Checking for config file: ${configFile}`);
99
let userOptions = null;
@@ -14,7 +14,7 @@ module.exports = async function buildOptions() {
1414
log('Running with default options');
1515
}
1616

17-
const options = { ...defaultOptions, ...userOptions };
17+
const options = { ...defaultOptions, ...userOptions, ...runtimeOptions };
1818

1919
if(options.url == null) {
2020
throw new Error('Url must be specified. Please ensure that you have set the url property in your genrss.json configuration file');

test/invalidItems/input/index.html

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<p data-feed-id="1"
10+
data-feed-title="Feed item 1">
11+
Item 1
12+
</p>
13+
<p data-feed-date="2020-01-02"
14+
data-feed-title="Feed item 2">
15+
Item 2
16+
</p>
17+
<p data-feed-id="3"
18+
data-feed-date="2020-01-03"
19+
data-feed-title="Feed item 3">
20+
Item 3
21+
</p>
22+
</body>
23+
</html>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const scan = require('../../src/index');
2+
const path = require('path');
3+
const fs = require('fs');
4+
const xml2js = require('xml2js');
5+
6+
const inputDir = path.join(__dirname, 'input');
7+
const outputDir = path.join(__dirname, 'output');
8+
const outputFile = path.join(outputDir, 'rss.xml');
9+
const url = 'https://example.com';
10+
11+
describe('invalid items:', () => {
12+
beforeAll(async () => {
13+
await scan({
14+
inputDir,
15+
outputDir,
16+
url
17+
});
18+
});
19+
20+
describe('rss', () => {
21+
let xml;
22+
23+
beforeEach(async () => {
24+
const rssContent = await fs.promises.readFile(outputFile, 'utf8');
25+
const parser = new xml2js.Parser();
26+
xml = await parser.parseStringPromise(rssContent);
27+
});
28+
29+
it('has only valid items', async () => {
30+
expect(xml.rss.channel[0].item.length).toBe(1);
31+
});
32+
33+
it('ignores items without a date', async () => {
34+
expect(xml.rss.channel[0].item[0].title[0]).not.toBe('Feed item 1');
35+
});
36+
37+
it('ignores items without an id', async () => {
38+
expect(xml.rss.channel[0].item[0].title[0]).not.toBe('Feed item 2');
39+
});
40+
});
41+
})

test/manyFiles/input/index1.html

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Document</title>
7+
</head>
8+
<body>
9+
<p data-feed-id="1"
10+
data-feed-date="2020-01-01"
11+
data-feed-title="Feed item 1">
12+
Item 1
13+
</p>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)