50 lines
1.3 KiB
TypeScript
50 lines
1.3 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import type { Response } from 'express'
|
|
import { created, noContent, ok } from './index'
|
|
|
|
function createResponseStub() {
|
|
const res = {
|
|
status: vi.fn(),
|
|
json: vi.fn(),
|
|
end: vi.fn(),
|
|
}
|
|
|
|
res.status.mockReturnValue(res)
|
|
res.json.mockReturnValue(res)
|
|
res.end.mockReturnValue(res)
|
|
|
|
return res as unknown as Response & typeof res
|
|
}
|
|
|
|
describe('http/respond helpers', () => {
|
|
it('ok responds with the expected data envelope', () => {
|
|
const res = createResponseStub()
|
|
const payload = { id: 'vehicle_1', name: 'Dacia Logan' }
|
|
|
|
ok(res, payload)
|
|
|
|
expect(res.status).not.toHaveBeenCalled()
|
|
expect(res.json).toHaveBeenCalledWith({ data: payload })
|
|
})
|
|
|
|
it('created responds with status 201 and the expected data envelope', () => {
|
|
const res = createResponseStub()
|
|
const payload = { id: 'reservation_1' }
|
|
|
|
created(res, payload)
|
|
|
|
expect(res.status).toHaveBeenCalledWith(201)
|
|
expect(res.json).toHaveBeenCalledWith({ data: payload })
|
|
})
|
|
|
|
it('noContent responds with status 204 and no body', () => {
|
|
const res = createResponseStub()
|
|
|
|
noContent(res)
|
|
|
|
expect(res.status).toHaveBeenCalledWith(204)
|
|
expect(res.end).toHaveBeenCalledWith()
|
|
expect(res.json).not.toHaveBeenCalled()
|
|
})
|
|
})
|