Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create API endpoints and tests for the entities #2

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
7 changes: 3 additions & 4 deletions backend/__tests__/categoryImage/getCategoryImages.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const categoryImageRepo = AppDataSource.getRepository(CategoryImage);
describe("Get Category Images", () => {
it("should return all category images", async () => {
const mockCategoryImages = [
// Mock category images data as needed
{ id: 1, imageUrl: "image1.jpg", category: { id: 1, name: "Category1" } },
{ id: 2, imageUrl: "image2.jpg", category: { id: 2, name: "Category2" } },
];
Expand All @@ -17,7 +16,7 @@ describe("Get Category Images", () => {
.spyOn(categoryImageRepo, "find")
.mockResolvedValueOnce(mockCategoryImages as any);

const res = await request(app).get("/api/category-image"); // Replace with the actual endpoint
const res = await request(app).get("/api/category-image");
expect(res.status).toEqual(200);
expect(res.body.message).toEqual("Category images retrieved successfully!");
expect(res.body.categoryImages).toEqual(mockCategoryImages);
Expand All @@ -26,7 +25,7 @@ describe("Get Category Images", () => {
it("should return 404 if no category images found", async () => {
jest.spyOn(categoryImageRepo, "find").mockResolvedValueOnce([]);

const res = await request(app).get("/api/category-image"); // Replace with the actual endpoint
const res = await request(app).get("/api/category-image");
expect(res.status).toEqual(404);
expect(res.body.message).toEqual("Category images not found!");
});
Expand All @@ -35,7 +34,7 @@ describe("Get Category Images", () => {
const error = new Error("Internal server error");
jest.spyOn(categoryImageRepo, "find").mockRejectedValueOnce(error);

const res = await request(app).get("/api/category-image"); // Replace with the actual endpoint
const res = await request(app).get("/api/category-image");
expect(res.status).toEqual(500);
expect(res.body.message).toEqual("Error in retrieving category images");
});
Expand Down
93 changes: 93 additions & 0 deletions backend/__tests__/product/createProduct.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const request = require("supertest");
import app from "../../app";
import { AppDataSource } from "../../database/dbConnect";
import { Product } from "../../entities/product";
import { Category } from "../../entities/category";

const productRepo = AppDataSource.getRepository(Product);
const categoryRepo = AppDataSource.getRepository(Category);

describe("Create a product", () => {
it("should create a product", async () => {
const product = {
name: "product 1",
description: "product 1 description",
quantity: 10,
price: 100,
status: "active",
is_active: true,
category: 1,
};

const categoryExists = {
id: 1,
name: "category 1",
description: "category 1 description",
is_active: true,
createdAt: new Date(),
updatedAt: new Date(),
images: [],
products: [],
};

jest
.spyOn(categoryRepo, "findOne")
.mockResolvedValueOnce(categoryExists as any);
jest.spyOn(productRepo, "save").mockResolvedValueOnce(product as any);

const res = await request(app)
.post("/api/product")
.send(product)
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200);

expect(res.body.message).toEqual("Product created successfully!");
const parsedProduct = JSON.parse(JSON.stringify(product));
expect(res.body.product).toEqual(parsedProduct);
});

it("should return an error if the category does not exist", async () => {
const product = {
name: "product 1",
description: "product 1 description",
quantity: 10,
price: 100,
status: "active",
is_active: true,
category: 1,
};

jest.spyOn(categoryRepo, "findOne").mockResolvedValueOnce(null);

const res = await request(app)
.post("/api/product")
.send(product)
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(404);

expect(res.body.message).toEqual("Category not found!");
});
it("should return an error if any required fields are missing", async () => {
const product = {
description: "Test Description",
quantity: 10,
price: 100,
status: "Available",
is_active: true,
category: 1,
};

const res = await request(app)
.post("/api/product")
.send(product)
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(400);

expect(res.body.message).toEqual(
"Missing required fields. Please provide all necessary product details."
);
});
});
73 changes: 73 additions & 0 deletions backend/__tests__/product/deleteProduct.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const request = require("supertest");
import app from "../../app";
import { AppDataSource } from "../../database/dbConnect";
import { Product } from "../../entities/product";

const productRepo = AppDataSource.getRepository(Product);

describe("Delete a product", () => {
it("should delete a product successfully", async () => {
const mockProduct = {
id: 1,
name: "Test Product",
description: "Test Description",
quantity: 10,
price: 100,
status: "Available",
is_active: true,
category: {
id: 1,
name: "Test Category",
description: "Test Category Description",
is_active: true,
createdAt: new Date(),
updatedAt: new Date(),
images: [],
products: [],
},
};

jest
.spyOn(productRepo, "findOne")
.mockResolvedValueOnce(mockProduct as any);
jest.spyOn(productRepo, "remove").mockResolvedValueOnce({} as any);

const res = await request(app)
.delete(`/api/product/${mockProduct.id}`)
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(200);

expect(res.body.message).toEqual("Product deleted successfully!");
});

it("should return an error if the product to delete is not found", async () => {
const productId = 1;

jest.spyOn(productRepo, "findOne").mockResolvedValueOnce(null);

const res = await request(app)
.delete(`/api/product/${productId}`)
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(404);

expect(res.body.message).toEqual("Product not found!");
});

it("should return an error if there is a problem deleting the product", async () => {
const errorMessage = "Error deleting product";

jest
.spyOn(productRepo, "findOne")
.mockRejectedValueOnce(new Error(errorMessage));

const res = await request(app)
.delete(`/api/product/1`)
.set("Accept", "application/json")
.expect("Content-Type", /json/)
.expect(400);

expect(res.body.message).toEqual("Product deletion failed!");
});
});
66 changes: 66 additions & 0 deletions backend/__tests__/product/getAllProducts.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
const request = require("supertest");
import app from "../../app";
import { AppDataSource } from "../../database/dbConnect";
import { Product } from "../../entities/product";
import { Category } from "../../entities/category";

const productRepo = AppDataSource.getRepository(Product);
const categoryRepo = AppDataSource.getRepository(Category);

describe("Get all products", () => {
it("should get all products", async () => {
const products = [
{
id: 1,
name: "product 1",
description: "product 1 description",
quantity: 10,
price: 100,
status: "active",
is_active: true,
category: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
{
id: 2,
name: "product 2",
description: "product 2 description",
quantity: 10,
price: 100,
status: "active",
is_active: true,
category: 1,
createdAt: new Date(),
updatedAt: new Date(),
},
];

jest.spyOn(productRepo, "find").mockResolvedValueOnce(products as any);

const res = await request(app).get("/api/product");

const parsedProducts = JSON.parse(JSON.stringify(products));
expect(res.status).toEqual(200);
expect(res.body.message).toEqual("Products fetched successfully!");
expect(res.body.products).toEqual(parsedProducts);
});

it("should return a 404 if there are no products", async () => {
jest.spyOn(productRepo, "find").mockResolvedValueOnce([]);

const res = await request(app).get("/api/product");
console.log("response body", res.body);
expect(res.status).toEqual(404);
expect(res.body.message).toEqual("No products found!");
});

it("should return a 400 if an error occurs", async () => {
const error = new Error("Internal server error");
jest.spyOn(productRepo, "find").mockRejectedValueOnce(error);

const res = await request(app).get("/api/product");
expect(res.status).toEqual(400);
expect(res.body.message).toEqual("Products fetch failed!");
});
});
51 changes: 51 additions & 0 deletions backend/__tests__/product/getProductById.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const request = require("supertest");
import app from "../../app";
import { AppDataSource } from "../../database/dbConnect";
import { Product } from "../../entities/product";
import { Category } from "../../entities/category";

const productRepo = AppDataSource.getRepository(Product);
const categoryRepo = AppDataSource.getRepository(Category);

describe("Get product by ID", () => {
it("should get a product by ID", async () => {
const product = {
id: 1,
name: "product 1",
description: "product 1 description",
quantity: 10,
price: 100,
status: "active",
is_active: true,
category: 1,
createdAt: new Date(),
updatedAt: new Date(),
};

jest.spyOn(productRepo, "findOne").mockResolvedValueOnce(product as any);

const res = await request(app).get("/api/product/1");

const parsedProduct = JSON.parse(JSON.stringify(product));
expect(res.status).toEqual(200);
expect(res.body.message).toEqual("Product retrieved successfully!");
expect(res.body.product).toEqual(parsedProduct);
});

it("should return a 404 if the product does not exist", async () => {
jest.spyOn(productRepo, "findOne").mockResolvedValueOnce(null);

const res = await request(app).get("/api/product/1");
expect(res.status).toEqual(404);
expect(res.body.message).toEqual("Product not found!");
});

it("should return a 400 if an error occurs", async () => {
const error = new Error("Internal server error");
jest.spyOn(productRepo, "findOne").mockRejectedValueOnce(error);

const res = await request(app).get("/api/product/1");
expect(res.status).toEqual(400);
expect(res.body.message).toEqual("Product fetch failed!");
});
});
Loading