-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: standardized and updated tests
- Loading branch information
Showing
12 changed files
with
364 additions
and
347 deletions.
There are no files selected for viewing
37 changes: 29 additions & 8 deletions
37
src/features/account/controllers/account-controller.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,56 @@ | ||
import type { NextFunction, Request, Response } from 'express'; | ||
import type { MockProxy } from 'vitest-mock-extended'; | ||
import { mock } from 'vitest-mock-extended'; | ||
import { mock, mockDeep } from 'vitest-mock-extended'; | ||
|
||
import { AccountController } from '@/features/account/controllers/account-controller'; | ||
import type { AccountRepository } from '@/features/account/repositories/account-repository/account-repository'; | ||
import { DeleteUserAccountsService } from '@/features/account/services/delete-user-accounts-service'; | ||
import { HttpError } from '@/shared/errors/http-error'; | ||
import { HttpStatusCode } from '@/shared/protocols/http-client'; | ||
|
||
describe('Account Controller', () => { | ||
let accountRepository: MockProxy<AccountRepository>; | ||
let deleteAccountByIdService: DeleteUserAccountsService; | ||
let accountController: AccountController; | ||
|
||
let req: Request; | ||
let res: Response; | ||
let next: NextFunction; | ||
let error: HttpError; | ||
|
||
beforeEach(() => { | ||
accountRepository = mock<AccountRepository>(); | ||
deleteAccountByIdService = new DeleteUserAccountsService(accountRepository); | ||
accountController = new AccountController(deleteAccountByIdService); | ||
|
||
req = mockDeep<Request>(); | ||
|
||
res = { | ||
json: vi.fn(), | ||
send: vi.fn(), | ||
status: vi.fn().mockReturnThis(), | ||
} as unknown as Response; | ||
|
||
next = vi.fn() as unknown as NextFunction; | ||
|
||
error = new HttpError(HttpStatusCode.serverError, 'error'); | ||
}); | ||
|
||
it('should be able to delete an account by id', async () => { | ||
const req = mock<Request>(); | ||
req.params.id = 'f19c169c-5fa2-406d-8de9-4d2c36dc6529'; | ||
|
||
const res = mock<Response>(); | ||
res.status.mockReturnThis(); | ||
res.send.mockReturnThis(); | ||
await accountController.deleteAccountById(req, res, next); | ||
|
||
const next = mock<NextFunction>(); | ||
expect(res.send).toHaveBeenCalled(); | ||
expect(res.status).toHaveBeenCalledWith(HttpStatusCode.noContent); | ||
expect(res.send).toHaveBeenCalled(); | ||
}); | ||
it('should call next with an error if deleteUserAccountService.execute throws', async () => { | ||
vi.spyOn(deleteAccountByIdService, 'execute').mockRejectedValueOnce(error); | ||
|
||
await accountController.deleteAccountById(req, res, next); | ||
|
||
expect(res.status).toHaveBeenCalledWith(204); | ||
expect(res.send).toHaveBeenCalled(); | ||
expect(next).toHaveBeenCalledWith(error); | ||
expect(next).toBeCalledTimes(1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.