diff --git a/docs/helpers/JSONResponse.md b/docs/helpers/JSONResponse.md index e36d85353..765c4e159 100644 --- a/docs/helpers/JSONResponse.md +++ b/docs/helpers/JSONResponse.md @@ -92,6 +92,12 @@ Checks for deep inclusion of a provided json in a response data. I.dontSeeResponseContainsJson({ user: 2 }); ``` +```js +// response.data == [{ data: { user: 1 } }] + +I.dontSeeResponseContainsJson({ user: 2 }); +``` + #### Parameters - `json` **[object][2]** @@ -139,6 +145,12 @@ Checks for deep inclusion of a provided json in a response data. I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } }); ``` +```js +// response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }] + +I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } }); +``` + #### Parameters - `json` **[object][2]** @@ -153,6 +165,12 @@ Checks for deep inclusion of a provided json in a response data. I.seeResponseContainsKeys(['user']); ``` +```js +// response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }] + +I.seeResponseContainsKeys(['user']); +``` + #### Parameters - `keys` **[array][3]** diff --git a/lib/helper/JSONResponse.js b/lib/helper/JSONResponse.js index 1d447121e..dd180bcce 100644 --- a/lib/helper/JSONResponse.js +++ b/lib/helper/JSONResponse.js @@ -173,12 +173,21 @@ class JSONResponse extends Helper { * * I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } }); * ``` + * ```js + * // response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }] + * + * I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } }); + * ``` * * @param {object} json */ seeResponseContainsJson(json = {}) { this._checkResponseReady(); - expect(this.response.data).to.deep.match(json); + if (Array.isArray(this.response.data)) { + this.response.data.forEach(data => expect(data).to.deep.match(json)); + } else { + expect(this.response.data).to.deep.match(json); + } } /** @@ -189,12 +198,21 @@ class JSONResponse extends Helper { * * I.dontSeeResponseContainsJson({ user: 2 }); * ``` + * ```js + * // response.data == [{ data: { user: 1 } }] + * + * I.dontSeeResponseContainsJson({ user: 2 }); + * ``` * * @param {object} json */ dontSeeResponseContainsJson(json = {}) { this._checkResponseReady(); - expect(this.response.data).not.to.deep.match(json); + if (Array.isArray(this.response.data)) { + this.response.data.forEach(data => expect(data).not.to.deep.match(json)); + } else { + expect(this.response.data).not.to.deep.match(json); + } } /** @@ -206,11 +224,21 @@ class JSONResponse extends Helper { * I.seeResponseContainsKeys(['user']); * ``` * + * ```js + * // response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }] + * + * I.seeResponseContainsKeys(['user']); + * ``` + * * @param {array} keys */ seeResponseContainsKeys(keys = []) { this._checkResponseReady(); - expect(this.response.data).to.include.keys(keys); + if (Array.isArray(this.response.data)) { + this.response.data.forEach(data => expect(data).to.include.keys(keys)); + } else { + expect(this.response.data).to.include.keys(keys); + } } /** diff --git a/test/helper/JSONResponse_test.js b/test/helper/JSONResponse_test.js index 155894029..94da6da08 100644 --- a/test/helper/JSONResponse_test.js +++ b/test/helper/JSONResponse_test.js @@ -83,7 +83,23 @@ describe('JSONResponse', () => { { id: 1, author: 'davert' }, ], }); - expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('AssertionError'); + expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }'); + }); + + it('should check for json inclusion - returned Array', () => { + const arrayData = [{ ...data }]; + restHelper.config.onResponse({ data: arrayData }); + I.seeResponseContainsJson({ + posts: [ + { id: 2 }, + ], + }); + I.seeResponseContainsJson({ + posts: [ + { id: 1, author: 'davert' }, + ], + }); + expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }'); }); it('should simply check for json inclusion', () => { @@ -93,16 +109,33 @@ describe('JSONResponse', () => { I.dontSeeResponseContainsJson({ name: 'joe' }); }); + it('should simply check for json inclusion - returned Array', () => { + restHelper.config.onResponse({ data: [{ user: { name: 'jon', email: 'jon@doe.com' } }] }); + I.seeResponseContainsJson({ user: { name: 'jon' } }); + I.dontSeeResponseContainsJson({ user: { name: 'jo' } }); + I.dontSeeResponseContainsJson({ name: 'joe' }); + }); + it('should simply check for json equality', () => { restHelper.config.onResponse({ data: { user: 1 } }); I.seeResponseEquals({ user: 1 }); }); + it('should simply check for json equality - returned Array', () => { + restHelper.config.onResponse({ data: [{ user: 1 }] }); + I.seeResponseEquals({ user: 1 }); + }); + it('should check json contains keys', () => { restHelper.config.onResponse({ data: { user: 1, post: 2 } }); I.seeResponseContainsKeys(['user', 'post']); }); + it('should check json contains keys - returned Array', () => { + restHelper.config.onResponse({ data: [{ user: 1, post: 2 }] }); + I.seeResponseContainsKeys(['user', 'post']); + }); + it('should check for json by callback', () => { restHelper.config.onResponse({ data }); const fn = ({ expect, data }) => { @@ -124,7 +157,7 @@ describe('JSONResponse', () => { name: joi.string(), }), }); - const fn = (joi) => { + const fn = () => { return schema; }; I.seeResponseMatchesJsonSchema(fn);