Skip to content

Commit

Permalink
Add open api comments and test for vehicles endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kk5190 committed Oct 5, 2022
1 parent ae04469 commit c8d35a8
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
22 changes: 20 additions & 2 deletions modules/vehicles/api/vehicles-routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,27 @@ import { getQtyFromRequest } from '../../../utils/route-utils';
import getRandomVehicles from '../utils/getRandomVehicles';

module.exports = function (app: core.Express) {
//Get a randomly generated user
/**
* @openapi
* "/vehicles/{qty}":
* get:
* tags:
* - Vehicles
* summary: Obtain a random list of vehicles
* parameters:
* - in: path
* name: qty
* description: The amount of vehicles you require
* type: string
* default: 10
* responses:
* '200':
* description: OK
* schema:
* $ref: '#/definitions/MockVehicles'
*/
app.get('/vehicles/:qty?', (req: Request, res: Response) => {
const qty = getQtyFromRequest(req, 1);
const qty = getQtyFromRequest(req, 10);
const users = getRandomVehicles(qty);
res.json(users);
});
Expand Down
34 changes: 34 additions & 0 deletions modules/vehicles/consts/Vehicle.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,37 @@
/**
* @openapi
* definitions:
* MockVehicles:
* type: array
* items:
* type: object
* properties:
* name:
* type: string
* example: Jeep Model X
* type:
* type: string
* example: Minivan
* color:
* type: string
* example: tan
* fuel:
* type: string
* example: Hybrid
* manufacturer:
* type: string
* example: Jeep
* model:
* type: string
* example: A8
* vin:
* type: string
* example: NP1WJW8L8WWM52689
* vrm:
* type: string
* example: JX30UEU
*/

type Vehicle = {
name: string;
type: string;
Expand Down
28 changes: 28 additions & 0 deletions modules/vehicles/tests/api/vehicles-routes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const request = require('supertest');
const baseURL = 'http://localhost:3000';

describe('vehicles api endpoints', () => {
describe('GET /vehicles', () => {
it('should return a vehicle', async () => {
const response = await request(baseURL).get(`/vehicles/`);

expect(response.body[0]).toHaveProperty('name');
expect(response.body[0]).toHaveProperty('type');
expect(response.body[0]).toHaveProperty('color');
expect(response.body[0]).toHaveProperty('fuel');
expect(response.body[0]).toHaveProperty('manufacturer');
expect(response.body[0]).toHaveProperty('model');
expect(response.body[0]).toHaveProperty('vin');
expect(response.body[0]).toHaveProperty('vrm');
});
});

describe('GET /vehicles/qty', () => {
const qty = 5;

it('should return a list of users ', async () => {
const response = await request(baseURL).get(`/users/${qty}`);
expect(response.body.length).toEqual(qty);
});
});
});
9 changes: 9 additions & 0 deletions modules/vehicles/tests/utils/getRandomVehicles.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import getRandomVehicles from '../../utils/getRandomVehicles';

describe('get random vehicles', () => {
it('should return a list of random vehicles', () => {
const amount = 4;
const res = getRandomVehicles(4);
expect(res.length).toBe(amount);
});
});

0 comments on commit c8d35a8

Please sign in to comment.