|
| 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