Skip to content
This repository has been archived by the owner on Dec 6, 2024. It is now read-only.

Commit

Permalink
wrote test
Browse files Browse the repository at this point in the history
  • Loading branch information
ikemHood committed Nov 27, 2024
1 parent 6fc5ab1 commit 034bbe1
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 1 deletion.
50 changes: 50 additions & 0 deletions backend/src/product/product.controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductController } from './product.controller';
import { ProductService } from './product.service';
import { PrismaService } from '../prisma/prisma.service';

Check failure on line 4 in backend/src/product/product.controller.spec.ts

View workflow job for this annotation

GitHub Actions / lint-and-build-backend

'PrismaService' is defined but never used
import { ProductDto } from './dto/product.dto';
import { FlagProductDto } from './dto/flag-product.dto';

Check failure on line 6 in backend/src/product/product.controller.spec.ts

View workflow job for this annotation

GitHub Actions / lint-and-build-backend

'FlagProductDto' is defined but never used
import { HttpException, HttpStatus } from '@nestjs/common';

Check failure on line 7 in backend/src/product/product.controller.spec.ts

View workflow job for this annotation

GitHub Actions / lint-and-build-backend

'HttpStatus' is defined but never used

describe('ProductsController', () => {
let controller: ProductController;
Expand All @@ -15,6 +18,8 @@ describe('ProductsController', () => {
provide: ProductService,
useValue: {
submitProduct: jest.fn(),
flagProduct: jest.fn(),
getFlaggedProducts: jest.fn(),
},
},
],
Expand Down Expand Up @@ -64,4 +69,49 @@ describe('ProductsController', () => {
await expect(controller.uploadProduct(dto)).rejects.toThrow();
});
});

describe('flagProduct', () => {
const mockFlagProductDto = {
name: 'Suspicious Product',
image: 'http://example.com/suspicious.jpg',
reason: 'Fake product',
};

it('should successfully flag a product', async () => {
const mockResponse = {
id: '1',
...mockFlagProductDto
};
jest.spyOn(service, 'flagProduct').mockResolvedValue(mockResponse);

const result = await controller.flagProduct(mockFlagProductDto);

expect(result).toEqual(mockResponse);
expect(service.flagProduct).toHaveBeenCalledWith(mockFlagProductDto);
});

it('should throw HttpException when flagging fails', async () => {
jest.spyOn(service, 'flagProduct').mockRejectedValue(new Error('Failed to flag'));

await expect(controller.flagProduct(mockFlagProductDto)).rejects.toThrow(
HttpException,
);
});
});

describe('getFlaggedProducts', () => {
it('should return all flagged products', async () => {
const mockFlaggedProducts = [
{ id: '1', name: 'Product 1', image: 'image1.jpg', reason: 'Fake' },
{ id: '2', name: 'Product 2', image: 'image2.jpg', reason: 'Expired' },
];

jest.spyOn(service, 'getFlaggedProducts').mockResolvedValue(mockFlaggedProducts);

const result = await controller.getFlaggedProducts();

expect(result).toEqual(mockFlaggedProducts);
expect(service.getFlaggedProducts).toHaveBeenCalled();
});
});
});
72 changes: 71 additions & 1 deletion backend/src/product/product.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
import { Test, TestingModule } from '@nestjs/testing';
import { ProductService } from './product.service';
import { ProductDto } from './dto/product.dto';
import { PrismaService } from '../prisma/prisma.service';
import { InternalServerErrorException } from '@nestjs/common';

describe('ProductsService', () => {
let service: ProductService;
let prismaService: PrismaService;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [ProductService],
providers: [
ProductService,
{
provide: PrismaService,
useValue: {
flaggedProduct: {
create: jest.fn(),
findMany: jest.fn(),
},
},
},
],
}).compile();

service = module.get<ProductService>(ProductService);
prismaService = module.get<PrismaService>(PrismaService);
});

it('should be defined', () => {
Expand Down Expand Up @@ -53,4 +68,59 @@ describe('ProductsService', () => {
);
});
});

describe('flagProduct', () => {
const mockFlagProductDto = {
name: 'Suspicious Product',
image: 'http://example.com/suspicious.jpg',
reason: 'Fake product',
};

it('should successfully flag a product', async () => {
const mockCreatedProduct = {
id: '1',
...mockFlagProductDto
};
jest.spyOn(prismaService.flaggedProduct, 'create').mockResolvedValue(mockCreatedProduct);

const result = await service.flagProduct(mockFlagProductDto);

expect(result).toEqual(mockCreatedProduct);
expect(prismaService.flaggedProduct.create).toHaveBeenCalledWith({
data: mockFlagProductDto,
});
});

it('should throw InternalServerErrorException when flagging fails', async () => {
jest.spyOn(prismaService.flaggedProduct, 'create').mockRejectedValue(new Error('Database error'));

await expect(service.flagProduct(mockFlagProductDto)).rejects.toThrow(
InternalServerErrorException,
);
});
});

describe('getFlaggedProducts', () => {
it('should return all flagged products', async () => {
const mockFlaggedProducts = [
{ id: '1', name: 'Product 1', image: 'image1.jpg', reason: 'Fake' },
{ id: '2', name: 'Product 2', image: 'image2.jpg', reason: 'Expired' },
];

jest.spyOn(prismaService.flaggedProduct, 'findMany').mockResolvedValue(mockFlaggedProducts);

const result = await service.getFlaggedProducts();

expect(result).toEqual(mockFlaggedProducts);
expect(prismaService.flaggedProduct.findMany).toHaveBeenCalled();
});

it('should throw InternalServerErrorException when fetch fails', async () => {
jest.spyOn(prismaService.flaggedProduct, 'findMany').mockRejectedValue(new Error('Database error'));

await expect(service.getFlaggedProducts()).rejects.toThrow(
InternalServerErrorException,
);
});
});
});

0 comments on commit 034bbe1

Please sign in to comment.