From 418fc5647f2adb97df312eed5eb9c2d17de9d24c Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Fri, 9 Apr 2021 20:49:54 +0200 Subject: [PATCH] test: added albumSearch test; chore: removed sample endpoint --- server/package.json | 2 +- server/src/routes/api/get/albumSearch.spec.ts | 40 ++++++++++ server/src/routes/api/get/albumSearch.ts | 77 +++++++++++++++++++ server/src/routes/api/get/index.ts | 4 +- server/src/routes/api/get/sample.ts | 11 --- server/src/tests/cookie-parser.spec.ts | 2 +- server/src/types.ts | 3 + 7 files changed, 124 insertions(+), 15 deletions(-) create mode 100644 server/src/routes/api/get/albumSearch.spec.ts delete mode 100644 server/src/routes/api/get/sample.ts diff --git a/server/package.json b/server/package.json index 69730cf..794d644 100644 --- a/server/package.json +++ b/server/package.json @@ -10,7 +10,7 @@ "prebuild": "yarn lint", "build": "tsc", "test": "jest", - "test-watch": "jest --watchAll" + "test-watch": "jest --watch" }, "dependencies": { "cookie-parser": "1.4.5", diff --git a/server/src/routes/api/get/albumSearch.spec.ts b/server/src/routes/api/get/albumSearch.spec.ts new file mode 100644 index 0000000..990f74a --- /dev/null +++ b/server/src/routes/api/get/albumSearch.spec.ts @@ -0,0 +1,40 @@ +import request from 'supertest' +import { app } from '../../../app' + +describe('albumSearch requests', () => { + it('should respond 200 to calls with term', async () => { + const responseStatusCollector: number[] = [] + const batchCalls = [ + '/api/album-search/?term=eminem', + '/api/album-search/?term=eminem?start=10', + '/api/album-search/?term=eminem?ack=aa', + '/api/album-search/?term=eminem?ack=aa?start=10', + '/api/album-search/?term=eminem?ack=aa?start=10?nb=34' + ] + + for (const uri of batchCalls) { + responseStatusCollector.push((await request(app).get(uri).send()).status) + } + + expect(responseStatusCollector).toMatchObject(new Array(responseStatusCollector.length).fill(200)) + }) + + it('should respond 400 to calls without term', async () => { + const responseStatusCollector: number[] = [] + const batchCalls = [ + '/api/album-search/', + '/api/album-search/?start=10', + '/api/album-search/?ack=aa', + '/api/album-search/?ack=aa?start=10', + '/api/album-search/?ack=aa?start=10?nb=34' + ] + + for (const uri of batchCalls) { + responseStatusCollector.push((await request(app).get(uri).send()).status) + } + + expect(responseStatusCollector).toMatchObject(new Array(responseStatusCollector.length).fill(400)) + }) + + it.todo('should respond the desired search result') +}) diff --git a/server/src/routes/api/get/albumSearch.ts b/server/src/routes/api/get/albumSearch.ts index e69de29..a3d5d28 100644 --- a/server/src/routes/api/get/albumSearch.ts +++ b/server/src/routes/api/get/albumSearch.ts @@ -0,0 +1,77 @@ +import { RequestHandler } from 'express' +import { ApiHandler } from '../../../types' + +export interface RawAlbumQueryParams { + term: string + start?: string + nb?: string + ack: number +} + +export interface AlbumSearchParams extends Omit { + start: number + nb: number +} + +export interface AlbumResponse { + data: any[] + total: number + ack: RawAlbumQueryParams['ack'] +} + +const path: ApiHandler['path'] = '/album-search/' + +const handler: RequestHandler<{}, {}, {}, RawAlbumQueryParams> = (req, res, next) => { + if (!req.query) { + res.status(400).send() + next() + } + + const { term, start, nb, ack } = parseQuery(req.query) + + if (!term || term.trim() === '') { + res.status(400).send() + next() + } + + // const albums = getAlbums(term, start, nb) + + // const output: AlbumResponse = { + // data: albums, + // total: albums.length, + // ack + // } + + // res.send(output) + res.send() + next() +} + +const apiHandler = { path, handler } + +export default apiHandler + +function parseQuery(query: RawAlbumQueryParams): AlbumSearchParams { + let startingPoint = 0 + + if (typeof query.start !== 'undefined') { + startingPoint = parseInt(query.start) + } + + let newNb = 30 + + if (typeof query.nb !== 'undefined') { + newNb = parseInt(query.nb) + } + + return { + term: query.term, + start: startingPoint, + nb: newNb, + ack: query.ack + } +} + +// function getAlbums(term: string, start: number, nb: number): any[] { +// return [] +// } diff --git a/server/src/routes/api/get/index.ts b/server/src/routes/api/get/index.ts index a48ec56..21f4ad7 100644 --- a/server/src/routes/api/get/index.ts +++ b/server/src/routes/api/get/index.ts @@ -1,4 +1,4 @@ -import sample from './sample' import getHome from './getHome' +import albumSearch from './albumSearch' -export default [sample, getHome] +export default [albumSearch, getHome] diff --git a/server/src/routes/api/get/sample.ts b/server/src/routes/api/get/sample.ts deleted file mode 100644 index 9bd955e..0000000 --- a/server/src/routes/api/get/sample.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { ApiHandler } from '../../../types' - -const path: ApiHandler['path'] = '/sample' - -const handler: ApiHandler['handler'] = (_, res) => { - res.send('Mandi') -} - -const apiHandler: ApiHandler = { path, handler } - -export default apiHandler diff --git a/server/src/tests/cookie-parser.spec.ts b/server/src/tests/cookie-parser.spec.ts index aa32fb2..a7650f1 100644 --- a/server/src/tests/cookie-parser.spec.ts +++ b/server/src/tests/cookie-parser.spec.ts @@ -4,7 +4,7 @@ import request from 'supertest' import express from 'express' import cookieParser from 'cookie-parser' -describe('request.agent(app)', function () { +describe('cookie parser', () => { const app = express() app.use(cookieParser()) diff --git a/server/src/types.ts b/server/src/types.ts index a6c35aa..69c53ca 100644 --- a/server/src/types.ts +++ b/server/src/types.ts @@ -1,5 +1,8 @@ import { RequestHandler } from 'express' +/* === Utilities === */ +export type WithOptional = Omit & Partial> + export type Port = number | string | boolean export interface ApiHandler {