Skip to content

Commit

Permalink
update testing to get full code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
swang committed May 12, 2020
1 parent df66fdd commit 202d130
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 25 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
npm-debug.log
node_modules/*
old/*
.nyc_output/
7 changes: 4 additions & 3 deletions lib/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,15 @@ Resource.prototype._get = function(cb) {
return cb(null, pBody.data.results, pBody)
})
})

/* istanbul ignore next */
res.on('error', function(e) {
return cb(e)
})
}
)
.on('error', function(err) {
console.log(err)
return cb(err)
})
this.param = {}
}
Expand All @@ -125,11 +127,10 @@ Resource.prototype.limit = function() {
}
return this
}

resources = ['Comic', 'Character', 'Creator', 'Event', 'Series', 'Story']

for (var i = 0; i < resources.length; i++) {
var fn = (function addResource(rsrc) {
;(function addResource(rsrc) {
rsrc = plural(lower(rsrc))
Resource.prototype[rsrc] = function(queryVal) {
if (this.resource !== rsrc) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "API wrapper for Marvel Comics",
"main": "index.js",
"scripts": {
"test": "mocha test/*.js --reporter=spec"
"test": "nyc mocha test/*.js --reporter=spec"
},
"repository": {
"type": "git",
Expand Down
Empty file added test/fixtures/bad.gz
Empty file.
Binary file added test/fixtures/bad.json.gz
Binary file not shown.
Binary file added test/fixtures/character-hulk-resp.json.gz
Binary file not shown.
60 changes: 50 additions & 10 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,34 @@ describe('Marvel', function() {
should(false).ok()
})
})
describe('keys', function() {
it('should set public/private keys with keys()', function() {
var x = new Marvel({
privateKey: 'aaa',
publicKey: 'bbb'
})

x.privateKey.should.eql('aaa')
x.publicKey.should.eql('bbb')
x.keys('ccc', 'ddd')
x.privateKey.should.eql('ccc')
x.publicKey.should.eql('ddd')
})
})
describe('invalid domain set', function() {
it('should generate an http error', function() {
var Marvel = require('../index')
var marvel = new Marvel({
privateKey: 'aaa',
publicKey: 'bbb',
apiDomain: 'https://invalidUrl.what'
})
marvel.characters
.name('Hulk')
.get()
.should.be.rejectedWith(/ENOTFOUND/)
})
})
})

describe('marvel.events.*.get', function() {
Expand Down Expand Up @@ -90,7 +118,6 @@ describe('marvel.characters.*.get', function() {
)
})
})

})

describe('marvel.characters.*.get (as a promise)', function() {
Expand All @@ -110,14 +137,27 @@ describe('marvel.characters.*.get (as a promise)', function() {
})

it('should return hulk data using a promise', function() {
marvel.characters.name('Hulk').get().then(function(resp) {
resp[0].id.should.eql(1009351)
resp[0].name.should.eql('Hulk')
resp[0].resourceURI.should.eql('http://gateway.marvel.com/v1/public/characters/1009351')
resp[0].should.have.properties('comics', 'series', 'stories', 'events', 'urls', 'thumbnail', 'resourceURI')
}).catch(function(err) {
should.not.exist(err)
})
marvel.characters
.name('Hulk')
.get()
.then(function(resp) {
resp[0].id.should.eql(1009351)
resp[0].name.should.eql('Hulk')
resp[0].resourceURI.should.eql(
'http://gateway.marvel.com/v1/public/characters/1009351'
)
resp[0].should.have.properties(
'comics',
'series',
'stories',
'events',
'urls',
'thumbnail',
'resourceURI'
)
})
.catch(function(err) {
should.not.exist(err)
})
})

})
130 changes: 119 additions & 11 deletions test/resource.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
'use strict';
'use strict'

var should = require('should')
, Resource = require('../lib/resource.js')
var should = require('should'),
Resource = require('../lib/resource.js'),
sinon = require('sinon')

