Skip to content

Commit 2cac0dd

Browse files
committed
test(websheep-api): ✅ validate response & add utils
1 parent 655476b commit 2cac0dd

File tree

5 files changed

+342
-16
lines changed

5 files changed

+342
-16
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,50 @@
1-
import { Express } from 'express';
2-
import * as request from 'supertest';
1+
import { openApiDocument } from './../shared/openapi/document';
32
import * as express from 'express';
3+
// Import this plugin
4+
import jestOpenAPI from 'jest-openapi';
5+
import { join } from 'path';
6+
import * as request from 'supertest';
47
import { resetDatabase } from '../database';
58
import { sheepRouter } from './sheep.router';
69

10+
jestOpenAPI({
11+
...openApiDocument,
12+
/* Remove servers otherwise the test crashes because we are using a random server port. */
13+
servers: []
14+
});
15+
716
describe('sheep router', () => {
8-
let app: Express;
17+
it(`should get farmer's sheep without authorization`, async () => {
18+
const { client, givenUser } = setUp();
19+
20+
givenUser('karinelemarchand');
921

10-
beforeEach(() => {
11-
app = express();
22+
const response = await client.get('/farmers/foobar/sheep');
23+
24+
expect(response.status).toEqual(200);
25+
expect(response.body.totalCount).toEqual(13);
26+
expect(response.body.items[0].name).toEqual('Adriana');
27+
expect(response).toSatisfyApiSpec();
28+
});
29+
30+
function setUp() {
31+
let _userId: string;
32+
33+
const app = express();
34+
35+
app.use((req, res, next) => {
36+
req['user'] = _userId ? { id: _userId } : null;
37+
next();
38+
});
1239
app.use(sheepRouter);
40+
1341
resetDatabase();
14-
});
1542

16-
it(`should get farmer's sheep without authorization`, async () => {
17-
const { body } = await request(app).get('/farmers/foobar/sheep');
18-
expect(body.totalCount).toEqual(13);
19-
expect(body.items[0].name).toEqual('Adriana');
20-
});
43+
return {
44+
client: request(app),
45+
givenUser(userId: string) {
46+
_userId = userId;
47+
}
48+
};
49+
}
2150
});

apps/websheep-api/src/app/shared/openapi/websheep.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
openapi: '3.0.0'
1+
openapi: '3.1.0'
22
info:
33
version: 1.0.0
44
title: Websheep
@@ -338,6 +338,8 @@ components:
338338
createdAt:
339339
type: string
340340
format: date-time
341+
farmId:
342+
type: string
341343

342344
# Sheep:
343345
# allOf:
@@ -372,7 +374,6 @@ components:
372374
allOf:
373375
- $ref: '#/components/schemas/ListResponse'
374376
- type: object
375-
additionalProperties: false
376377
properties:
377378
items:
378379
type: array

apps/websheep-api/src/app/shared/sheep/get-farmer-sheep-list.ts

-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export function getFarmerSheepList(req, res) {
1414
.map(sheep => serializeSheep({ sheep, host: req.headers.host }));
1515

1616
res.json({
17-
next: null,
18-
previous: null,
1917
totalCount: sheepList.length,
2018
items: sheepList
2119
});

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
"dotenv": "6.2.0",
9999
"eslint": "6.1.0",
100100
"jest": "24.1.0",
101+
"jest-openapi": "^0.14.2",
101102
"jest-preset-angular": "7.0.0",
102103
"jsonpath": "^1.0.2",
103104
"prettier": "1.18.2",

0 commit comments

Comments
 (0)