Skip to content

Commit

Permalink
Merge pull request #501 from H1Gdev/utils
Browse files Browse the repository at this point in the history
Allow special characters * and ? to be used for object values.
  • Loading branch information
vlucas authored Nov 26, 2018
2 parents 4ca9442 + d0ccae7 commit f87c7c3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 16 deletions.
1 change: 1 addition & 0 deletions __tests__/expects_jsonTypes_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ describe('expect(\'jsonTypes\')', function() {
frisby.fromJSON({
offers: [{name: 'offer1'}, {name: 'offer2'}]
})
.expect('jsonTypes', 'offers', Joi.array())
.expect('jsonTypes', 'offers.*', {
name: Joi.string()
})
Expand Down
38 changes: 38 additions & 0 deletions __tests__/expects_json_path_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ describe('expect(\'json\', <path>, <value>)', function() {
.done(doneFn);
});

it('should test one of the values in an object', function(doneFn) {
mocks.use(['getUsersName']);

frisby.fetch(testHost + '/users.name')
.expect('json', 'data.?', {
id: 1,
email: '[email protected]'
})
.done(doneFn);
});

it('should test one of the values in an object for a single field', function(doneFn) {
mocks.use(['getUsersName']);

frisby.fetch(testHost + '/users.name')
.expect('json', 'data.?.email', '[email protected]')
.done(doneFn);
});

it('should error on an empty array (1)', function(doneFn) {
frisby.setup({ request: { inspectOnFailure: false } })
.fromJSON([])
Expand Down Expand Up @@ -127,6 +146,25 @@ describe('expect(\'json\', <path>, <value>)', function() {
.expect('jsonTypes', 'data.&.id', Joi.number())
.done(doneFn);
});

it('should test every object in an object (2)', function(doneFn) {
mocks.use(['getUsersName']);

frisby.fetch(testHost + '/users.name')
.expect('jsonTypes', 'data.*', {
id: Joi.number(),
email: Joi.string().email()
})
.done(doneFn);
});

it('should test every object in an object for a single value (2)', function(doneFn) {
mocks.use(['getUsersName']);

frisby.fetch(testHost + '/users.name')
.expect('jsonTypes', 'data.*.id', Joi.number())
.done(doneFn);
});
});

});
Expand Down
20 changes: 4 additions & 16 deletions src/frisby/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,10 @@ function withPath(path, jsonBody, callback) {
// Use given path to check deep objects
_.each(_.toPath(path), segment => {
_.each(jsonChunks.splice(0), jsonChunk => {
let jt = typeof jsonChunk;

if ('*' === segment || '?' === segment) {
// Must be array if special characters are present
if (!_.isArray(jsonChunk)) {
throw new TypeError(`Expected '${path}' to be Array (got '${jt}' from JSON response)`);
}

type = segment;
_.each(jsonChunk, value => {
jsonChunks.push(value);
});
} else if ('&' === segment) {
if (_.includes(['*', '?', '&'], segment)) {
// Must be object if special character is present
if (!_.isObject(jsonChunk)) {
throw new TypeError(`Expected '${path}' to be Object (got '${jt}' from JSON response)`);
throw new TypeError(`Expected '${path}' to be Object (got '${typeof jsonChunk}' from JSON response)`);
}

type = segment;
Expand All @@ -50,7 +38,7 @@ function withPath(path, jsonBody, callback) {
});

if ('?' === type) {
// ONE item in array should match
// ONE item in object and array should match
let itemCount = jsonChunks.length;
let errorCount = 0;
let errorLast;
Expand All @@ -69,7 +57,7 @@ function withPath(path, jsonBody, callback) {
throw errorLast || new Error(`Expected one object in path '${path}' to match provided JSON values`);
}
} else {
// EACH item in array should match
// EACH item in object and array should match
// Normal matcher
_.each(jsonChunks, jsonChunk => {
callback(jsonChunk);
Expand Down

0 comments on commit f87c7c3

Please sign in to comment.