From 5c3c7d9baf4d9566b15301d9a32d7cdef6d40ca4 Mon Sep 17 00:00:00 2001 From: christiangsn Date: Wed, 8 Mar 2023 18:41:11 -0300 Subject: [PATCH] test: ensure return Result failure if email already registered --- ...signupUseCase.ts => signupUseCase.spec.ts} | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) rename tests/@application/useCases/user/signup/{signupUseCase.ts => signupUseCase.spec.ts} (66%) diff --git a/tests/@application/useCases/user/signup/signupUseCase.ts b/tests/@application/useCases/user/signup/signupUseCase.spec.ts similarity index 66% rename from tests/@application/useCases/user/signup/signupUseCase.ts rename to tests/@application/useCases/user/signup/signupUseCase.spec.ts index e5c911f..6fef7bc 100644 --- a/tests/@application/useCases/user/signup/signupUseCase.ts +++ b/tests/@application/useCases/user/signup/signupUseCase.spec.ts @@ -1,7 +1,8 @@ +import { Result } from '@domain/shared/core' import { SignupUseCases } from '@application/useCases/user/signup/signUpUseCases' import { type IUserRepositoryContract } from '@domain/contracts/repositories/userRepository.contract' -import { OS } from '@domain/user' +import { type OS, UserAggregate } from '@domain/user' import { mock, type MockProxy } from 'jest-mock-extended' export interface IFakerDTO { @@ -18,7 +19,7 @@ export interface IFakerDTO { } describe('SignUpUseCase', () => { - const fakeDTO = (props: IFakerDTO) => { + const fakeDTO = (props?: IFakerDTO) => { return { acceptedTerms: props?.acceptedTerms ?? true, email: props?.email ?? 'johnjoe@example.com', @@ -45,10 +46,22 @@ describe('SignUpUseCase', () => { expect(signupUseCases).toBeDefined() }) - it('Shoudl return fails if not accept the terms', async () => { + it('Should return fails if not accept the terms', async () => { const fakerDTO = fakeDTO({ acceptedTerms: false }) const result = await signupUseCases.execute(fakerDTO) expect(result.isFailure).toBe(true) expect(result.isSuccess).toBe(false) + expect(result.error).toBe('Terms should be accepted') + }) + + it('Shpuld fails if user already exists for provided email', async () => { + jest.spyOn(userRepository, 'exist').mockResolvedValueOnce(true) + const fakerDTO = fakeDTO() + const result = await signupUseCases.execute(fakerDTO) + + expect(userRepository.exist).toHaveBeenCalledWith({ email: fakerDTO.email }) + expect(userRepository.exist).toHaveBeenCalledTimes(1) + expect(result.isFailure).toBe(true) + expect(result.isSuccess).toBe(false) }) })