Skip to content

Commit 871deb2

Browse files
committed
Add some unit tests
1 parent 9650902 commit 871deb2

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,10 @@
2626
"homepage": "https://github.com/christianbundy/node-hacker-news-api",
2727
"dependencies": {
2828
"request": "~2.34.0"
29+
},
30+
"devDependencies": {
31+
"chai": "*",
32+
"nock": "*",
33+
"mocha": "*"
2934
}
3035
}

test/fixtures/fixtures.json

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

test/fixtures/generate_fixtures.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var https = require('https');
2+
var async = require('async');
3+
var request = require('request');
4+
var fs = require('fs');
5+
6+
var urls = [
7+
'/api/v1/search?tags=comment',
8+
'/api/v1/search_by_date?tags=comment',
9+
'/api/v1/search?tags=poll',
10+
'/api/v1/search_by_date?tags=poll',
11+
'/api/v1/search?tags=(story%2Cpoll)',
12+
'/api/v1/search_by_date?tags=(story%2Cpoll)',
13+
'/api/v1/search?tags=story',
14+
'/api/v1/search_by_date?tags=story',
15+
];
16+
17+
async.mapSeries(urls, function(item, next) {
18+
request.get('https://hn.algolia.com' + item, function(err, response, body) {
19+
next(err, {
20+
url: item,
21+
reply: JSON.parse(body)
22+
});
23+
});
24+
}, function(err, results) {
25+
if(err) {
26+
console.err("Bad news: " + err);
27+
process.exit();
28+
}
29+
fs.writeFileSync("./fixtures.json", JSON.stringify(results));
30+
console.log("Wrote fixtures.json!");
31+
});

test/test.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
var hn = require("./../index.js");
2+
var fs = require('fs');
3+
var expect = require('chai').expect;
4+
var nock = require('nock');
5+
6+
var fixtures = JSON.parse(fs.readFileSync(__dirname + "/fixtures/fixtures.json"));
7+
8+
var api = nock('https://hn.algolia.com').persist();
9+
fixtures.forEach(function(f) {
10+
console.log(f.url);
11+
api.get(f.url).reply(200, f.reply);
12+
});
13+
14+
15+
var slice = function(arr){ return Array.prototype.slice.call(arr); };
16+
17+
function crazy_curry(args, fn) {
18+
args = slice(arguments);
19+
fn = args.pop();
20+
21+
return function(innerArgs) {
22+
console.log(arguments[0]);
23+
innerArgs = slice(arguments);
24+
Array.prototype.push.apply(innerArgs, args);
25+
fn.apply(this, innerArgs);
26+
};
27+
}
28+
29+
30+
31+
describe('hn', function(){
32+
function verifyDataHasOneOfTags(err, data, tags, done) {
33+
if(err) {
34+
return done("Error");
35+
}
36+
expect(data).to.have.property('hits');
37+
data.hits.map(function(comment) {
38+
var intersection = tags.filter(function(n) {
39+
return comment._tags.indexOf(n) != -1;
40+
});
41+
expect(intersection).not.to.be.empty;
42+
});
43+
done();
44+
}
45+
46+
it('should get comments', function(done) {
47+
hn.getComments(crazy_curry(['comment'], done, verifyDataHasOneOfTags));
48+
});
49+
it('should get latest comments', function(done) {
50+
hn.getLastComments(crazy_curry(['comment'], done, verifyDataHasOneOfTags));
51+
});
52+
53+
54+
it('should get polls', function(done) {
55+
hn.getPolls(crazy_curry(['poll'], done, verifyDataHasOneOfTags));
56+
});
57+
it('should get latest polls', function(done) {
58+
hn.getLastPolls(crazy_curry(['poll'], done, verifyDataHasOneOfTags));
59+
});
60+
61+
62+
it('should get posts', function(done) {
63+
hn.getPosts(crazy_curry(['story', 'poll'], done, verifyDataHasOneOfTags));
64+
});
65+
it('should get latest posts', function(done) {
66+
hn.getLastPosts(crazy_curry(['story', 'poll'], done, verifyDataHasOneOfTags));
67+
});
68+
69+
70+
it('should get stories', function(done) {
71+
hn.getStories(crazy_curry(['story'], done, verifyDataHasOneOfTags));
72+
});
73+
it('should get latest stories', function(done) {
74+
hn.getLastStories(crazy_curry(['story'], done, verifyDataHasOneOfTags));
75+
});
76+
});

0 commit comments

Comments
 (0)