feat: implemented album search (currently has a failing test)

This commit is contained in:
Roberto Tonino 2021-04-24 20:13:22 +02:00
parent 6f75b17bdb
commit 17f8070dea
4 changed files with 33 additions and 14 deletions

View file

@ -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() {

View file

@ -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)
})
})

View file

@ -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()
}

View file

@ -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)