var https = require('https')
var fs = require('fs')
var IncomingMessage = require('http').IncomingMessage

var opts = {
API_VERSION: 'v1',
Expand All @@ -11,6 +16,19 @@ var opts = {
gzip: true
}

var fakeHttpResponse = function(fixtureFile) {
var httpGet = sinon.stub(https, 'get').callsFake(function(_opts, _cb) {
var im = new IncomingMessage('test-socket')
_cb(im)
im._read = function() {}
im.emit('data', fs.readFileSync('./test/fixtures/' + fixtureFile))
im.emit('end')
return httpGet
})
httpGet.on = function() {}
return httpGet
}

describe('Resource', function() {
var resourceCalls, test

Expand All @@ -25,12 +43,7 @@ describe('Resource', function() {
})
})

resourceCalls = [
'name'
, 'nameStartsWith'
, 'offset'
, 'orderBy'
]
resourceCalls = ['name', 'nameStartsWith', 'offset', 'orderBy']

for (var i = 0; i < resourceCalls.length; i++) {
;(function(descTest) {
Expand All @@ -40,20 +53,115 @@ describe('Resource', function() {
test.param[descTest].should.equal('_abcd')
})
})
}(resourceCalls[i]))
})(resourceCalls[i])
}

describe('limit', function() {
it('should set limit properly when called with one param', function() {
test.limit(20)
test.param.limit.should.equal(20)

})
it('should set offset and limit when called with two params', function() {
test.limit(5, 10)
test.param.offset.should.equal(5)
test.param.limit.should.equal(10)
})
})
describe('test a Resource() object', function() {
var r
beforeEach(function() {
r = new Resource('characters', opts)
})

it('should find subproperty resources for a resource', function() {
var fakeThis = {
resource: 'events',
param: {}
}
var actual = r.characters.bind(fakeThis)('Hulk')
var result = { resource: 'events', param: { characters: 'Hulk' } }
actual.should.eql(result)
})

it('should not allow the same resource/subresource', function() {
var fakeThis = {
resource: 'characters',
param: {}
}
var actual = r.characters.bind(fakeThis)('Hulk')
var result = { resource: 'characters', param: {} }
actual.should.eql(result)
})
})

describe('test a Resource() http calls', function() {
var _httpGet
var r
beforeEach(function() {
r = new Resource('characters', opts)
})

afterEach(function() {
if (_httpGet) _httpGet.restore()
})

it('should read response from http.get', function(done) {
_httpGet = fakeHttpResponse('character-hulk-resp.json.gz')

r.characters('Hulk').get(function(err, res) {
should.not.exist(err)
res[0].name.should.eql('Hulk')
// _httpGet.restore()
done()
})
})

it('should error when the server doesnt return a gzip', function(done) {
_httpGet = fakeHttpResponse('bad.gz')

r.characters('Hulk').get(function(err, res) {
should.exist(err)
should.exist(err.message)
err.message.should.match(/not a gzip result/i)
should.not.exist(res)
done()
})
})

it('should error when the server doesnt return a valid json', function(done) {
_httpGet = fakeHttpResponse('bad.json.gz')

r.characters('Hulk').get(function(err, res) {
should.exist(err)
should.exist(err.message)
err.message.should.match(/was not a JSON/i)
should.not.exist(res)
done()
})
})

it('should return a promise if the getter function has no parameters passed', function(done) {
var json = require('./fixtures/character-hulk.json') // eslint-disable-line global-require
var _get = sinon.stub(Resource.prototype, '_get').yields(null, json)
var x = Resource.prototype.get()
x.should.be.Promise()
x.then(function(res) {
should.exist(res)
}).catch(function(err) {
should.fail(err)
})
_get.restore()

var _getError = sinon
.stub(Resource.prototype, '_get')
.yields(new Error('fakeerror'))
var y = Resource.prototype.get()
y.should.be.Promise()
y.catch(function(err) {
should.exist(err)
done()
})
_getError.restore()
})
})
})

0 comments on commit 202d130

Please sign in to comment.