Skip to content

Commit 39cd0b5

Browse files
Merge pull request #5 from christianbundy/euankdev
Fix searchPosts; write remaining tests
2 parents 8ee2a07 + 19ce247 commit 39cd0b5

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,10 @@ var hn = {
134134

135135
// search popular/recent posts
136136
searchPosts: function (query, cb) {
137-
hn.call('search?query=' + query + '&tags=story', cb);
137+
hn.call('search', {query: query, tags: 'story'}, cb);
138138
},
139139
searchLastPosts: function (query, cb) {
140-
hn.call('search_by_date?query=' + query + '&tags=story', cb);
140+
hn.call('search_by_date', {query: query, tags: 'story'}, cb);
141141
},
142142

143143
// search popular/recent stories

test/fixtures/fixtures.json

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

test/fixtures/generate_fixtures.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,24 @@ var urls = [
1212
'/api/v1/search_by_date?tags=(story%2Cpoll)',
1313
'/api/v1/search?tags=story',
1414
'/api/v1/search_by_date?tags=story',
15+
'/api/v1/items/17',
16+
'/api/v1/users/pg',
17+
'/api/v1/search?tags=comment%2Cauthor_pg',
18+
'/api/v1/search_by_date?tags=comment%2Cauthor_pg',
19+
'/api/v1/search?tags=poll%2Cauthor_pg',
20+
'/api/v1/search_by_date?tags=poll%2Cauthor_pg',
21+
'/api/v1/search?tags=story%2Cauthor_pg',
22+
'/api/v1/search_by_date?tags=story%2Cauthor_pg',
23+
'/api/v1/search?query=apple&tags=comment',
24+
'/api/v1/search_by_date?query=apple&tags=comment',
25+
'/api/v1/search?query=apple&tags=poll',
26+
'/api/v1/search_by_date?query=apple&tags=poll',
27+
'/api/v1/search?query=apple&tags=story',
28+
'/api/v1/search_by_date?query=apple&tags=story',
29+
'/api/v1/search?query=apple&tags=story',
30+
'/api/v1/search_by_date?query=apple&tags=story',
31+
'/api/v1/search?tags=ask_hn&query=apple&page=2',
32+
'/api/v1/search_by_date?tags=ask_hn&query=apple&page=2',
1533
];
1634

1735
async.mapSeries(urls, function(item, next) {

test/test.js

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ var fixtures = JSON.parse(fs.readFileSync(__dirname + "/fixtures/fixtures.json")
77

88
var api = nock('https://hn.algolia.com').persist();
99
fixtures.forEach(function(f) {
10-
console.log(f.url);
1110
api.get(f.url).reply(200, f.reply);
1211
});
1312

@@ -33,7 +32,7 @@ describe('hn', function(){
3332
return done("Error");
3433
}
3534
expect(data).to.have.property('hits');
36-
data.hits.map(function(comment) {
35+
data.hits.forEach(function(comment) {
3736
var intersection = tags.filter(function(n) {
3837
return comment._tags.indexOf(n) != -1;
3938
});
@@ -42,6 +41,19 @@ describe('hn', function(){
4241
done();
4342
}
4443

44+
function verifyDataHasAllOfTags(err, data, tags, done) {
45+
if(err) {
46+
return done("Error");
47+
}
48+
expect(data).to.have.property('hits');
49+
data.hits.forEach(function(item) {
50+
tags.forEach(function(tag) {
51+
expect(item._tags).to.contain(tag);
52+
});
53+
});
54+
done();
55+
}
56+
4557
it('should get comments', function(done) {
4658
hn.getComments(crazy_curry(['comment'], done, verifyDataHasOneOfTags));
4759
});
@@ -72,4 +84,79 @@ describe('hn', function(){
7284
it('should get latest stories', function(done) {
7385
hn.getLastStories(crazy_curry(['story'], done, verifyDataHasOneOfTags));
7486
});
87+
88+
it('should get item', function(done) {
89+
hn.getItem(17, function(err, res) {
90+
if(err) return done(err);
91+
expect(res.id).to.equal(17);
92+
expect(res.type).to.equal('comment');
93+
done();
94+
});
95+
});
96+
97+
it('should get user', function(done) {
98+
hn.getUser('pg', function(err, res) {
99+
if(err) return done(err);
100+
expect(res.username).to.equal('pg');
101+
done();
102+
});
103+
});
104+
105+
it('should get user comments', function(done) {
106+
hn.getUserComments('pg', crazy_curry(['comment','author_pg'], done, verifyDataHasAllOfTags));
107+
});
108+
it('should get last user comments', function(done) {
109+
hn.getLastUserComments('pg', crazy_curry(['comment','author_pg'], done, verifyDataHasAllOfTags));
110+
});
111+
112+
it('should get user polls', function(done) {
113+
hn.getUserPolls('pg', crazy_curry(['poll','author_pg'], done, verifyDataHasAllOfTags));
114+
});
115+
it('should get last user polls', function(done) {
116+
hn.getLastUserPolls('pg', crazy_curry(['poll','author_pg'], done, verifyDataHasAllOfTags));
117+
});
118+
119+
it('should get user stories', function(done) {
120+
hn.getUserStories('pg', crazy_curry(['story','author_pg'], done, verifyDataHasAllOfTags));
121+
});
122+
it('should get last user stories', function(done) {
123+
hn.getLastUserStories('pg', crazy_curry(['story','author_pg'], done, verifyDataHasAllOfTags));
124+
});
125+
126+
it('should search comments', function(done) {
127+
hn.searchComments('apple', crazy_curry(['comment'], done, verifyDataHasAllOfTags));
128+
});
129+
it('should search last comments', function(done) {
130+
hn.searchLastComments('apple', crazy_curry(['comment'], done, verifyDataHasAllOfTags));
131+
});
132+
133+
it('should search polls', function(done) {
134+
hn.searchPolls('apple', crazy_curry(['poll'], done, verifyDataHasAllOfTags));
135+
});
136+
it('should search last polls', function(done) {
137+
hn.searchLastPolls('apple', crazy_curry(['poll'], done, verifyDataHasAllOfTags));
138+
});
139+
140+
141+
it('should search posts', function(done) {
142+
hn.searchPosts('apple', crazy_curry(['story'], done, verifyDataHasAllOfTags));
143+
});
144+
it('should search last posts', function(done) {
145+
hn.searchLastPosts('apple', crazy_curry(['story'], done, verifyDataHasAllOfTags));
146+
});
147+
148+
it('should search posts', function(done) {
149+
hn.searchStories('apple', crazy_curry(['story'], done, verifyDataHasAllOfTags));
150+
});
151+
it('should search last posts', function(done) {
152+
hn.searchLastStories('apple', crazy_curry(['story'], done, verifyDataHasAllOfTags));
153+
});
154+
155+
it('should search', function(done) {
156+
hn.search({tags: 'ask_hn', query: 'apple', page: 2}, crazy_curry(['ask_hn'], done, verifyDataHasAllOfTags));
157+
});
158+
it('should search last', function(done) {
159+
hn.searchLast({tags: 'ask_hn', query: 'apple', page: 2}, crazy_curry(['ask_hn'], done, verifyDataHasAllOfTags));
160+
});
161+
75162
});

0 commit comments

Comments
 (0)