Dolls with friends
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import { Test, TestingModule } from '@nestjs/testing';
|
||||
import { EventEmitter2 } from '@nestjs/event-emitter';
|
||||
import { DollsService } from './dolls.service';
|
||||
import { PrismaService } from '../database/prisma.service';
|
||||
import { NotFoundException, ForbiddenException } from '@nestjs/common';
|
||||
@@ -30,6 +31,13 @@ describe('DollsService', () => {
|
||||
findFirst: jest.fn().mockResolvedValue(mockDoll),
|
||||
update: jest.fn().mockResolvedValue(mockDoll),
|
||||
},
|
||||
friendship: {
|
||||
findMany: jest.fn().mockResolvedValue([]),
|
||||
},
|
||||
};
|
||||
|
||||
const mockEventEmitter = {
|
||||
emit: jest.fn(),
|
||||
};
|
||||
|
||||
beforeEach(async () => {
|
||||
@@ -40,6 +48,10 @@ describe('DollsService', () => {
|
||||
provide: PrismaService,
|
||||
useValue: mockPrismaService,
|
||||
},
|
||||
{
|
||||
provide: EventEmitter2,
|
||||
useValue: mockEventEmitter,
|
||||
},
|
||||
],
|
||||
}).compile();
|
||||
|
||||
@@ -73,14 +85,14 @@ describe('DollsService', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('findAll', () => {
|
||||
it('should return an array of dolls', async () => {
|
||||
describe('listByOwner', () => {
|
||||
it('should return own dolls without friendship check', async () => {
|
||||
const userId = 'user-1';
|
||||
await service.findAll(userId);
|
||||
await service.listByOwner(userId, userId);
|
||||
|
||||
expect(prismaService.doll.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
userId,
|
||||
userId: userId,
|
||||
deletedAt: null,
|
||||
},
|
||||
orderBy: {
|
||||
@@ -88,6 +100,42 @@ describe('DollsService', () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("should return friend's dolls if friends", async () => {
|
||||
const ownerId = 'friend-1';
|
||||
const requestingUserId = 'user-1';
|
||||
|
||||
// Mock friendship
|
||||
jest
|
||||
.spyOn(prismaService.friendship, 'findMany')
|
||||
.mockResolvedValueOnce([{ friendId: ownerId } as any]);
|
||||
|
||||
await service.listByOwner(ownerId, requestingUserId);
|
||||
|
||||
expect(prismaService.doll.findMany).toHaveBeenCalledWith({
|
||||
where: {
|
||||
userId: ownerId,
|
||||
deletedAt: null,
|
||||
},
|
||||
orderBy: {
|
||||
createdAt: 'asc',
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw ForbiddenException if not friends', async () => {
|
||||
const ownerId = 'stranger-1';
|
||||
const requestingUserId = 'user-1';
|
||||
|
||||
// Mock empty friendship (default)
|
||||
jest
|
||||
.spyOn(prismaService.friendship, 'findMany')
|
||||
.mockResolvedValueOnce([]);
|
||||
|
||||
await expect(
|
||||
service.listByOwner(ownerId, requestingUserId),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('findOne', () => {
|
||||
@@ -107,13 +155,11 @@ describe('DollsService', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('should throw ForbiddenException if doll belongs to another user', async () => {
|
||||
jest
|
||||
.spyOn(prismaService.doll, 'findFirst')
|
||||
.mockResolvedValueOnce({ ...mockDoll, userId: 'user-2' });
|
||||
it('should throw NotFoundException if doll not accessible', async () => {
|
||||
jest.spyOn(prismaService.doll, 'findFirst').mockResolvedValueOnce(null);
|
||||
|
||||
await expect(service.findOne('doll-1', 'user-1')).rejects.toThrow(
|
||||
ForbiddenException,
|
||||
NotFoundException,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -125,6 +171,17 @@ describe('DollsService', () => {
|
||||
|
||||
expect(prismaService.doll.update).toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('should throw ForbiddenException if not owner', async () => {
|
||||
jest
|
||||
.spyOn(prismaService.doll, 'findFirst')
|
||||
.mockResolvedValueOnce({ ...mockDoll, userId: 'user-2' });
|
||||
|
||||
const updateDto = { name: 'Updated Doll' };
|
||||
await expect(
|
||||
service.update('doll-1', 'user-1', updateDto),
|
||||
).rejects.toThrow(ForbiddenException);
|
||||
});
|
||||
});
|
||||
|
||||
describe('remove', () => {
|
||||
@@ -138,5 +195,15 @@ describe('DollsService', () => {
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it('should throw ForbiddenException if not owner', async () => {
|
||||
jest
|
||||
.spyOn(prismaService.doll, 'findFirst')
|
||||
.mockResolvedValueOnce({ ...mockDoll, userId: 'user-2' });
|
||||
|
||||
await expect(service.remove('doll-1', 'user-1')).rejects.toThrow(
|
||||
ForbiddenException,
|
||||
);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user