Skip to content

Commit

Permalink
feat: create and test userFindById controller
Browse files Browse the repository at this point in the history
  • Loading branch information
nathalia-84 committed May 14, 2024
1 parent f639b59 commit 08437d3
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 9 deletions.
56 changes: 51 additions & 5 deletions src/features/user/controllers/user-controller.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ const makeSut = () => {
res,
userController,
userCreateService,
userFindByIdService,
validator,
};
};
Expand Down Expand Up @@ -126,19 +127,64 @@ describe('[Controllers] UserController', () => {

const validateSpy = vi.spyOn(validator, 'validate');

req.body = {};
req.params = { id: 'valid_id' };
const uuid = crypto.randomUUID();

req.params = { id: uuid };
req.path = '/users';
req.query = {};

await userController.userFindById(req, res, next);

expect(validateSpy).toHaveBeenCalledWith(expect.anything(), {
body: req.body,
params: req.params,
path: req.path,
query: req.query,
});
});

it('should call service with correctly params', async () => {
const { next, req, res, userController, userFindByIdService } = makeSut();

const serviceSpy = vi.spyOn(userFindByIdService, 'execute');

const uuid = crypto.randomUUID();

req.params.id = uuid;

await userController.userFindById(req, res, next);

expect(serviceSpy).toHaveBeenCalledWith({
id: req.params.id,
});
});

it('should response 404 if user is not found', async () => {
const { next, req, res, userController, userFindByIdService } = makeSut();

const serviceSpy = vi.spyOn(userFindByIdService, 'execute');

const response = undefined;

serviceSpy.mockReturnValue(response);

const uuid = crypto.randomUUID();

req.params.id = uuid;

await userController.userFindById(req, res, next);

expect(res.status).toHaveBeenCalledWith(404);
});

it('should call next when an error', async () => {
const { next, req, res, userController, userFindByIdService } = makeSut();
const error = new HttpError(500, 'error');

vi.spyOn(userFindByIdService, 'execute').mockRejectedValueOnce(
new HttpError(500, 'error')
);

await userController.userFindById(req, res, next);

expect(next).toHaveBeenCalledWith(error);
});
});
});
2 changes: 0 additions & 2 deletions src/features/user/controllers/user-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,8 @@ export class UserController implements Controller {
userFindById: AsyncRequestHandler = async (req, res, next) => {
try {
this.validator.validate(userFindByIdSchema, {
body: req.body,
params: req.params,
path: req.path,
query: req.query,
});

const user = await this.serviceFindById.execute({
Expand Down
2 changes: 0 additions & 2 deletions src/features/user/validators/user-find-by-id-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ export const userFindByIdParamsSchema = Joi.object({
});

export const userFindByIdSchema = Joi.object({
body: Joi.object().required(),
params: userFindByIdParamsSchema,
path: Joi.string().required(),
query: Joi.object().required(),
});

0 comments on commit 08437d3

Please sign in to comment.