|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- import { FastifyInstance } from 'fastify';
- import {
- describe,
- it,
- expect,
- beforeAll,
- afterAll,
- vi,
- } from 'vitest';
- import { constants } from 'http2';
- import { createServer } from '../src/server';
- import { addRoutes } from '../src/routes';
- import {
- UuidServiceImpl,
- InvalidArgumentTypeError,
- } from '../src/modules/uuid';
-
- describe('Example', () => {
- let server: FastifyInstance;
- const body = { username: 'username', onlineMode: false };
-
- beforeAll(() => {
- server = createServer();
- addRoutes(server);
- });
-
- afterAll(async () => {
- await server.close();
- });
-
- it('returns result when successful', async () => {
- const response = await server
- .inject()
- .post('/api/uuid')
- .body(body)
- .headers({
- 'Accept': 'application/json',
- });
- expect(response.statusCode).toBe(constants.HTTP_STATUS_OK);
- });
-
- it('returns error when given invalid inputs', async () => {
- vi.spyOn(UuidServiceImpl.prototype, 'getUsernameUuid').mockImplementationOnce(() => {
- throw new InvalidArgumentTypeError('Invalid input');
- });
-
- const response = await server
- .inject()
- .post('/api/uuid')
- .body(body)
- .headers({
- 'Accept': 'application/json',
- });
- expect(response.statusCode).toBe(constants.HTTP_STATUS_BAD_REQUEST);
- });
-
- it('returns error when an unexpected error occurs', async () => {
- vi.spyOn(UuidServiceImpl.prototype, 'getUsernameUuid').mockImplementationOnce(() => {
- throw new Error('Unexpected error');
- });
-
- const response = await server
- .inject()
- .post('/api/uuid')
- .body(body)
- .headers({
- 'Accept': 'application/json',
- });
- expect(response.statusCode).toBe(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR);
- });
- });
|