Skip to content

Commit

Permalink
refactor: ♻️ name from find to findOne
Browse files Browse the repository at this point in the history
  • Loading branch information
zhumeisongsong committed Dec 9, 2024
1 parent d171615 commit 1c6aa0a
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe('GetUserByEmailUseCase', () => {

beforeEach(() => {
usersRepository = {
findById: jest.fn(),
findByEmail: jest.fn(),
findOneById: jest.fn(),
findOneByEmail: jest.fn(),
};
getUserByEmailUseCase = new GetUserByEmailUseCase(usersRepository);
});
Expand All @@ -21,21 +21,25 @@ describe('GetUserByEmailUseCase', () => {
firstName: 'John',
lastName: 'Doe',
};
(usersRepository.findByEmail as jest.Mock).mockResolvedValue(mockUser);
(usersRepository.findOneByEmail as jest.Mock).mockResolvedValue(mockUser);

const result = await getUserByEmailUseCase.execute('[email protected]');

expect(result).toEqual(mockUser);
expect(usersRepository.findByEmail).toHaveBeenCalledWith("[email protected]");
expect(usersRepository.findOneByEmail).toHaveBeenCalledWith(
'[email protected]',
);
});

it('should return null when user not found', async () => {
(usersRepository.findByEmail as jest.Mock).mockResolvedValue(null);
(usersRepository.findOneByEmail as jest.Mock).mockResolvedValue(null);

const result = await getUserByEmailUseCase.execute('[email protected]');

expect(result).toBeNull();
expect(usersRepository.findByEmail).toHaveBeenCalledWith("[email protected]");
expect(usersRepository.findOneByEmail).toHaveBeenCalledWith(
'[email protected]',
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ describe('GetUserByIdUseCase', () => {

beforeEach(() => {
usersRepository = {
findById: jest.fn(),
findByEmail: jest.fn(),
findOneById: jest.fn(),
findOneByEmail: jest.fn(),
};
getUserByIdUseCase = new GetUserByIdUseCase(usersRepository);
});
Expand All @@ -21,21 +21,21 @@ describe('GetUserByIdUseCase', () => {
firstName: 'John',
lastName: 'Doe',
};
(usersRepository.findById as jest.Mock).mockResolvedValue(mockUser);
(usersRepository.findOneById as jest.Mock).mockResolvedValue(mockUser);

const result = await getUserByIdUseCase.execute('1');

expect(result).toEqual(mockUser);
expect(usersRepository.findById).toHaveBeenCalledWith('1');
expect(usersRepository.findOneById).toHaveBeenCalledWith('1');
});

it('should return null when user not found', async () => {
(usersRepository.findById as jest.Mock).mockResolvedValue(null);
(usersRepository.findOneById as jest.Mock).mockResolvedValue(null);

const result = await getUserByIdUseCase.execute('1');

expect(result).toBeNull();
expect(usersRepository.findById).toHaveBeenCalledWith('1');
expect(usersRepository.findOneById).toHaveBeenCalledWith('1');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ export class GetUserByIdUseCase {
) {}

async execute(id: string): Promise<User | null> {
return await this.usersRepository.findById(id);
return await this.usersRepository.findOneById(id);
}
}
4 changes: 2 additions & 2 deletions libs/users/application/src/lib/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('UsersService', () => {
};
(getUserByIdUseCase.execute as jest.Mock).mockResolvedValue(mockUser);

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

expect(result).toEqual(mockUser);
expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1');
Expand All @@ -51,7 +51,7 @@ describe('UsersService', () => {
it('should return null when user not found', async () => {
(getUserByIdUseCase.execute as jest.Mock).mockResolvedValue(null);

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

expect(result).toBeNull();
expect(getUserByIdUseCase.execute).toHaveBeenCalledWith('1');
Expand Down
4 changes: 2 additions & 2 deletions libs/users/application/src/lib/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export class UsersService {
private readonly getUserByEmailUseCase: GetUserByEmailUseCase,
) {}

async findById(id: string): Promise<User | null> {
async findOneById(id: string): Promise<User | null> {
return this.getUserByIdUseCase.execute(id);
}

async findByEmail(email: string): Promise<User | null> {
async findOneByEmail(email: string): Promise<User | null> {
return this.getUserByEmailUseCase.execute(email);
}
}
22 changes: 11 additions & 11 deletions libs/users/domain/src/lib/users.repository.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ class MockUsersRepository implements UsersRepository {
},
];

async findById(id: string): Promise<User | null> {
async findOneById(id: string): Promise<User | null> {
return this.users.find((user) => user.id === id) || null;
}

async findByEmail(email: string): Promise<User | null> {
async findOneByEmail(email: string): Promise<User | null> {
return this.users.find((user) => user.email === email) || null;
}
}
Expand All @@ -28,23 +28,23 @@ describe('UsersRepository', () => {
usersRepository = new MockUsersRepository();
});

test('findById should return a user by id', async () => {
const user = await usersRepository.findById('1');
test('findOneById should return a user by id', async () => {
const user = await usersRepository.findOneById('1');
expect(user).toEqual({
id: '1',
email: '[email protected]',
firstName: 'John',
lastName: 'Doe'
lastName: 'Doe',
});
});

test('findById should return null if user not found', async () => {
const user = await usersRepository.findById('3');
test('findOneById should return null if user not found', async () => {
const user = await usersRepository.findOneById('3');
expect(user).toBeNull();
});

test('findByEmail should return a user by email', async () => {
const user = await usersRepository.findByEmail('[email protected]');
test('findOneByEmail should return a user by email', async () => {
const user = await usersRepository.findOneByEmail('[email protected]');
expect(user).toEqual({
id: '2',
email: '[email protected]',
Expand All @@ -53,8 +53,8 @@ describe('UsersRepository', () => {
});
});

test('findByEmail should return null if user not found', async () => {
const user = await usersRepository.findByEmail('[email protected]');
test('findOneByEmail should return null if user not found', async () => {
const user = await usersRepository.findOneByEmail('[email protected]');
expect(user).toBeNull();
});
});
4 changes: 2 additions & 2 deletions libs/users/domain/src/lib/users.repository.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { User } from './user.entity';

export interface UsersRepository {
findById(id: string): Promise<User | null>;
findByEmail(email: string): Promise<User | null>;
findOneById(id: string): Promise<User | null>;
findOneByEmail(email: string): Promise<User | null>;
}

export const USERS_REPOSITORY = Symbol('USERS_REPOSITORY');
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ describe('MongooseUsersRepository', () => {
{
provide: getModelToken(UserDocument.name),
useValue: {
findById: jest.fn(),
findOneById: jest.fn(),
},
},
],
Expand All @@ -36,11 +36,11 @@ describe('MongooseUsersRepository', () => {
lastName: 'Doe',
};

jest.spyOn(userModel, 'findById').mockReturnValue({
jest.spyOn(userModel, 'findOneById').mockReturnValue({
exec: jest.fn().mockResolvedValue(mockUser),
} as any);

const user = await repository.findById(mockUser.id);
const user = await repository.findOneById(mockUser.id);
expect(user).toBeInstanceOf(User);
expect(user?.id).toBe(mockUser.id);
expect(user?.email).toBe(mockUser.email);
Expand All @@ -49,11 +49,11 @@ describe('MongooseUsersRepository', () => {
});

it('should return null when user is not found', async () => {
jest.spyOn(userModel, 'findById').mockReturnValue({
jest.spyOn(userModel, 'findOneById').mockReturnValue({
exec: jest.fn().mockResolvedValue(null),
} as any);

const user = await repository.findById('507f1f77bcf86cd799439011');
const user = await repository.findOneById('507f1f77bcf86cd799439011');
expect(user).toBeNull();
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ export class MongooseUsersRepository implements UsersRepository {
@InjectModel(UserDocument.name) private userModel: Model<UserDocument>,
) {}

async findById(id: string): Promise<User | null> {
async findOneById(id: string): Promise<User | null> {
const _id = new Types.ObjectId(id);
const userDocument = await this.userModel.findById(_id).exec();
const userDocument = await this.userModel.findOne(_id).exec();

if (!userDocument) {
return null;
Expand All @@ -26,7 +26,7 @@ export class MongooseUsersRepository implements UsersRepository {
);
}

async findByEmail(email: string): Promise<User | null> {
async findOneByEmail(email: string): Promise<User | null> {
const userDocument = await this.userModel.findOne({ email }).exec();

if (!userDocument) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe('UsersResolver', () => {
{
provide: UsersService,
useValue: {
findById: jest.fn(),
findOneById: jest.fn(),
},
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class UsersResolver {

@Query(() => UserDto, { nullable: true })
async getUser(@Args({ name: 'id', type: () => ID }) id: string): Promise<UserDto | null> {
const user: User| null = await this.usersService.findById(id);
const user: User| null = await this.usersService.findOneById(id);

if (!user) {
return null;
Expand Down

0 comments on commit 1c6aa0a

Please sign in to comment.