efficiency & performance fine tuning
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { FriendsService } from './friends.service';
|
||||
import { PrismaService } from '../database/prisma.service';
|
||||
import {
|
||||
@@ -15,6 +16,7 @@ enum FriendRequestStatus {
|
||||
|
||||
describe('FriendsService', () => {
|
||||
let service: FriendsService;
|
||||
let eventEmitter: EventEmitter2;
|
||||
|
||||
const mockUser1 = {
|
||||
id: 'user-1',
|
||||
@@ -82,6 +84,10 @@ describe('FriendsService', () => {
|
||||
$transaction: jest.fn(),
|
||||
};
|
||||
|
||||
const mockEventEmitter = {
|
||||
emit: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
const module: TestingModule = await Test.createTestingModule({
|
||||
providers: [
|
||||
@@ -90,10 +96,15 @@ describe('FriendsService', () => {
|
||||
provide: PrismaService,
|
||||
useValue: mockPrismaService,
|
||||
},
|
||||
{
|
||||
provide: EventEmitter2,
|
||||
useValue: mockEventEmitter,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
service = module.get<FriendsService>(FriendsService);
|
||||
eventEmitter = module.get<EventEmitter2>(EventEmitter2);
|
||||
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
@@ -110,6 +121,12 @@ describe('FriendsService', () => {
|
||||
mockPrismaService.friendRequest.create.mockResolvedValue(
|
||||
mockFriendRequest,
|
||||
);
|
||||
// Mock transaction implementation
|
||||
mockPrismaService.$transaction.mockImplementation(
|
||||
async (callback: (prisma: any) => Promise<any>) => {
|
||||
return (await callback(mockPrismaService)) as unknown;
|
||||
},
|
||||
);
|
||||
|
||||
const result = await service.sendFriendRequest('user-1', 'user-2');
|
||||
|
||||
@@ -128,6 +145,7 @@ describe('FriendsService', () => {
|
||||
receiver: true,
|
||||
},
|
||||
});
|
||||
expect(mockEventEmitter.emit).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw BadRequestException when trying to send request to self', async () => {
|
||||
@@ -141,6 +159,11 @@ describe('FriendsService', () => {
|
||||
|
||||
it('should throw NotFoundException when receiver does not exist', async () => {
|
||||
mockPrismaService.user.findUnique.mockResolvedValue(null);
|
||||
mockPrismaService.$transaction.mockImplementation(
|
||||
async (callback: (prisma: any) => Promise<any>) => {
|
||||
return (await callback(mockPrismaService)) as unknown;
|
||||
},
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.sendFriendRequest('user-1', 'nonexistent'),
|
||||
@@ -153,6 +176,11 @@ describe('FriendsService', () => {
|
||||
it('should throw ConflictException when users are already friends', async () => {
|
||||
mockPrismaService.user.findUnique.mockResolvedValue(mockUser2);
|
||||
mockPrismaService.friendship.findFirst.mockResolvedValue(mockFriendship);
|
||||
mockPrismaService.$transaction.mockImplementation(
|
||||
async (callback: (prisma: any) => Promise<any>) => {
|
||||
return (await callback(mockPrismaService)) as unknown;
|
||||
},
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.sendFriendRequest('user-1', 'user-2'),
|
||||
@@ -168,6 +196,11 @@ describe('FriendsService', () => {
|
||||
mockPrismaService.friendRequest.findFirst.mockResolvedValue(
|
||||
mockFriendRequest,
|
||||
);
|
||||
mockPrismaService.$transaction.mockImplementation(
|
||||
async (callback: (prisma: any) => Promise<any>) => {
|
||||
return (await callback(mockPrismaService)) as unknown;
|
||||
},
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.sendFriendRequest('user-1', 'user-2'),
|
||||
@@ -185,6 +218,11 @@ describe('FriendsService', () => {
|
||||
senderId: 'user-2',
|
||||
receiverId: 'user-1',
|
||||
});
|
||||
mockPrismaService.$transaction.mockImplementation(
|
||||
async (callback: (prisma: any) => Promise<any>) => {
|
||||
return (await callback(mockPrismaService)) as unknown;
|
||||
},
|
||||
);
|
||||
|
||||
await expect(
|
||||
service.sendFriendRequest('user-1', 'user-2'),
|
||||
@@ -259,6 +297,7 @@ describe('FriendsService', () => {
|
||||
|
||||
expect(result).toEqual(acceptedRequest);
|
||||
expect(mockPrismaService.$transaction).toHaveBeenCalled();
|
||||
expect(mockEventEmitter.emit).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw NotFoundException when request does not exist', async () => {
|
||||
@@ -320,6 +359,7 @@ describe('FriendsService', () => {
|
||||
expect(mockPrismaService.friendRequest.delete).toHaveBeenCalledWith({
|
||||
where: { id: 'request-1' },
|
||||
});
|
||||
expect(mockEventEmitter.emit).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw NotFoundException when request does not exist', async () => {
|
||||
@@ -392,6 +432,7 @@ describe('FriendsService', () => {
|
||||
],
|
||||
},
|
||||
});
|
||||
expect(mockEventEmitter.emit).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw BadRequestException when trying to unfriend self', async () => {
|
||||
|
||||
Reference in New Issue
Block a user