Skip to content

Commit

Permalink
fix: 🐛 remove null type in getUser response
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 17, 2024
1 parent 9504bdb commit 2a0a169
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -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<GetUserByIdUseCase>;

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>(UsersResolver);
service = module.get<UsersService>(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: '[email protected]',
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');
});
});
});
14 changes: 6 additions & 8 deletions libs/users/interface-adapters/src/lib/resolver/users.resolver.ts
Original file line number Diff line number Diff line change
@@ -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<UserDto | null> {
const user: User| null = await this.usersService.findOneById(id);

if (!user) {
return null;
}
async getUser(
@Args({ name: 'id', type: () => ID }) id: string,
): Promise<UserDto> {
const user: User = await this.getUserByIdUseCase.execute(id);

return new UserDto(user.id, user.email, user.firstName, user.lastName);
}
Expand Down
7 changes: 5 additions & 2 deletions libs/users/interface-adapters/src/lib/users.module.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -16,7 +20,6 @@ import { UsersResolver } from './resolver/users.resolver';
UsersResolver,
UsersService,
GetUserByIdUseCase,
GetUserByEmailUseCase,
{
provide: USERS_REPOSITORY,
useClass: MongooseUsersRepository,
Expand Down

0 comments on commit 2a0a169

Please sign in to comment.