Skip to content

Commit

Permalink
Add expectKey helper
Browse files Browse the repository at this point in the history
Similar to expectValue, you can use expectKey to assert that the response
body contains a given a key.

This can be very useful when testing against live APIs where data can
change, but the schema should remain the same.
  • Loading branch information
nettofarah committed Apr 28, 2016
1 parent aeb7ff6 commit 34e0789
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 0 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ hippie()
.get('/users/vesln')
.expectStatus(200)
.expectHeader('Content-Type', 'application/json; charset=utf-8')
.expectKey('username')
.expectValue('username', 'vesln')
.expectValue('repos[0].name', 'jsmd')
.expectBody({
Expand Down Expand Up @@ -411,6 +412,22 @@ hippie()
For more information about string paths visit
[pathval](https://github.com/chaijs/pathval).

### #expectKey

Register a string path expectation for a given key.

```js
hippie()
.json()
.get('https://api.github.com/users/vesln')
.expectKey('details.company')
.expectKey('repos[0].name')
.end(fn);
```

For more information about string paths visit
[pathval](https://github.com/chaijs/pathval).

### #expectBody

Strict expectations:
Expand Down
1 change: 1 addition & 0 deletions examples/expectation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ api()
.expectStatus(200)
.expectHeader('Content-Type', 'application/json; charset=utf-8')
.expectValue('username', 'vesln')
.expectKey('repos')
.expectValue('repos[0].name', 'jsmd')
.expectBody({
username: 'vesln',
Expand Down
13 changes: 13 additions & 0 deletions lib/hippie/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,19 @@ Client.prototype.expectValue = function(key, val) {
return this;
};

/**
* Set a key expectation.
*
* @param {String} string path
* @returns {Client} self
* @api public
*/

Client.prototype.expectKey = function(key) {
this.expect(expect.keyCheck(key));
return this;
};

/**
* Set a body expectation.
*
Expand Down
16 changes: 16 additions & 0 deletions lib/hippie/expect.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,22 @@ exports.value = function(key, val) {
};
};

/**
* Return a key expectation.
*
* @param {String} string path
* @returns {Function}
* @api public
*/

exports.keyCheck = function(keyParam) {
return function keyCheck(res, body, next) {
var value = pathval(keyParam, body);
var notUndefined = typeof value !== 'undefined';
next(assert(notUndefined, true, 'Key - ' + keyParam));
};
};

/**
* Return a body expectation.
*
Expand Down
31 changes: 31 additions & 0 deletions test/expect-key.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

describe('#expectKey', function() {
it('returns an error when the key is missing from the response', function(done) {
api()
.json()
.get('/keys')
.expectKey('non_existing')
.end(function(err) {
err.should.be.ok;
done();
});
});

it('does not return an error when the key is present', function(done) {
api()
.json()
.get('/keys')
.expectKey('hippie')
.end(done);
});

it('should work with falsy values if key is present', function(done) {
api()
.json()
.get('/keys')
.expectKey('blank')
.expectKey('nullable')
.expectKey('boolean')
.end(done);
});
});
4 changes: 4 additions & 0 deletions test/support/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ app.get('/list', function(req, res) {
res.json([{ id: 4 }]);
});

app.get('/keys', function(req, res) {
res.json({ hippie: 'cool', blank: '', nullable: null, boolean: false });
});

app.get('/user', function(req, res) {
res.json({ id: 4 });
});
Expand Down

0 comments on commit 34e0789

Please sign in to comment.