|
1 |
| -import type { NextFunction, Request, Response } from 'express'; |
2 |
| -import type { MockProxy } from 'vitest-mock-extended'; |
3 |
| -import { mock } from 'vitest-mock-extended'; |
| 1 | +import type { Request, Response } from 'express'; |
| 2 | +import { mock, mockDeep } from 'vitest-mock-extended'; |
4 | 3 |
|
5 | 4 | import { AccountController } from '@/features/account/controllers/account-controller';
|
6 |
| -import type { AccountRepository } from '@/features/account/repositories/account-repository/account-repository'; |
7 |
| -import { DeleteUserAccountsService } from '@/features/account/services/delete-user-accounts-service'; |
8 |
| - |
9 |
| -describe('Account Controller', () => { |
10 |
| - let accountRepository: MockProxy<AccountRepository>; |
11 |
| - let deleteAccountByIdService: DeleteUserAccountsService; |
12 |
| - let accountController: AccountController; |
13 |
| - |
14 |
| - beforeEach(() => { |
15 |
| - accountRepository = mock<AccountRepository>(); |
16 |
| - deleteAccountByIdService = new DeleteUserAccountsService(accountRepository); |
17 |
| - accountController = new AccountController(deleteAccountByIdService); |
| 5 | +import type { DeleteUserAccountsService } from '@/features/account/services/delete-user-accounts-service'; |
| 6 | +import { accountDeleteBySchema } from '@/features/account/validators/account-find-by-id-schema'; |
| 7 | +import { HttpError } from '@/shared/errors/http-error'; |
| 8 | +import { HttpStatusCode } from '@/shared/protocols/http-client'; |
| 9 | + |
| 10 | +const makeSut = () => { |
| 11 | + const deleteUserAccountServiceMock = mock<DeleteUserAccountsService>({ |
| 12 | + execute: vi.fn(), |
18 | 13 | });
|
19 | 14 |
|
20 |
| - it('should be able to delete an account by id', async () => { |
21 |
| - const req = mock<Request>(); |
22 |
| - req.params.id = 'f19c169c-5fa2-406d-8de9-4d2c36dc6529'; |
| 15 | + const accountController = new AccountController(deleteUserAccountServiceMock); |
| 16 | + |
| 17 | + const req = mockDeep<Request>(); |
| 18 | + |
| 19 | + const res = { |
| 20 | + send: vi.fn(), |
| 21 | + |
| 22 | + status: vi.fn().mockReturnThis(), |
| 23 | + } as unknown as Response; |
| 24 | + |
| 25 | + const next = vi.fn(); |
| 26 | + |
| 27 | + return { |
| 28 | + accountController, |
| 29 | + |
| 30 | + deleteUserAccountService: deleteUserAccountServiceMock, |
23 | 31 |
|
24 |
| - const res = mock<Response>(); |
25 |
| - res.status.mockReturnThis(); |
26 |
| - res.send.mockReturnThis(); |
| 32 | + next, |
27 | 33 |
|
28 |
| - const next = mock<NextFunction>(); |
| 34 | + req, |
| 35 | + |
| 36 | + res, |
| 37 | + }; |
| 38 | +}; |
| 39 | + |
| 40 | +describe('deleteAccountById', () => { |
| 41 | + it('should delete an account and return no content status', async () => { |
| 42 | + const { accountController, deleteUserAccountService, next, req, res } = |
| 43 | + makeSut(); |
| 44 | + |
| 45 | + const socialMediaId = '123'; |
| 46 | + |
| 47 | + req.params = { id: socialMediaId }; |
| 48 | + |
| 49 | + const executeSpy = vi |
| 50 | + .spyOn(deleteUserAccountService, 'execute') |
| 51 | + .mockResolvedValue(); |
29 | 52 |
|
30 | 53 | await accountController.deleteAccountById(req, res, next);
|
31 | 54 |
|
32 |
| - expect(res.status).toHaveBeenCalledWith(204); |
| 55 | + expect(executeSpy).toHaveBeenCalledWith({ |
| 56 | + socialMediaId: Number(socialMediaId), |
| 57 | + }); |
| 58 | + |
| 59 | + expect(res.status).toHaveBeenCalledWith(HttpStatusCode.noContent); |
| 60 | + |
33 | 61 | expect(res.send).toHaveBeenCalled();
|
| 62 | + |
| 63 | + expect(next).not.toHaveBeenCalled(); |
| 64 | + }); |
| 65 | + |
| 66 | + it('should call next with an error when service throws', async () => { |
| 67 | + const { accountController, deleteUserAccountService, next, req, res } = |
| 68 | + makeSut(); |
| 69 | + |
| 70 | + const socialMediaId = '123'; |
| 71 | + req.params = { id: socialMediaId }; |
| 72 | + |
| 73 | + const error = new HttpError(HttpStatusCode.badRequest, 'Service error'); |
| 74 | + |
| 75 | + vi.spyOn(deleteUserAccountService, 'execute').mockRejectedValueOnce(error); |
| 76 | + |
| 77 | + await accountController.deleteAccountById(req, res, next); |
| 78 | + |
| 79 | + expect(next).toHaveBeenCalledWith(error); |
| 80 | + |
| 81 | + expect(res.status).not.toHaveBeenCalled(); |
| 82 | + expect(res.send).not.toHaveBeenCalled(); |
| 83 | + }); |
| 84 | + |
| 85 | + describe('accountDeleteByschema', () => { |
| 86 | + it('should pass validation when id is a valid number', () => { |
| 87 | + const validInput = { id: 123 }; |
| 88 | + const parsedData = accountDeleteBySchema.parse(validInput); |
| 89 | + |
| 90 | + expect(parsedData).toEqual({ id: validInput.id }); |
| 91 | + }); |
34 | 92 | });
|
35 | 93 | });
|
0 commit comments