From 2a0a169cbbd5cfad42e2f96874d236afbad46382 Mon Sep 17 00:00:00 2001 From: zhumeisongsong Date: Tue, 17 Dec 2024 13:19:38 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20=F0=9F=90=9B=20remove=20null=20type=20?= =?UTF-8?q?=20in=20getUser=20response?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/lib/resolver/users.resolver.spec.ts | 48 +++++++++++++++---- .../src/lib/resolver/users.resolver.ts | 14 +++--- .../src/lib/users.module.ts | 7 ++- 3 files changed, 51 insertions(+), 18 deletions(-) diff --git a/libs/users/interface-adapters/src/lib/resolver/users.resolver.spec.ts b/libs/users/interface-adapters/src/lib/resolver/users.resolver.spec.ts index da6aed2..85eadb8 100644 --- a/libs/users/interface-adapters/src/lib/resolver/users.resolver.spec.ts +++ b/libs/users/interface-adapters/src/lib/resolver/users.resolver.spec.ts @@ -1,30 +1,62 @@ import { Test, TestingModule } from '@nestjs/testing'; -import { UsersService } from '@users/application'; - +import { GetUserByIdUseCase } from '@users/application'; import { UsersResolver } from './users.resolver'; +import { UserDto } from '../dto/user.dto'; describe('UsersResolver', () => { let resolver: UsersResolver; - let service: UsersService; + let getUserByIdUseCase: jest.Mocked; beforeEach(async () => { + getUserByIdUseCase = { + execute: jest.fn(), + } as any; + const module: TestingModule = await Test.createTestingModule({ providers: [ UsersResolver, { - provide: UsersService, - useValue: { - findOneById: jest.fn(), - }, + provide: GetUserByIdUseCase, + useValue: getUserByIdUseCase, }, ], }).compile(); resolver = module.get(UsersResolver); - service = module.get(UsersService); }); it('should be defined', () => { expect(resolver).toBeDefined(); }); + + describe('getUser', () => { + it('should return a UserDto when user is found', async () => { + const mockUser = { + id: '1', + email: 'test@test.com', + firstName: 'John', + lastName: 'Doe' + }; + getUserByIdUseCase.execute.mockResolvedValue(mockUser); + + const result = await resolver.getUser('1'); + + expect(result).toBeInstanceOf(UserDto); + expect(result).toEqual(new UserDto( + mockUser.id, + mockUser.email, + mockUser.firstName, + mockUser.lastName + )); + expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1'); + }); + + it('should propagate errors from use case', async () => { + const error = new Error('Test error'); + getUserByIdUseCase.execute.mockRejectedValue(error); + + await expect(resolver.getUser('1')).rejects.toThrow(error); + expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1'); + }); + }); }); diff --git a/libs/users/interface-adapters/src/lib/resolver/users.resolver.ts b/libs/users/interface-adapters/src/lib/resolver/users.resolver.ts index ca099db..5078493 100644 --- a/libs/users/interface-adapters/src/lib/resolver/users.resolver.ts +++ b/libs/users/interface-adapters/src/lib/resolver/users.resolver.ts @@ -1,20 +1,18 @@ import { Args, ID, Query, Resolver } from '@nestjs/graphql'; -import { UsersService } from '@users/application'; +import { GetUserByIdUseCase } from '@users/application'; import { User } from '@users/domain'; import { UserDto } from '../dto/user.dto'; @Resolver(() => UserDto) export class UsersResolver { - constructor(private usersService: UsersService) {} + constructor(private readonly getUserByIdUseCase: GetUserByIdUseCase) {} @Query(() => UserDto, { nullable: true }) - async getUser(@Args({ name: 'id', type: () => ID }) id: string): Promise { - const user: User| null = await this.usersService.findOneById(id); - - if (!user) { - return null; - } + async getUser( + @Args({ name: 'id', type: () => ID }) id: string, + ): Promise { + const user: User = await this.getUserByIdUseCase.execute(id); return new UserDto(user.id, user.email, user.firstName, user.lastName); } diff --git a/libs/users/interface-adapters/src/lib/users.module.ts b/libs/users/interface-adapters/src/lib/users.module.ts index 6bb1454..c8c5285 100644 --- a/libs/users/interface-adapters/src/lib/users.module.ts +++ b/libs/users/interface-adapters/src/lib/users.module.ts @@ -1,6 +1,10 @@ import { Module } from '@nestjs/common'; import { DatabaseModule } from '@shared/infrastructure-mongoose'; -import { UsersService, GetUserByIdUseCase, GetUserByEmailUseCase } from '@users/application'; +import { + UsersService, + GetUserByIdUseCase, + +} from '@users/application'; import { MongooseUsersRepository, UserDocument, @@ -16,7 +20,6 @@ import { UsersResolver } from './resolver/users.resolver'; UsersResolver, UsersService, GetUserByIdUseCase, - GetUserByEmailUseCase, { provide: USERS_REPOSITORY, useClass: MongooseUsersRepository,