2022-11-27 15:59:20 +01:00
|
|
|
import axios from 'axios'
|
2022-11-27 16:14:56 +01:00
|
|
|
import server from '../../../src/server/index'
|
|
|
|
|
import * as scheduler from '../../../src/common/scheduler'
|
|
|
|
|
import * as utils from '../../../src/server/utils'
|
2022-11-27 15:59:20 +01:00
|
|
|
|
|
|
|
|
const port = Math.round(Math.random() * 50000 + 10000)
|
|
|
|
|
const baseUrl = `http://localhost:${port}`
|
|
|
|
|
|
|
|
|
|
describe('server', () => {
|
|
|
|
|
const consoleLogSpy = jest.fn()
|
|
|
|
|
const consoleErrorSpy = jest.fn()
|
|
|
|
|
|
|
|
|
|
beforeAll(() => {
|
|
|
|
|
server.listen(port)
|
|
|
|
|
console.log = consoleLogSpy
|
|
|
|
|
console.error = consoleErrorSpy
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
afterAll(() => {
|
|
|
|
|
server.close()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
beforeEach(() => {
|
|
|
|
|
jest.resetAllMocks()
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-27 16:09:21 +01:00
|
|
|
test('should accept POST to /reservations', async () => {
|
2022-11-27 15:59:20 +01:00
|
|
|
jest
|
|
|
|
|
.spyOn(scheduler, 'work')
|
|
|
|
|
.mockImplementationOnce(() => Promise.resolve({}))
|
|
|
|
|
const response = await axios.post(
|
|
|
|
|
`${baseUrl}/reservations`,
|
|
|
|
|
{},
|
|
|
|
|
{ headers: { 'content-type': 'application/json' } }
|
|
|
|
|
)
|
|
|
|
|
expect(response.status).toBe(200)
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-27 16:09:21 +01:00
|
|
|
test('should reject non-POST request', async () => {
|
2022-11-27 15:59:20 +01:00
|
|
|
jest
|
|
|
|
|
.spyOn(scheduler, 'work')
|
|
|
|
|
.mockImplementationOnce(() => Promise.resolve({}))
|
|
|
|
|
await expect(() => axios.get(`${baseUrl}/reservations`)).rejects.toThrow(
|
|
|
|
|
axios.AxiosError
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-27 16:09:21 +01:00
|
|
|
test('should reject request to other route', async () => {
|
2022-11-27 15:59:20 +01:00
|
|
|
jest
|
|
|
|
|
.spyOn(scheduler, 'work')
|
|
|
|
|
.mockImplementationOnce(() => Promise.resolve({}))
|
|
|
|
|
await expect(() => axios.post(`${baseUrl}/something-else`)).rejects.toThrow(
|
|
|
|
|
axios.AxiosError
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-27 16:09:21 +01:00
|
|
|
test('should reject request without content-type of json', async () => {
|
2022-11-27 15:59:20 +01:00
|
|
|
jest
|
|
|
|
|
.spyOn(scheduler, 'work')
|
|
|
|
|
.mockImplementationOnce(() => Promise.resolve({}))
|
|
|
|
|
await expect(() =>
|
|
|
|
|
axios.post(`${baseUrl}/reservations`, 'test,123', {
|
|
|
|
|
headers: { 'content-type': 'text/csv' },
|
|
|
|
|
})
|
|
|
|
|
).rejects.toThrow(axios.AxiosError)
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-27 16:09:21 +01:00
|
|
|
test('should reject request if body cannot be parsed', async () => {
|
2022-11-27 15:59:20 +01:00
|
|
|
jest.spyOn(utils, 'parseJson').mockImplementationOnce(Promise.reject)
|
|
|
|
|
await expect(() =>
|
|
|
|
|
axios.post(
|
|
|
|
|
`${baseUrl}/reservations`,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
).rejects.toThrow(axios.AxiosError)
|
|
|
|
|
})
|
|
|
|
|
|
2022-11-27 16:09:21 +01:00
|
|
|
test('should reject request if schedule cannot be performed', async () => {
|
2022-11-27 15:59:20 +01:00
|
|
|
jest.spyOn(scheduler, 'work').mockImplementationOnce(Promise.reject)
|
|
|
|
|
await expect(() =>
|
|
|
|
|
axios.post(
|
|
|
|
|
`${baseUrl}/reservations`,
|
|
|
|
|
{},
|
|
|
|
|
{
|
|
|
|
|
headers: { 'content-type': 'application/json' },
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
).rejects.toThrow(axios.AxiosError)
|
|
|
|
|
})
|
|
|
|
|
})
|