Skip to content

Commit

Permalink
feat: ✨ getUserUseCase in UsersService
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Nov 19, 2024
1 parent a1bdfdb commit 66310ac
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 53 deletions.
18 changes: 12 additions & 6 deletions libs/users/application/src/lib/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,19 @@ describe('UsersService', () => {
expect(service).toBeDefined();
});

it('should return a user by id', () => {
const user: User | undefined = service.findById('1');
expect(user).toEqual({ id: '1', name: 'John Doe' });
it('should return a user by id', async () => {
const user = new User('1', 'John Doe');
jest.spyOn(service, 'findById').mockResolvedValue(user);

const result = await service.findById('1');
expect(result).toEqual(user);
});

it('should return undefined if user is not found', () => {
const user: User | undefined = service.findById('3');
expect(user).toBeUndefined();
it('should return undefined if user is not found', async () => {
jest.spyOn(service, 'findById').mockResolvedValue(null);

const result = await service.findById('1');
expect(result).toBeNull();

});
});
11 changes: 5 additions & 6 deletions libs/users/application/src/lib/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { Injectable } from '@nestjs/common';
import { User } from '@users/domain';

import { GetUserUseCase } from './use-cases/get-user.use-case';

@Injectable()
export class UsersService {
private users: User[] = [
{ id: '1', name: 'John Doe' },
{ id: '2', name: 'Richard Roe' },
];
constructor(private readonly getUserUseCase: GetUserUseCase) {}

findById(id: string): User | undefined {
return this.users.find((user) => user.id === id);
async findById(id: string): Promise<User | null> {
return this.getUserUseCase.execute(id);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,40 +30,19 @@ describe('UsersResolver', () => {
});

describe('getUser', () => {
it('should return a user by id', () => {
const user: User = { id: '1', name: 'John Doe' };
jest.spyOn(service, 'findById').mockReturnValue(user);
it('should return a user by id', async () => {
const user = new User('1', 'John Doe');
jest.spyOn(service, 'findById').mockResolvedValue(user);

expect(resolver.getUser('1')).toEqual(user);
expect(service.findById).toHaveBeenCalledWith('1');
const result = await resolver.getUser('1');
expect(result).toEqual(user);
});

it('should return undefined if user not found', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);
it('should return undefined if user not found', async () => {
jest.spyOn(service, 'findById').mockResolvedValue(null);

expect(resolver.getUser('2')).toBeNull();
expect(service.findById).toHaveBeenCalledWith('2');
});
});

describe('resolveReference', () => {
it('should return a user by reference id', () => {
const user: User = { id: '1', name: 'John Doe' };
jest.spyOn(service, 'findById').mockReturnValue(user);

expect(
resolver.resolveReference({ __typename: 'User', id: '1' }),
).toEqual(user);
expect(service.findById).toHaveBeenCalledWith('1');
});

it('should return undefined if user not found by reference id', () => {
jest.spyOn(service, 'findById').mockReturnValue(undefined);

expect(
resolver.resolveReference({ __typename: 'User', id: '2' }),
).toBeUndefined();
expect(service.findById).toHaveBeenCalledWith('2');
const result = await resolver.getUser('1');
expect(result).toBeNull();
});
});
});
13 changes: 2 additions & 11 deletions libs/users/interface-adapters/src/lib/resolver/users.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@ export class UsersResolver {
constructor(private usersService: UsersService) {}

@Query(() => UserDto, { nullable: true })
getUser(@Args({ name: 'id', type: () => ID }) id: string): UserDto | null {
const user: User | undefined = this.usersService.findById(id); // Domain entity
return user ? new UserDto(user.id, user.name) : null;
}

@ResolveReference()
resolveReference(reference: {
__typename: string;
id: string;
}): User | undefined {
return this.usersService.findById(reference.id);
getUser(@Args({ name: 'id', type: () => ID }) id: string): Promise<UserDto | null> {
return this.usersService.findById(id);
}
}

0 comments on commit 66310ac

Please sign in to comment.