Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/helpers/JSONResponse.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]**
Expand Down Expand Up @@ -139,6 +145,12 @@ Checks for deep inclusion of a provided json in a response data.
I.seeResponseContainsJson({ user: { email: '[email protected]' } });
```

```js
// response.data == [{ user: { name: 'jon', email: '[email protected]' } }]

I.seeResponseContainsJson({ user: { email: '[email protected]' } });
```

#### Parameters

- `json` **[object][2]**
Expand All @@ -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: '[email protected]' } }]

I.seeResponseContainsKeys(['user']);
```

#### Parameters

- `keys` **[array][3]**
Expand Down
34 changes: 31 additions & 3 deletions lib/helper/JSONResponse.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,21 @@ class JSONResponse extends Helper {
*
* I.seeResponseContainsJson({ user: { email: '[email protected]' } });
* ```
* ```js
* // response.data == [{ user: { name: 'jon', email: '[email protected]' } }]
*
* I.seeResponseContainsJson({ user: { email: '[email protected]' } });
* ```
*
* @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);
}
}

/**
Expand All @@ -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);
}
}

/**
Expand All @@ -206,11 +224,21 @@ class JSONResponse extends Helper {
* I.seeResponseContainsKeys(['user']);
* ```
*
* ```js
* // response.data == [{ user: { name: 'jon', email: '[email protected]' } }]
*
* 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);
}
}

/**
Expand Down
37 changes: 35 additions & 2 deletions test/helper/JSONResponse_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand All @@ -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: '[email protected]' } }] });
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 }) => {
Expand All @@ -124,7 +157,7 @@ describe('JSONResponse', () => {
name: joi.string(),
}),
});
const fn = (joi) => {
const fn = () => {
return schema;
};
I.seeResponseMatchesJsonSchema(fn);
Expand Down