From 17f8070dea11d062c704e4cf5274d4b96b51c0d5 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Sat, 24 Apr 2021 20:13:22 +0200 Subject: [PATCH] feat: implemented album search (currently has a failing test) --- server/src/main.ts | 2 +- server/src/routes/api/get/albumSearch.spec.ts | 15 ++++++++++-- server/src/routes/api/get/albumSearch.ts | 23 ++++++++++--------- server/src/tests/utils.ts | 7 ++++++ 4 files changed, 33 insertions(+), 14 deletions(-) create mode 100644 server/src/tests/utils.ts diff --git a/server/src/main.ts b/server/src/main.ts index eb973ff..7aab7ea 100644 --- a/server/src/main.ts +++ b/server/src/main.ts @@ -2,7 +2,7 @@ import { Deezer } from 'deezer-js' console.log('init!') -const dz = new Deezer() +export const dz = new Deezer() let homeCache: any, chartsCache: any export async function getHome() { diff --git a/server/src/routes/api/get/albumSearch.spec.ts b/server/src/routes/api/get/albumSearch.spec.ts index f872782..6198e5a 100644 --- a/server/src/routes/api/get/albumSearch.spec.ts +++ b/server/src/routes/api/get/albumSearch.spec.ts @@ -1,5 +1,6 @@ import request from 'supertest' import { app } from '../../../app' +import { appSendGet } from '../../../tests/utils' describe('albumSearch requests', () => { it('should respond 200 to calls with term', async () => { @@ -13,7 +14,7 @@ describe('albumSearch requests', () => { ] for (const uri of batchCalls) { - responseStatusCollector.push((await request(app).get(uri).send()).status) + responseStatusCollector.push((await appSendGet(uri)).status) } expect(responseStatusCollector).toMatchObject(new Array(batchCalls.length).fill(200)) @@ -37,5 +38,15 @@ describe('albumSearch requests', () => { expect(responseStatusCollector).toMatchObject(new Array(responseStatusCollector.length).fill(400)) }) - it.todo('should respond the desired search result') + it('should respond the desired search result', async () => { + const res = (await appSendGet('/api/album-search/?term=eminem')).body + + expect(res.data.data.length).not.toBe(0) + }) + + it('should respond the desired search result with a start parameter', async () => { + const res = (await appSendGet('/api/album-search/?term=eminem?start=10')).body + + expect(res.data.data.length).not.toBe(0) + }) }) diff --git a/server/src/routes/api/get/albumSearch.ts b/server/src/routes/api/get/albumSearch.ts index 9366bee..7da830c 100644 --- a/server/src/routes/api/get/albumSearch.ts +++ b/server/src/routes/api/get/albumSearch.ts @@ -1,5 +1,6 @@ import { RequestHandler } from 'express' import { ApiHandler } from '../../../types' +import { dz } from '../../../main' export interface RawAlbumQuery { term: string @@ -21,28 +22,28 @@ export interface AlbumResponse { const path: ApiHandler['path'] = '/album-search/' -const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = (req, res, next) => { +const handler: RequestHandler<{}, {}, {}, RawAlbumQuery> = async (req, res, next) => { if (!req.query) { res.status(400).send() - next() + return next() } - const { term } = parseQuery(req.query) + const { term, start, nb, ack } = parseQuery(req.query) if (!term || term.trim() === '') { res.status(400).send() - next() + return next() } - // const albums = getAlbums(term, start, nb) + const albums = await dz.api.search_album(term, { start, nb }) - // const output: AlbumResponse = { - // data: albums, - // total: albums.length, - // ack - // } + const output: AlbumResponse = { + data: albums, + total: albums.data.length, + ack + } - // res.send(output) + res.send(output) res.send() next() } diff --git a/server/src/tests/utils.ts b/server/src/tests/utils.ts new file mode 100644 index 0000000..748ce95 --- /dev/null +++ b/server/src/tests/utils.ts @@ -0,0 +1,7 @@ +import { Application } from 'express' +import request from 'supertest' +import { app } from '../app' + +export const sendGet = (app: Application) => (uri: string) => request(app).get(uri).send() + +export const appSendGet = sendGet(app)