test: added albumSearch test; chore: removed sample endpoint
This commit is contained in:
parent
e2c79f6ee6
commit
418fc5647f
|
@ -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",
|
||||
|
|
|
@ -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')
|
||||
})
|
|
@ -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<RawAlbumQueryParams, 'start' | 'nb'> {
|
||||
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 []
|
||||
// }
|
|
@ -1,4 +1,4 @@
|
|||
import sample from './sample'
|
||||
import getHome from './getHome'
|
||||
import albumSearch from './albumSearch'
|
||||
|
||||
export default [sample, getHome]
|
||||
export default [albumSearch, getHome]
|
||||
|
|
|
@ -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
|
|
@ -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())
|
||||
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
import { RequestHandler } from 'express'
|
||||
|
||||
/* === Utilities === */
|
||||
export type WithOptional<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
|
||||
|
||||
export type Port = number | string | boolean
|
||||
|
||||
export interface ApiHandler {
|
||||
|
|
Loading…
Reference in New Issue