Skip to content

Commit 9ca6f73

Browse files
authored
improve: jsonresponse validation (#3316)
* WIP * fix: handle the returned response is array * fix: code reviews * fix: code reviews
1 parent e956cb8 commit 9ca6f73

File tree

3 files changed

+84
-5
lines changed

3 files changed

+84
-5
lines changed

docs/helpers/JSONResponse.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ Checks for deep inclusion of a provided json in a response data.
9292
I.dontSeeResponseContainsJson({ user: 2 });
9393
```
9494

95+
```js
96+
// response.data == [{ data: { user: 1 } }]
97+
98+
I.dontSeeResponseContainsJson({ user: 2 });
99+
```
100+
95101
#### Parameters
96102

97103
- `json` **[object][2]**
@@ -139,6 +145,12 @@ Checks for deep inclusion of a provided json in a response data.
139145
I.seeResponseContainsJson({ user: { email: '[email protected]' } });
140146
```
141147

148+
```js
149+
// response.data == [{ user: { name: 'jon', email: '[email protected]' } }]
150+
151+
I.seeResponseContainsJson({ user: { email: '[email protected]' } });
152+
```
153+
142154
#### Parameters
143155

144156
- `json` **[object][2]**
@@ -153,6 +165,12 @@ Checks for deep inclusion of a provided json in a response data.
153165
I.seeResponseContainsKeys(['user']);
154166
```
155167

168+
```js
169+
// response.data == [{ user: { name: 'jon', email: '[email protected]' } }]
170+
171+
I.seeResponseContainsKeys(['user']);
172+
```
173+
156174
#### Parameters
157175

158176
- `keys` **[array][3]**

lib/helper/JSONResponse.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,12 +173,21 @@ class JSONResponse extends Helper {
173173
*
174174
* I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } });
175175
* ```
176+
* ```js
177+
* // response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }]
178+
*
179+
* I.seeResponseContainsJson({ user: { email: 'jon@doe.com' } });
180+
* ```
176181
*
177182
* @param {object} json
178183
*/
179184
seeResponseContainsJson(json = {}) {
180185
this._checkResponseReady();
181-
expect(this.response.data).to.deep.match(json);
186+
if (Array.isArray(this.response.data)) {
187+
this.response.data.forEach(data => expect(data).to.deep.match(json));
188+
} else {
189+
expect(this.response.data).to.deep.match(json);
190+
}
182191
}
183192

184193
/**
@@ -189,12 +198,21 @@ class JSONResponse extends Helper {
189198
*
190199
* I.dontSeeResponseContainsJson({ user: 2 });
191200
* ```
201+
* ```js
202+
* // response.data == [{ data: { user: 1 } }]
203+
*
204+
* I.dontSeeResponseContainsJson({ user: 2 });
205+
* ```
192206
*
193207
* @param {object} json
194208
*/
195209
dontSeeResponseContainsJson(json = {}) {
196210
this._checkResponseReady();
197-
expect(this.response.data).not.to.deep.match(json);
211+
if (Array.isArray(this.response.data)) {
212+
this.response.data.forEach(data => expect(data).not.to.deep.match(json));
213+
} else {
214+
expect(this.response.data).not.to.deep.match(json);
215+
}
198216
}
199217

200218
/**
@@ -206,11 +224,21 @@ class JSONResponse extends Helper {
206224
* I.seeResponseContainsKeys(['user']);
207225
* ```
208226
*
227+
* ```js
228+
* // response.data == [{ user: { name: 'jon', email: 'jon@doe.com' } }]
229+
*
230+
* I.seeResponseContainsKeys(['user']);
231+
* ```
232+
*
209233
* @param {array} keys
210234
*/
211235
seeResponseContainsKeys(keys = []) {
212236
this._checkResponseReady();
213-
expect(this.response.data).to.include.keys(keys);
237+
if (Array.isArray(this.response.data)) {
238+
this.response.data.forEach(data => expect(data).to.include.keys(keys));
239+
} else {
240+
expect(this.response.data).to.include.keys(keys);
241+
}
214242
}
215243

216244
/**

test/helper/JSONResponse_test.js

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,23 @@ describe('JSONResponse', () => {
8383
{ id: 1, author: 'davert' },
8484
],
8585
});
86-
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('AssertionError');
86+
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }');
87+
});
88+
89+
it('should check for json inclusion - returned Array', () => {
90+
const arrayData = [{ ...data }];
91+
restHelper.config.onResponse({ data: arrayData });
92+
I.seeResponseContainsJson({
93+
posts: [
94+
{ id: 2 },
95+
],
96+
});
97+
I.seeResponseContainsJson({
98+
posts: [
99+
{ id: 1, author: 'davert' },
100+
],
101+
});
102+
expect(() => I.seeResponseContainsJson({ posts: [{ id: 2, author: 'boss' }] })).to.throw('expected { …(2) } to deeply match { Object (posts) }');
87103
});
88104

89105
it('should simply check for json inclusion', () => {
@@ -93,16 +109,33 @@ describe('JSONResponse', () => {
93109
I.dontSeeResponseContainsJson({ name: 'joe' });
94110
});
95111

112+
it('should simply check for json inclusion - returned Array', () => {
113+
restHelper.config.onResponse({ data: [{ user: { name: 'jon', email: '[email protected]' } }] });
114+
I.seeResponseContainsJson({ user: { name: 'jon' } });
115+
I.dontSeeResponseContainsJson({ user: { name: 'jo' } });
116+
I.dontSeeResponseContainsJson({ name: 'joe' });
117+
});
118+
96119
it('should simply check for json equality', () => {
97120
restHelper.config.onResponse({ data: { user: 1 } });
98121
I.seeResponseEquals({ user: 1 });
99122
});
100123

124+
it('should simply check for json equality - returned Array', () => {
125+
restHelper.config.onResponse({ data: [{ user: 1 }] });
126+
I.seeResponseEquals({ user: 1 });
127+
});
128+
101129
it('should check json contains keys', () => {
102130
restHelper.config.onResponse({ data: { user: 1, post: 2 } });
103131
I.seeResponseContainsKeys(['user', 'post']);
104132
});
105133

134+
it('should check json contains keys - returned Array', () => {
135+
restHelper.config.onResponse({ data: [{ user: 1, post: 2 }] });
136+
I.seeResponseContainsKeys(['user', 'post']);
137+
});
138+
106139
it('should check for json by callback', () => {
107140
restHelper.config.onResponse({ data });
108141
const fn = ({ expect, data }) => {
@@ -124,7 +157,7 @@ describe('JSONResponse', () => {
124157
name: joi.string(),
125158
}),
126159
});
127-
const fn = (joi) => {
160+
const fn = () => {
128161
return schema;
129162
};
130163
I.seeResponseMatchesJsonSchema(fn);

0 commit comments

Comments
 (0)