-
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: test and implemented repo findByEmail
- Loading branch information
Showing
4 changed files
with
59 additions
and
51 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,27 @@ | ||
import type { UserRepository } from '@/features/user/repositories/user-repository'; | ||
import { UserCreateService } from '@/features/user/services/user-create-service'; | ||
import { BcryptAdapter } from '@/shared/infra/crypto/bcrypt-adapter'; | ||
import { mock } from 'vitest-mock-extended'; | ||
|
||
// TODO: Refactor | ||
const makeSut = () => { | ||
class UserRepositoryStub implements UserRepository { | ||
create({ email, name, password, username }: any) { | ||
return Promise.resolve({ | ||
createdAt: new Date(2024, 5, 1), | ||
deletedAt: null, | ||
email, | ||
id: 'valid_id', | ||
isActive: true, | ||
name, | ||
password, | ||
updatedAt: new Date(2024, 5, 1), | ||
username, | ||
}); | ||
} | ||
import type { UserRepository } from '@/features/user/repositories/user-repository'; | ||
import type { BcryptAdapter } from '@/shared/infra/crypto/bcrypt-adapter'; | ||
import { bcryptAdapteMock } from '@/shared/test-helpers/mocks/bcryptAdapter.mock'; | ||
import { userRepositoryMock } from '@/shared/test-helpers/mocks/repositories/user-repository.mock'; | ||
|
||
findById(id: string): Promise<{ | ||
email: string; | ||
id: string; | ||
name: null | string; | ||
username: string; | ||
} | null> { | ||
throw new Error('Method not implemented. ' + id); | ||
} | ||
updateIsActiveStatus(_: string): Promise<void> { | ||
throw new Error('Method not implemented.'); | ||
} | ||
} | ||
import { UserCreateService } from './user-create-service'; | ||
|
||
const userRepository = new UserRepositoryStub(); | ||
let userCreateService: UserCreateService; | ||
|
||
const bcryptAdapter = new BcryptAdapter(); | ||
let userRepository = mock<UserRepository>(userRepositoryMock); | ||
|
||
const userCreateService = new UserCreateService( | ||
userRepository, | ||
bcryptAdapter | ||
); | ||
let bcryptAdapter = mock<BcryptAdapter>(bcryptAdapteMock); | ||
|
||
return { bcryptAdapter, userCreateService, userRepository }; | ||
}; | ||
beforeEach(() => { | ||
userCreateService = new UserCreateService(userRepository, bcryptAdapter); | ||
}); | ||
|
||
describe('UserCreateService', () => { | ||
it('should call userRepository with correct params', async () => { | ||
const { bcryptAdapter, userCreateService, userRepository } = makeSut(); | ||
|
||
const repositorySpy = vi.spyOn(userRepository, 'create'); | ||
|
||
vi.spyOn(bcryptAdapter, 'encrypt').mockImplementationOnce( | ||
async () => 'valid_password' | ||
); | ||
vi.spyOn(bcryptAdapter, 'encrypt').mockResolvedValue('valid_password'); | ||
|
||
await userCreateService.execute({ | ||
email: '[email protected]', | ||
|
@@ -71,11 +40,7 @@ describe('UserCreateService', () => { | |
}); | ||
|
||
it('should throw when userRepository throws', async () => { | ||
const { userCreateService, userRepository } = makeSut(); | ||
|
||
vi.spyOn(userRepository, 'create').mockImplementationOnce(async () => { | ||
throw new Error('error'); | ||
}); | ||
vi.spyOn(userRepository, 'create').mockRejectedValue(new Error('error')); | ||
|
||
const response = userCreateService.execute({ | ||
email: '[email protected]', | ||
|
@@ -89,8 +54,6 @@ describe('UserCreateService', () => { | |
}); | ||
|
||
it('should conflict when password and repeatPassword dont match', async () => { | ||
const { userCreateService } = makeSut(); | ||
|
||
const response = userCreateService.execute({ | ||
email: '[email protected]', | ||
name: 'valid_name', | ||
|
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { vi } from 'vitest'; | ||
|
||
export const bcryptAdapteMock = { | ||
compare: vi.fn(), | ||
encrypt: vi.fn(), | ||
}; |
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