Skip to content

Controllers

Testing controllers can be achieved by simply instantiating the controller and calling the method.

demo-http/tests/controller.test.ts
import 'reflect-metadata';
import { UsersController } from '../src/controllers/users.js';
import { UserNotFoundError } from '../src/errors/user-not-found.js';
import { UsersService } from '../src/services/users.js';

let service: UsersService;
let controller : UsersController;

beforeEach(() => {
  service = new UsersService();
  controller = new UsersController(
    service
  );
});

it('Throws error when /:name is not found', () => {
  expect(() => controller.get('amaury')).toThrowError(UserNotFoundError);
});

it('Returns the user when /:name is found', () => {
  const user = {
    name: 'amaury'
  };
  service.users.push(user);
  expect(controller.get(user.name)).toStrictEqual(user);
});
demo-http/src/controllers/users.ts
import { inject } from '@triptyk/nfw-core';
import { Controller, GET, Param, POST, UseGuard, UseResponseHandler } from '@triptyk/nfw-http';
import { UserNotFoundError } from '../errors/user-not-found.js';
import { AuthGuard } from '../guards/guard.js';
import { ValidatedBody } from '../params/validated-body.js';
import { RestResponseHandler } from '../response-handlers/rest.js';
import { User, UsersService } from '../services/users.js';
import { userSchema } from '../validations/user.js';

@Controller({
  routeName: '/users'
})
@UseResponseHandler(RestResponseHandler)
@UseGuard(AuthGuard)
export class UsersController {
  public constructor (
    @inject(UsersService) public usersService: UsersService
  ) {}

  @GET('/:name')
  public get (@Param('name') param: string) {
    const user = this.usersService.findOne(param);
    if (!user) {
      throw new UserNotFoundError();
    }
    return user;
  }

  @POST('/')
  public create (@ValidatedBody(userSchema) body: User) {
    const user = this.usersService.createOne(body);
    return user;
  }
}