-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(products): Implement product update and deletion by sellers
- a seller should be able to update a product from their collection - a seller should be able to delete a product from their collection Delivers #187419126
- Loading branch information
Showing
20 changed files
with
933 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import "@testing-library/jest-dom"; | ||
import { | ||
fireEvent, render, screen, waitFor, | ||
} from "@testing-library/react"; | ||
import { Provider } from "react-redux"; | ||
import { BrowserRouter as Router } from "react-router-dom"; | ||
|
||
import store from "../redux/store"; | ||
import { fetchProducts } from "../redux/reducers/productsSlice"; | ||
import ProductsTable from "../components/dashboard/products/ProductsTable"; | ||
|
||
jest.mock("react-dropzone", () => ({ | ||
useDropzone: jest.fn(), | ||
})); | ||
|
||
jest.mock("../redux/api/productsApiSlice", () => ({ | ||
addProduct: jest.fn(), | ||
updateProduct: jest.fn(), | ||
deleteProduct: jest.fn(), | ||
})); | ||
|
||
jest.mock("react-toastify", () => ({ | ||
toast: { | ||
success: jest.fn(), | ||
error: jest.fn(), | ||
}, | ||
})); | ||
|
||
jest.mock("../components/dashboard/ConfirmModal", () => () => ( | ||
<div>ConfirmModal</div> | ||
)); | ||
jest.mock("../components/dashboard/Spinner", () => () => <div>Spinner</div>); | ||
jest.mock( | ||
"../components/dashboard/ToggleSwitch", | ||
() => ({ checked, onChange }) => ( | ||
<div onClick={onChange}> | ||
ToggleSwitch | ||
{checked ? "On" : "Off"} | ||
</div> | ||
), | ||
); | ||
|
||
// Mock data for products | ||
const mockProducts = [ | ||
{ | ||
id: 1, | ||
name: "Product 1", | ||
stockQuantity: 10, | ||
expiryDate: "2024-12-31T00:00:00.000Z", | ||
price: 100, | ||
category: { name: "Electronics" }, | ||
isAvailable: true, | ||
images: ["image1.png"], | ||
}, | ||
{ | ||
id: 2, | ||
name: "Product 2", | ||
stockQuantity: 5, | ||
expiryDate: "2025-06-30T00:00:00.000Z", | ||
price: 50, | ||
category: { name: "Fashion" }, | ||
isAvailable: true, | ||
images: ["image2.png"], | ||
}, | ||
]; | ||
|
||
global.URL.createObjectURL = jest.fn(); | ||
|
||
describe("Products slice tests", () => { | ||
it("should handle products initial state", () => { | ||
expect(store.getState().products).toEqual({ | ||
loading: false, | ||
data: [], | ||
error: null, | ||
}); | ||
}); | ||
|
||
it("should handle products pending", () => { | ||
store.dispatch(fetchProducts.pending("")); | ||
expect(store.getState().products).toEqual({ | ||
loading: true, | ||
data: [], | ||
error: null, | ||
}); | ||
}); | ||
|
||
it("should handle products fulfilled", () => { | ||
const mockData = { message: "success" }; | ||
// @ts-ignore | ||
store.dispatch(fetchProducts.fulfilled(mockData, "", {})); | ||
expect(store.getState().products).toEqual({ | ||
loading: false, | ||
data: mockData, | ||
error: null, | ||
}); | ||
}); | ||
|
||
it("should handle products fetch rejected", () => { | ||
// @ts-ignore | ||
store.dispatch(fetchProducts.rejected(null, "", {})); | ||
expect(store.getState().products.error).toEqual("Rejected"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from "react"; | ||
|
||
interface ConfirmDeleteModalProps { | ||
onConfirm: () => void; | ||
onCancel: () => void; | ||
message: string; | ||
product: any; | ||
loading: boolean; | ||
} | ||
|
||
const ConfirmModal: React.FC<ConfirmDeleteModalProps> = ({ | ||
onConfirm, | ||
onCancel, | ||
message, | ||
product, | ||
loading, | ||
}) => ( | ||
<div className="fixed inset-0 z-40 flex items-center justify-center bg-black bg-opacity-50"> | ||
<div className="bg-white py-8 px-6 rounded-lg duration-75 animate-fadeIn"> | ||
<div className="flex flex-col gap-2 mb-3"> | ||
<h2 className="text-lg">{message}</h2> | ||
<p className="text-gray-700"> | ||
<strong className="text-black"> | ||
{product.name} | ||
. | ||
</strong> | ||
{' '} | ||
This can't be | ||
undone | ||
</p> | ||
</div> | ||
<div className="flex justify-end"> | ||
<button | ||
className="border border-blue-700 text-blue-700 px-4 py-2 rounded mr-2" | ||
onClick={onCancel} | ||
> | ||
Cancel | ||
</button> | ||
<button | ||
className="bg-red-600 text-white px-4 py-2 rounded" | ||
onClick={onConfirm} | ||
> | ||
{loading ? "Loading..." : "Delete"} | ||
</button> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
|
||
export default ConfirmModal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.